[英]ViewPager with Fragment with different layouts
I am new to using ViewPagers
and Fragments
together. 我是新手一起使用
ViewPagers
和Fragments
。 My app has a ListView
on startup, which bring the user to a details view based on a id that is passed. 我的应用程序在启动时有一个
ListView
,它根据传递的id将用户带到详细信息视图。 The problem I'm running into is that the details view has a different layout then the list view. 我遇到的问题是详细信息视图具有与列表视图不同的布局。 The
ViewPager
is on the intial screen's layout, so when using the ViewPager
the layout retains the inital layout, which is basically just a status bar at the bottom, which should go away when the user is on the details view. ViewPager
位于初始屏幕的布局中,因此在使用ViewPager
,布局保留了初始布局,这基本上只是底部的状态栏,当用户在详细信息视图中时,它应该消失。
This is what I have so far: 这是我到目前为止:
Main.xml (initial layout on startup, with a status bar) Main.xml (启动时的初始布局,带状态栏)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<!-- Footer -->
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<!-- Footer -->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@id/status
/>
</RelativeLayout>
Main.class Main.class
public class Main extends SherlockFragmentActivity
{
private static int NUMBER_OF_PAGES;
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
NUMBER_OF_PAGES = 5;
fragments = new ArrayList<Fragment>();
fragments.add(new ListingFragment()); //initial screen that just a ListView
fragments.add(DetailsFragment.newInstance(1));
fragments.add(DetailsFragment.newInstance(2));
fragments.add(DetailsFragment.newInstance(3));
fragments.add(DetailsFragment.newInstance(4));
}
private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
return fragments.get(index);
}
@Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
}
Details.xml (details view with no status bar) Details.xml (没有状态栏的详细信息视图)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</RelativeLayout>
DetailsFragment.class DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
private int detailId;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
public static DetailsFragment newInstance(int id) {
DetailsFragment lf = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
lf.setArguments(bundle);
return lf;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
detailId = getArguments().getInt("id");
return view;
}
}
The ViewPager is on the intial screen's layout, so when using the ViewPager the layout retains the inital layout, which is basically just a status bar at the bottom, which should go away when the user is on the details view.
ViewPager位于初始屏幕的布局中,因此在使用ViewPager时,布局保留了初始布局,这基本上只是底部的状态栏,当用户在详细信息视图中时,它应该消失。
Then don't put it in the main layout file. 然后不要把它放在主布局文件中。 If the status is tied to the first fragment then it belong in its view, especially if the
DetailsFragment
doesn't need to show that status TextView
. 如果状态与第一个片段绑定,则它属于其视图,特别是如果
DetailsFragment
不需要显示该状态TextView
。
So remove the status TextView
from main.xml and add it to the layout file used in the ListingFragment
. 因此,从main.xml中删除状态
TextView
并将其添加到ListingFragment
使用的布局文件中。 If ListingFragment
extends ListFragment
then create a layout file which contains a ListView
with the id( android:id="@android:id/list"
) + the status TextView
and inflate this layout file in the onCreateView
. 如果
ListingFragment
扩展了ListFragment
则创建一个布局文件,其中包含一个带有id( android:id="@android:id/list"
)的ListView
+状态TextView
并在onCreateView
膨胀此布局文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.