问题描述
我尝试设置以下导航在我的应用程序:
I try to setup following navigation in my app:
实际情况:ViewPager +标签,以列表间刷卡:
Actual condition: ViewPager + Tabs to swipe between lists:
- ListFragment A
- ListFragment乙
所需的条件:ViewPager +标签:
Desired condition:ViewPager + Tabs:
- ListFragment一个onListItemSelected取代ListFragment A和DetailFragment A
- 在ListFragment乙onListItemSelected取代ListFragment B带DetailFragment乙
的目的是要显示的标签导航内部细节片段。我不能代替fragmentList由detailFragment(该fragmentList没有自定义布局,因此没有身份证,我不知道该怎么做)。此外,在开始一个新的活动隐藏标签栏。
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
- 寻呼机只知道它拥有包装的碎片,而这些将管理他们自己的内容。
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的视图容器
- 因为我们使用视图这个ID把孩子片段 - MyListFragment
或 DetailsFragment
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 容器
):
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与标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!