Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例
昨天UI妹子給了給需求,展示水平分頁(yè)效果,而且第二頁(yè)要默認(rèn)顯示一部分,提示用戶(hù)水平可以滑動(dòng),先上效果圖:

很明顯橫向滑動(dòng)的分頁(yè),第一反應(yīng)就是使用ViewPager,畢竟只要通過(guò)自定義ViewPager,實(shí)現(xiàn)這個(gè)效果還是很容易,但是實(shí)際中問(wèn)題時(shí),當(dāng)前模塊是Recyclerview中某一個(gè)Holder,為了性能,肯定盡量使用Recyclerview去復(fù)用View,而且ViewPager并不能復(fù)用,所以考慮之后,還是要用Recyclerview去實(shí)現(xiàn)。
解決思路
既然打算用Recyclerview實(shí)現(xiàn),很明顯這就可以用GridLayoutManager處理橫向滑動(dòng)的列表,初步實(shí)現(xiàn)橫向列表的效果,列數(shù)為4的橫向分頁(yè)效果

橫向列表效果是實(shí)現(xiàn)了,但是并沒(méi)有達(dá)到設(shè)計(jì)稿的要求,第二頁(yè)要默認(rèn)顯示一部分,那么就要從水平方向上去思考解決問(wèn)題,既然第二頁(yè)要顯示一部分,假如顯示16dp,那么將第一頁(yè)列表寬度減少右邊距16dp,第二頁(yè)就可以在第一頁(yè)顯示了。
在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方法中,去修改邊距
@Override
public 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的工具類(lèi)
int screenWidth = TCommonUtils.getScreenWidth(context);
if (position <= 3) { //因?yàn)槊苛袛?shù)量為4個(gè),那么只需要將前4個(gè)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());
}```
來(lái)看看效果

可以看到默認(rèn)第二頁(yè)可以顯示一部分,而且后面每一頁(yè)都正常顯示,沒(méi)有像第二頁(yè)一樣侵入上一頁(yè)中
總結(jié)
實(shí)現(xiàn)這種分頁(yè)效果的方法有很多,但是選擇最容易并且效率最高的方式,才是開(kāi)發(fā)中需要的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁(yè)的Gridview實(shí)例源碼
- Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
- Android之ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)ListView分頁(yè)自動(dòng)加載數(shù)據(jù)的方法
- android實(shí)現(xiàn)listview分頁(yè)的方法
- Android實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)效果
- Android提高之SQLite分頁(yè)表格實(shí)現(xiàn)方法
- Android開(kāi)發(fā)中滑動(dòng)分頁(yè)功能實(shí)例詳解
- Android端代碼量非常小的分頁(yè)加載庫(kù)
相關(guān)文章
Android中Xposed框架篇---修改系統(tǒng)位置信息實(shí)現(xiàn)自身隱藏功能實(shí)例
本篇文章介紹了Android中Xposed框架的使用,詳細(xì)的介紹了修改系統(tǒng)位置信息實(shí)現(xiàn)自身隱藏功能實(shí)例,有需要的朋友可以了解一下。2016-11-11
Android開(kāi)發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法,涉及Android編程Wifi的調(diào)用及IP地址的獲取與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android詳細(xì)講解谷歌推出的官方二維碼掃描庫(kù)
Google推出的官方二維碼掃描庫(kù)你知道嗎?還不知道就落伍咯!本篇文字帶你了解google二維碼掃描庫(kù)的詳細(xì)情況與使用,還不知道的朋友快來(lái)看看吧2022-03-03
android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要介紹了android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,自主完成一個(gè)簡(jiǎn)單APP的設(shè)計(jì)工作,綜合應(yīng)用已經(jīng)學(xué)到的Android?UI設(shè)計(jì)技巧,下面來(lái)看看實(shí)驗(yàn)實(shí)現(xiàn)過(guò)程2021-12-12
flutter傳遞值到任意widget(當(dāng)需要widget嵌套使用需要傳遞值的時(shí)候)
這篇文章主要介紹了flutter傳遞值到任意widget(當(dāng)需要widget嵌套使用需要傳遞值的時(shí)候),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07

