国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Recyclerview實現(xiàn)水平分頁GridView效果示例

2019-12-12 02:17:39
字體:
供稿:網(wǎng)友

昨天UI妹子給了給需求,展示水平分頁效果,而且第二頁要默認(rèn)顯示一部分,提示用戶水平可以滑動,先上效果圖:

很明顯橫向滑動的分頁,第一反應(yīng)就是使用ViewPager,畢竟只要通過自定義ViewPager,實現(xiàn)這個效果還是很容易,但是實際中問題時,當(dāng)前模塊是Recyclerview中某一個Holder,為了性能,肯定盡量使用Recyclerview去復(fù)用View,而且ViewPager并不能復(fù)用,所以考慮之后,還是要用Recyclerview去實現(xiàn)。

解決思路

既然打算用Recyclerview實現(xiàn),很明顯這就可以用GridLayoutManager處理橫向滑動的列表,初步實現(xiàn)橫向列表的效果,列數(shù)為4的橫向分頁效果

橫向列表效果是實現(xiàn)了,但是并沒有達(dá)到設(shè)計稿的要求,第二頁要默認(rèn)顯示一部分,那么就要從水平方向上去思考解決問題,既然第二頁要顯示一部分,假如顯示16dp,那么將第一頁列表寬度減少右邊距16dp,第二頁就可以在第一頁顯示了。
在Recyclerview的Adapter中,先上布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/rl_parent"  android:layout_width="match_parent"  android:layout_height="55dp"  android:background="@drawable/news_click_bg"  android:clickable="true"  android:gravity="center_vertical">  <ImageView    android:id="@+id/iv_img"    android:layout_width="48dp"    android:layout_height="48dp"    android:layout_centerVertical="true"     android:layout_marginLeft="16dp"    android:padding="3dp"    android:src="@drawable/icon_book_default"    android:tint="@color/blue" />  <com.ddz.lifestyle.baseview.customview.RobotoTextView    android:id="@+id/tv_title"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:layout_marginLeft="15dp"    android:layout_marginRight="20dp"    android:layout_toRightOf="@+id/iv_img"    android:ellipsize="end"    android:lines="1"    android:textSize="18sp"    app:typeface="roboto_regular"    tools:text="name" />  <ImageView    android:id="@+id/iv_menu"    android:layout_width="34dp"    android:layout_height="34dp"    android:layout_alignParentRight="true"    android:layout_centerVertical="true"    android:layout_marginRight="10dp"    android:padding="10dp"    android:src="@drawable/menu_right"    android:visibility="invisible" /></RelativeLayout>```

在onBindViewHolder方法中,去修改邊距

@Overridepublic void onBindViewHolder(ItemHolder holder, int position) {  if (null == bean) {    return;  }  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(86));   //DensityUtil是px轉(zhuǎn)dp的工具類  int screenWidth = TCommonUtils.getScreenWidth(context);  if (position <= 3) { //因為每列數(shù)量為4個,那么只需要將前4個item的寬度減少32dp    screenWidth -= DensityUtil.dip2px(32); //寬度減少32dp,即左右各16dp    params.width = screenWidth;  } else {    params.width = screenWidth;  }  holder.rlParent.setLayoutParams(params);  holder.tvTitle.setText(bean.get(position).getTitle());}```

來看看效果

可以看到默認(rèn)第二頁可以顯示一部分,而且后面每一頁都正常顯示,沒有像第二頁一樣侵入上一頁中

總結(jié)

實現(xiàn)這種分頁效果的方法有很多,但是選擇最容易并且效率最高的方式,才是開發(fā)中需要的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌审旗| 凤翔县| 广安市| 栾川县| 永清县| 龙江县| 疏勒县| 阿坝| 巴林右旗| 方城县| 教育| 巴里| 东明县| 东兰县| 象州县| 青岛市| 阜阳市| 勃利县| 保德县| 诸暨市| 乌兰察布市| 普陀区| 黔西县| 丰原市| 通州市| 龙里县| 治多县| 法库县| 沁水县| 乌什县| 巴林左旗| 保山市| 石阡县| 商河县| 昌邑市| 井陉县| 四川省| 石河子市| 繁昌县| 南城县| 来凤县|