您好,登錄后才能下訂單哦!
Android 中怎么利用Recyclerview實(shí)現(xiàn)水平分頁(yè),相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wè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的工具類 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)看看效果
看完上述內(nèi)容,你們掌握Android 中怎么利用Recyclerview實(shí)現(xiàn)水平分頁(yè)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。