简体   繁体   English

ListPage行内的Viewpager

[英]Viewpager inside listview row

I have listview in which each list item contains some textviews and one viewpager. 我有一个listview,其中每个列表项都包含一些textviews和一个viewpager。 I want to scrollable images in each listitem via viewpager just like Airbnb app. 我想像Airbnb应用程序一样,通过viewpager在每个列表项中滚动图像。

I have tried to implement it but problem is that each time in list adapter I have to create new viewpager instance, set a new id to viewpager. 我尝试实现它,但问题是每次在列表适配器中我都必须创建新的viewpager实例,并为viewpager设置新的ID。 Also each time I have to create new FragmentStatePagerAdapter and set it to viewpager. 同样,每次我必须创建新的FragmentStatePagerAdapter并将其设置为viewpager时。 The app crash on reuse of viewpager and adapter. 重复使用Viewpager和适配器会导致应用崩溃。

So is there any way or library available to integrate viewpager in listview?? 那么有什么方法或库可用于将viewpager集成到listview中?

Here is my code I am using SimpleCursorAdapter for listview and SimpleCursorAdapter.ViewBinder to bind viewpager adapter. 这是我的代码,我正在使用SimpleCursorAdapter进行listview,并使用SimpleCursorAdapter.ViewBinder绑定viewpager适配器。

SimpleCursorAdapter.ViewBinder SimpleCursorAdapter.ViewBinder

...

    enter code here
    listPager = new ViewPager(MainListActivity.this);
    listPager.setId(R.id.viewpager_id_base + cursor.getPosition() + 1);
    listPager.setPageMargin(-(int) (pageMargin * 1.5));
    listPager.setLayoutParams(pagerParams);

    imageList      = MainListDAO.getMedia(rowId);
    mediaAdapter   = new ViewPagerAdapter(getSupportFragmentManager(), imageList);
    mediaAdapter   . setCount(imageList.size());
    listPager.setAdapter(mediaAdapter);

...


listitem.xml
...
  <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingTop="5dp" >

                <TextView
                    android:id="@+id/txtCommentCount"
                    style="@style/textStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <ImageView
                    android:id="@+id/txtSending"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/clock" />

                <ImageView
                    android:id="@+id/imgMute"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:padding="5dp"
                    android:src="@drawable/ic__mute_small" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lytMedia"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_default_margin"
                android:focusable="false"
                android:focusableInTouchMode="true" >
            </android.support.v4.view.ViewPager>


        </LinearLayout>

... ...

I did try implement the same, but no success. 我确实尝试实现相同的方法,但没有成功。

I think that the app implements a custom component. 我认为该应用程序实现了自定义组件。 It don't work in all devices eg: Moto G, Samsung Galaxy Note II. 它不适用于所有设备,例如:Moto G,三星Galaxy Note II。 The items will be showed like a list and the horizontal pager will be disabled. 这些项目将像列表一样显示,并且水平寻呼机将被禁用。

You could use AndroidImageSlider library to implement each item in a Recyclerview. 您可以使用AndroidImageSlider库在Recyclerview中实现每个项目。 You must create a custom adapter that implements the onBindViewHolder and set the SliderLayout attributes in this method. 您必须创建一个实现onBindViewHolder的自定义适配器,并在此方法中设置SliderLayout属性。 I did it ... and works. 我做到了...并且可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM