我有一个可用于“活动”的应用程序,现在需要与TabLayout
一起使用。
我已经将我的3个活动转换为3个片段,这些片段已经在ViewPager
和TabLayout
中工作。
IMG
但是现在出现了问题,当我单击菜单时,我创建了一个新的Fragment,并将其作为参数传递给文本。
此片段是在我的ViewPager
顶部创建的,最终无法使用我的标签页。
我根据以下代码使用Fragment Container
:
public class ContainerFragment extends Fragment {
private static TabLayoutSetupCallback mToolbarSetupCallback;
private List<String> mTabNamesList;
static ViewPager viewPager;
static ContainerFragment.ItemsPagerAdapter adapter;
public ContainerFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MainActivity) {
mToolbarSetupCallback = (TabLayoutSetupCallback) context;
} else {
throw new ClassCastException(context.toString() + " must implement TabLayoutSetupCallback");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//android.support.v4.app.Fragment fragment = null;
//fragment = GridFragment.newInstance("6360392529587060753");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//Fragment 1
android.support.v4.app.Fragment fragment = null;
fragment = new Galeria();
//Fragment 2
android.support.v4.app.Fragment fragment2 = new PageFragment();
//Fragment 3
final String query = "Morning";//textView.getText().toString().trim();
android.support.v4.app.Fragment fragment3 = new SearchActivity(); // replace your custom fragment class
Bundle bundle = new Bundle();
bundle.putString(query, query); // use as per your need
fragment3.setArguments(bundle);
//fragmentTransaction.addToBackStack(null);
// fragmentTransaction.replace(R.layout.activity_search,fragment);
// fragmentTransaction.commit();
//View view = inflater.inflate(R.layout.fragment_container, container, false);
//ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
//viewPager.setAdapter(new ItemsPagerAdapter(getChildFragmentManager(), mTabNamesList));
//mToolbarSetupCallback.setupTabLayout(viewPager);
View view = inflater.inflate(R.layout.fragment_container, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
adapter = new ContainerFragment.ItemsPagerAdapter(getChildFragmentManager());
adapter.addFrag(fragment, "ONE");
adapter.addFrag(fragment2, "TWO");
adapter.addFrag(fragment3, "THREE");
viewPager.setOffscreenPageLimit(5);
viewPager.setAdapter(adapter);
mToolbarSetupCallback.setupTabLayout(viewPager);
return view;
}
public static class ItemsPagerAdapter extends FragmentStatePagerAdapter {
private List<String> mTabs = new ArrayList<>();
private static List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ItemsPagerAdapter(FragmentManager fm) {
super(fm);
//mTabs = tabNames;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getItemPosition(Object object) {
/* if (object instanceof UpdateableFragment) {
((UpdateableFragment) object).update(updateData);
}*/
//don't return POSITION_NONE, avoid fragment recreation.
return super.getItemPosition(object);
//return POSITION_NONE;
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void replaceFragment(Fragment fragment, String title, int index) {
mFragmentList.remove(index);
mFragmentList.add(index, fragment);
mFragmentTitleList.add(title);
}
}
public interface TabLayoutSetupCallback {
void setupTabLayout(ViewPager viewPager);
}
}
我的班次MainActivity
我认为我的问题或错误就在主机上!
if (savedInstanceState == null) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new ContainerFragment());
transaction.commit();
}
我的布局activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ad_view"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<!-- <include layout="@layout/sample" />
<include layout="@layout/content_main" />
-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/activity_main_drawer"
app:headerLayout="@layout/nav_header_main"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.v4.widget.DrawerLayout>
我的布局fragment_content 这是我的碎片容器布局
<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"
tools:context=".ContainerFragment">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewPager"/>
</FrameLayout>
我已经尽一切可能使新的Fragment在TabLayout
corretatente中打开,但总是在上方创建。
android.support.v4.app.Fragment fragment3 = new SearchActivity(); // replace your custom fragment class
String className = fragment3.getClass().getName();
Bundle bundle = new Bundle();
android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
bundle.putString(KEY_QUERY, suggestion); // use as per your need
fragment3.setArguments(bundle);
fragmentTransaction.addToBackStack(className);
fragmentTransaction.replace(R.id.container,fragment3);
fragmentTransaction.isAddToBackStackAllowed();
fragmentTransaction.commit();
下面用于打开新片段的代码,应创建一个向其传递参数的新片段,并在TabLayout
和ViewPager
内部正常打开。
请帮助我,我已经花了2天的时间才能成功完成工作,我几乎完全更改了应用程序,仅恢复了没有选项卡的活动。
对不起,我的英语。
答案 0 :(得分:0)
尝试像这样更改您的FragmentPagerAdapter类:-
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title, int position) {
mFragmentList.add(position, fragment);
mFragmentTitleList.add(position, title);
}
public void removeFragment(Fragment fragment, int position) {
mFragmentList.remove(position);
mFragmentTitleList.remove(position);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
在片段的onCreate()
中添加setHasOptionsMenu(true)
,以在片段中添加与活动不同的选项菜单。
步骤2:现在添加onCreateOptionsMenu方法,该方法将被覆盖为:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.filter, menu); // Use filter.xml from step 1
}
第3步:第4步:现在添加onOptionsItemSelected方法,通过该方法,您可以在从actionBar中选择添加的操作图标时使用addFragment(Fragment fragment, String title, int position)
和removeFragment(Fragment fragment, int position)
动态添加/删除片段:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_filter){
//Do whatever you want to do
// mSectionsPagerAdapter.addFragment(new HomeFeedFragments (), "HOME",0);
// mSectionsPagerAdapter.addFragment(new BlankFragment(), "NEWS", 1);
// viewPager.setAdapter(mSectionsPagerAdapter);
//or
// mSectionsPagerAdapter.removeFragment(new BlankFragment(), 1);
//mSectionsPagerAdapter.notifyDataChange();
return true;
}
return super.onOptionsItemSelected(item);
}
别忘了调用notifyDataChange()。
对于第一次启动,您应该设置Adapter:-
private void setupViewPager(ViewPager viewPager) {
mSectionsPagerAdapter.addFragment(new HomeFeedFragments(), "HOME",0);
mSectionsPagerAdapter.addFragment(new BlankFragment(), "NEWS", 1);
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(mSectionsPagerAdapter);
}
希望这会有所帮助。