【发布时间】:2014-01-09 10:21:04
【问题描述】:
在我的应用程序中,我有一个 ViewPager,其中每个页面都显示一个列表。当我开始时,我直接将 ListFragement 添加到 ViewPager。为了更改每个页面的布局(例如在每个列表下方添加 ViewText),我后来创建了一个带有自己的布局文件的片段,其中包含此 listFragement,我将其添加到 ViewPager
这就是这个布局(每个页面的外观)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/tab_fragment"
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:id="@+id/message_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/lightgreen"
android:orientation="horizontal"
android:paddingBottom="6sp"
android:paddingTop="6sp"
android:visibility="visible" >
<TextView
android:id="@+id/message"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="@string/emptyString"
android:gravity="center_horizontal"
android:text="@string/emptyString"
android:textAlignment="center"
android:textColor="@color/green"
android:textStyle="bold" />
</LinearLayout>
这是 ViewPager 的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white" >
</android.support.v4.view.ViewPager>
这就是我如何将每个 ListFragement 添加到其父 Fragment(具有相同的布局)(在本例中为联系人列表)
public class FragmentTabContacts extends FragmentTab{
@Override
protected Fragment getFragment() {
return new ListFragmentContactlist();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.fragment = getFragment();
View view = inflater.inflate(R.layout.tab_fragement, null);
messageCanvas = (ViewGroup)view.findViewById(R.id.message_canvas);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.tab_fragment, this.fragment);
fragmentTransaction.commit();
return view;
}
}
这是 ViewPager 的适配器
public class FragmentAdapter extends FragmentPagerAdapter {
/**
* list of {@link NinjaListFragment} used for all tabs
*/
private List<ITabContainer> fragments;
public FragmentAdapter(FragmentManager fm, List<ITabContainer> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public Fragment getItem(int position) {
return (Fragment) fragments.get(position);
}
@Override
public int getItemPosition(Object object) {
return fragments.indexOf(object);
}
}
这是我在 ViewPager 中编写的用于为其设置新片段以及相应选项卡的方法
/**
* adds the {@link ViewGroup} representing the tab to the {@value #mapTabs} for generating the tabs
* @param inflater the {@link LayoutInflater} used to inflate the tab
* @param tabs the {@link ViewGroup} containing the inflated tabs.
* @param tabContainer the fragment with the interface {@link ITabContainer} used as content for the tab added to {@link #fragments}
* @param imageId the id of the image to be shown on the tab
* @param onClickListener the {@link View.OnClickListener} to receive the tab clicks
*/
private void addTab(LayoutInflater inflater, ViewGroup tabs, ITabContainer tabContainer, int imageId, View.OnClickListener onClickListener) {
inflater.inflate(R.layout.main_tab, tabs);
int index = tabs.getChildCount() - 1;
ImageView ivTab = (ImageView) tabs.getChildAt(index);
ivTab.setTag(String.valueOf(index));
ivTab.setImageResource(imageId);
ivTab.setOnClickListener(onClickListener);
mapTabs.put(index, ivTab);
fragments.add(tabContainer);
}
这就是我在 ViewPager 的 onCreate 方法中调用此方法的方式
addTab(inflater, vgTabs, new FragmentTabContacts(), R.drawable.ic_tab_contacts, onClickListener);
addTab(inflater, vgTabs, new FragmentTabHistory(), R.drawable.ic_tab_history, onClickListener);
addTab(inflater, vgTabs, new ListFragmentGroups(), R.drawable.ic_tab_groups, onClickListener);
addTab(inflater, vgTabs, new FragmentTabFavorits(), R.drawable.ic_tab_favorits, onClickListener);
因为对于我添加 ListFragement 的 Framelayout,具有相同布局的所有片段自然包含相同的 id (android:id="@+id/tab_fragment"),看来 ViewPager 在识别每个片段时都有问题他们。我只以这种方式完成了前 2 页(第 4 个选项卡包含另一个 ViewPager,这是可行的),但结果是第一页现在显示了第二页的内容,而第二页是空的。
我想为每个页面设置一个单独的 TextView,因为我想在每个页面中存储不同的文本,并且我不想复制相同的布局(只是使用不同的 id)4 次(以防没有其他解决方案它)。我不想将该 TextView 放在主 PageView 布局中并在每个选项卡切换后刷新内容。
如何为每个页面的每个 ListFragement 重复使用相同的布局?
【问题讨论】:
-
我也是这样。不能重用布局(因为相同的 id)。如果可以消除多余的布局,那就太好了。
标签: android android-layout android-fragments android-viewpager