[英]Viewpager inside listview row
我有一個listview,其中每個列表項都包含一些textviews和一個viewpager。 我想像Airbnb應用程序一樣,通過viewpager在每個列表項中滾動圖像。
我嘗試實現它,但問題是每次在列表適配器中我都必須創建新的viewpager實例,並為viewpager設置新的ID。 同樣,每次我必須創建新的FragmentStatePagerAdapter並將其設置為viewpager時。 重復使用Viewpager和適配器會導致應用崩潰。
那么有什么方法或庫可用於將viewpager集成到listview中?
這是我的代碼,我正在使用SimpleCursorAdapter進行listview,並使用SimpleCursorAdapter.ViewBinder綁定viewpager適配器。
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>
...
我確實嘗試實現相同的方法,但沒有成功。
我認為該應用程序實現了自定義組件。 它不適用於所有設備,例如:Moto G,三星Galaxy Note II。 這些項目將像列表一樣顯示,並且水平尋呼機將被禁用。
您可以使用AndroidImageSlider庫在Recyclerview中實現每個項目。 您必須創建一個實現onBindViewHolder
的自定義適配器,並在此方法中設置SliderLayout
屬性。 我做到了...並且可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.