android - 具有不同布局的 Fragment 的 ViewPager

标签 android android-fragments android-viewpager

我不熟悉同时使用 ViewPagersFragments。我的应用程序在启动时有一个 ListView,它会根据传递的 id 将用户带到详细信息 View 。我遇到的问题是详细信息 View 的布局与 ListView 不同。 ViewPager 在初始屏幕的布局上,所以当使用 ViewPager 时,布局保留初始布局,基本上只是底部的状态栏,应该消失当用户在详细信息 View 中时。

这是我目前所拥有的:

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>

主类

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(没有状态栏的详细 View )

 <?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

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.

那就不要放在主布局文件中了。如果状态与第一个 fragment 相关联,那么它属于它的 View ,特别是如果 DetailsFragment 不需要显示该状态 TextView

因此从 main.xml 中删除 status TextView 并将其添加到 ListingFragment 中使用的布局文件中>。如果 ListingFragment 扩展了 ListFragment 然后创建一个布局文件,其中包含一个 ListView id(android:id="@android:id/list") + 状态 TextView 并在 onCreateView 中填充此布局文件。

关于android - 具有不同布局的 Fragment 的 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411226/

相关文章:

android - 尽管在 onDestroyView 中将父 View 设置为 null,但 fragment View 内存泄漏

android - 当应用程序处于后台时, Activity 和 fragment 会发生什么

android - ViewPager + FragmentStatePagerAdapter : ViewPager stops working when screen orientation changes

java - Android - Webview HTML 代码提取不起作用(Javascript)

android - 如何在android jetpack compose中显示工具提示

android - 从 map 中删除单个项目

java - fragment 数组中 "this"上的错误第一个参数类型错误

android - 如何使用 "Buttons"将 "table row"放入框架中?

android - Android 中的嵌套 viewpager 问题

android - 如何为 FragmentPagerAdapter 中的 Fragment 调用一些方法?