gpt4 book ai didi

android - 带有 viewpager 的嵌套 fragment ,其中 viewpager 未加载 fragment

转载 作者:行者123 更新时间:2023-11-29 01:19:58 25 4
gpt4 key购买 nike

我有一个正在加载 fragment 的 Activity

Home fragmentA = new Home();
android.support.v4.app.FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();
fragmentTransactionA.replace(R.id.mainFrame, fragmentA);
fragmentTransactionA.commitAllowingStateLoss();

在我的主页 fragment 中,我有一个 slidingtablayout 和 viewpager

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="mobile.data.woises.fragment.Home">

<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"/>

</FrameLayout>

我的主页 fragment 的 java 代码是这样的

public class Home extends Fragment {

View infHome;
SlidingTabLayout slidingTabLayout;
ViewPager viewPager;
private FragmentPageAdapterHome Pa;
CharSequence Titles[] = {"Requirements", "Notification"};
int Numboftabs = 2;

public Home() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
infHome = inflater.inflate(R.layout.fragment_home2, container, false);

return infHome;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
slidingTabLayout = (SlidingTabLayout) infHome.findViewById(R.id.sliding_tabs);
viewPager = (android.support.v4.view.ViewPager) infHome.findViewById(R.id.viewpager);

// adapter = new ViewPagerAdapter(getResources(), getChildFragmentManager());
// FragmentManager fragmentManager = getChildFragmentManager();
Pa = new FragmentPageAdapterHome(getChildFragmentManager(), Titles, Numboftabs, getActivity().getApplicationContext());
viewPager.setAdapter(Pa);
slidingTabLayout.setSelectedIndicatorColors(Color.parseColor("#0bbea5"));
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
}

}

我的 FragmentPageAdapterHome 是这个

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.Map;

import mobile.data.woises.fragment.Notification;
import mobile.data.woises.fragment.Requirements;

/*import com.example.uday.innohabit.fragments.FragA;
import com.example.uday.innohabit.fragments.FragB;
import com.example.uday.innohabit.fragments.FragC;*/


/**
* Created by Uday on 05-11-2015.
*/
public class FragmentPageAdapterHome extends FragmentPagerAdapter {


private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;
private Context mContext;
private String WoisesId;
CharSequence Titles[];
int NumofTabs;
//int icons[] = {R.drawable.search,R.drawable.cart,R.drawable.quickorder};

//String tabTitle;
public FragmentPageAdapterHome(FragmentManager fm, CharSequence mTitles[],
int mNumbOfTabsumb, Context context) {
super(fm);


this.Titles = mTitles;
this.NumofTabs = mNumbOfTabsumb;


mFragmentManager = fm;
mFragmentTags = new HashMap<Integer, String>();
mContext = context;

// TODO Auto-generated constructor stub


}


@Override
public Fragment getItem(int arg0) {
Log.d("FragmentPageAdapterHome","Arg="+arg0);
switch (arg0) {
case 0:
Requirements requirements = new Requirements();
return requirements;
case 1:
Notification notification = new Notification();
return notification;
default:
break;
}
return null;
}

@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return NumofTabs;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
// record the fragment tag here.
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
}
return obj;
}

public Fragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return mFragmentManager.findFragmentByTag(tag);
}
}

要求中的所有日志和通知 fragment 都显示在 Logcat 中,这意味着正在调用该函数但未显示该 fragment 请帮助我哪里出错了。我正在使用 getsupportfragmentmanager() 但有人在堆栈溢出上发布说我应该在嵌套 fragment 时使用 getChildFragmentManager() 。我该怎么办。请帮助我,我无法弄清楚是什么问题。

最佳答案

您将 ViewPagers height 设置为 0pxlayout_weightLinearLayout 的一项功能,因为您使用的是 FrameLayout,所以不会显示任何内容。可能的修复:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="mobile.data.woises.fragment.Home">

<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"/>

</LinearLayout>

//or:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="mobile.data.woises.fragment.Home">

<mobile.data.woises.tabs.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_color"
app:tabIndicatorColor="#0bbea5"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"/>

</FrameLayout>

另外,不要使用 px,因为您的布局在不同的屏幕上看起来会有所不同。请改用 dp(密度点)!

关于android - 带有 viewpager 的嵌套 fragment ,其中 viewpager 未加载 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com