将 ListFragment 替换为带有选项卡的 ViewPager 中的 Fragment [英] Replace ListFragment with Fragment inside ViewPager with Tabs

查看:25
本文介绍了将 ListFragment 替换为带有选项卡的 ViewPager 中的 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用中设置以下导航:

I try to setup following navigation in my app:

实际情况:ViewPager + Tabs 在列表之间滑动:

Actual condition: ViewPager + Tabs to swipe between lists:

  • 列表片段A
  • 列表片段 B

所需条件:ViewPager + 标签:

Desired condition: ViewPager + Tabs:

  • ListFragment A onListItemSelected 将 ListFragment A 替换为 DetailFragment A
  • ListFragment B onListItemSelected 用 DetailFragment B 替换 ListFragment B

目标是在标签导航中显示细节片段.我不能用 detailFragment 替换 fragmentList(fragmentList 没有自定义布局,因此没有 ID,我不知道该怎么做).此外,启动新活动会隐藏标签栏.

The goal is to display the detail fragments inside the tab navigation. I can't replace the fragmentList by a detailFragment (the fragmentList has no custom layout and therefore no ID AND i don't know how to do it). Also, starting a new activity hides the tab bar.

有人可以帮我吗?

推荐答案

我会制作一个包装片段,它知道要显示什么 - 列表或细节.这将与 ViewPager 完全分离——pager 只知道它包含包装片段,而这些片段会自己管理它们的内容.

I would make a wrapper fragment which knows what to show - list or details. This would be completely decoupled from ViewPager - pager would only know it holds wrapper fragments, and these would manage their content themselves.

实施将遵循以下路线:

public class WrapperFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyListFragment list = new MyListFragment();
        list.setListAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override public void onItemClick(AdapterView<?> l, View v, int position, long id) {
              // Create details fragment based on clicked item's position
              Fragment details = new Fragment();
              getChildFragmentManager()
                  .beginTransaction()
                  .replace(R.id.container, details)
                  .addToBackStack(null)
                  .commit();
            }
        });

        getChildFragmentManager()
            .beginTransaction()
            .add(R.id.container, list)
            .commit();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // There has to be a view with id `container` inside `wrapper.xml`
        return inflater.inflate(R.layout.wrapper, container, false);
    }

    public class MyListFragment extends ListFragment {

        private OnItemClickListener listener;

        public void setOnItemClickListener(OnItemClickListener l) {
            this.listener = l;
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            if(listener != null) {
              listener.onItemClick(l, v, position, id);
            }
        }
    }
}

<小时>

wrapper.xml.想法是 WrapperFragment 必须提供一个布局,其中包含一个 ID 为 container 的视图 - 因为我们正在使用带有此 ID 的视图来放置子片段 - MyListFragmentDetailsFragment.


wrapper.xml. Idea is that WrapperFragment has to provide a layout which contains a view with id container - because we're using view with this id to put child fragment - MyListFragment or DetailsFragment.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

<小时>

另一种方式.这应该可行,但您必须尝试一下(布局本身具有 id,而不是具有 id container 的孩子):


Another way. This should work, but you'll have to try that out (layout has id itself, rather than having a child with id container):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

这篇关于将 ListFragment 替换为带有选项卡的 ViewPager 中的 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆