Android标签显示错误的片段

时间:2017-07-10 08:40:27

标签: android android-fragments android-viewpager android-tabs

您好,我正在尝试使用标签实施一项活动。一些选项卡包含显示简单列表的片段,其他选项卡包含像列表一样的树视图。 具有简单列表的片段工作正常,而我在显示树视图的列表中遇到麻烦:树构建正确,但我将它们放在错误的选项卡中。在第一个选项卡中,我总是看到第二个选项卡的内容,然后向后滑动或展开/折叠树会产生对整个树的新的不需要的替换。

我正在努力去理解我做错了什么。我希望你能建议我做什么。提前致谢! 这是我的代码:

寻呼机适配器:

 public class PagerAdapter extends FragmentStatePagerAdapter {
private final List<ListFragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();


public PagerAdapter(FragmentManager manager)
{
    super(manager);
}

@Override
public ListFragment getItem(int index) {

    return mFragmentList.get(index);

}

@Override
public int getCount()
{
    return mFragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {

   return mFragmentTitleList.get(position);
}
public void addFragment(int index, boolean tree) {
   Bundle b=new Bundle();
   String category= Items.getCategoryByIndex(index);
   b.putString(Items.CATEGORY,category);

   ListFragment fragment;
   if(!tree) {
       fragment = new ContentFragment();

   } else {
       fragment = new TreeFragment();

   }

   fragment.setArguments(b);
   mFragmentList.add(fragment);
   mFragmentTitleList.add(Items.getCategoryByIndex(index));
}
}

这是TreeFragment类:

public class TreeFragment extends TreeViewFragment {

TreeListAdapter mAdapter;

public TreeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Bundle b = getArguments();
    String category = (String) b.get(Items.CATEGORY);

    GlobalVariables.dataList = Items.getTreeNodeByCategory(category);

    GlobalVariables.nodes = TreeViewLists.LoadInitialNodes(GlobalVariables.dataList);

    TreeViewLists.LoadDisplayList();

    mAdapter = new TreeListAdapter(inflater.getContext());
    setListAdapter(mAdapter);
    return super.onCreateView(inflater, container, savedInstanceState);

}
}

并在活动中:

public class MyActivity extends AppCompatActivity {

 private ViewPager mViewPager;
 private PagerAdapter mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mAdapter = new PagerAdapter(getSupportFragmentManager());

   // (...) Content creation (...) 

    mAdapter.addFragment(0, true);
    mAdapter.addFragment(1, true);
    mAdapter.addFragment(2, true);
    mAdapter.addFragment(3, true);
    mAdapter.addFragment(4, false);
    mAdapter.addFragment(5, false);

    mViewPager.setAdapter(mAdapter);

    TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(mViewPager);
}

0 个答案:

没有答案