【问题标题】:ViewPager of views (not fragments) as header of a gridview (Etsy's StaggeredGridView) in a fragment视图(不是片段)的 ViewPager 作为片段中网格视图(Etsy 的 StaggeredGridView)的标题
【发布时间】:2014-10-22 20:30:20
【问题描述】:

所以,我想添加视图的 ViewPager(不是片段)作为片段中 gridview 的标题,但是当我将 ViewPager 设置为我的标题视图时,它没有出现。我目前正在使用 Etsy 的 StaggeredGridView 让我的 gridview 有一个标题,并且我使用 PagerAdapter 而不是 FragmentStatePagerAdapter 能够滚动视图而不是片段。我打算只使用片段,但我不确定这是否会很好,因为我认为这将被视为具有嵌套片段,仅在 4.2+ 和支持库中受支持:我的应用支持 4.0及以上,目前,我正在使用本机片段。

我也尝试以编程方式设置视图,但仍然没有显示。

我在 ViewPager 中设置了一个 toast 以查看是否实际创建了任何内容,并且 toast 会按预期显示,但仍然没有标题。我试图夸大的观点不应该是问题,因为如果我将它单独用作标题,它就会显示。这是我到目前为止所拥有的:

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //inflate fragment which contains the gridview. Have a pointer to the gridview.
    View v = inflater.inflate(R.layout.fragment_menu, container, false);
    mGridView = (StaggeredGridView) v.findViewById(R.id.dishesGridView);

    View header = inflater.inflate(R.layout.view_pager_header, container, false);
    ViewPager mPager = (ViewPager) header.findViewById(R.id.pager);

    //list of views to test header
    final List<View> views = new ArrayList<View>();
    views.add(inflater.inflate(R.layout.menu_header, null));

    mPager.setAdapter(new PagerAdapter() {
        public View getView(int position) {
            return views.get(position);
        }
        @Override
        public Object instantiateItem(ViewGroup collection, int position) {
            View v = getView(position);
            ((ViewPager)collection).addView(v);
             Toast.makeText(getActivity(),
                        "Hello ViewPager",
                        Toast.LENGTH_SHORT).show();
            return v;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == (View) arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return views.size();
        }
    });
    mGridView.addHeaderView(mPager);
    return v;
}

这是 view_pager_header.xml:

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

这里是 menu_header.xml:

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

<FrameLayout
    android:id="@+id/resHeader"
    android:layout_width="wrap_content"
    android:layout_height="100dp" >

    <com.platey.IonPlatey.RectangleImageView
        android:id="@+id/restaurant_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <ImageView
        android:src="@drawable/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>
<TextView
    android:id="@+id/resType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingRight="15dp"
    android:text="ResType Here"
    android:textColor="#d82727"
    android:textStyle="bold" />
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-fragments android-viewpager android-gridview


    【解决方案1】:

    原因:

    基本上问题是由ViewPagerandroid:layout_height="match_parent"引起的。

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

    match_parent 在这里肯定是错误的,因为您不希望标题填充整个父级。但不管怎样——match_parentStaggeredGridView 中将不起作用。

    这与标准 ListView 项目等中的相同。 - 不允许使用 match_parent,在执行布局时它将仅替换为 wrap_content。 不幸的是,ViewPager 不支持 wrap_content,因为 ViewPager 无法包装它的内容(它不知道儿童的大小)。

    解决方案:

    您有两种方法可以在此处正确显示ViewPager

    1. 只需指定一个静态高度(例如50dp
    2. 如果您不想要静态高度,请扩展您的 ViewPager 并执行您自己的测量。

    截图

    这里还有match_parent的截图。可以看到 ViewPager 也没有显示(高度为零):

    并且具有静态高度:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    

    【讨论】:

    • 你,先生,真棒。你的回答非常清楚和彻底。我从不知道您不能使用 match_parent,因为在我的 menu_header.xml 中,我使用 match_parent 作为相对布局的高度。我也在其他地方使用这个没有问题。我想我错误地认为这些标题布局成为通过调用 addHeaderView 创建的一些新视图的子级,事后看来仍然不应该。我猜我的其他标题布局有效,因为我的相对布局的孩子有固定的高度。无论如何,谢谢。
    • 哦,等等,没关系,你说 match_parent 会自动更改为 wrap_content。这就是我的其他观点奏效的原因。
    • @Maciej 如果我想要那个高度以编程方式。?我的意思是 Devicewidth / 2?
    • @GreenRobo 您可以尝试在标题视图上使用getLayoutParams() 并将其height 设置为所需值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    相关资源
    最近更新 更多