Android RecyclerView實(shí)現(xiàn)水平、垂直方向分割線
android RecyclerView不像過去的ListView那樣隨意的設(shè)置水平方向的分割線,如果要實(shí)現(xiàn)RecyclerView的水平/垂直分割線,則需要繼承自RecyclerView.ItemDecoration重寫getItemOffsets方法,從而增加水平/垂直分割線。
寫一個(gè)例子。
MainActivity.Java:
package zhangphil.app; import android.content.Context; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); // 兩列 int spanCount = 2; // StaggeredGridLayoutManager管理RecyclerView的布局。 StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); //為RecyclerView增加分割線,水平和垂直方向都有。增加分割線值比如為32。 RecyclerViewItemDecoration decoration = new RecyclerViewItemDecoration(32); mRecyclerView.addItemDecoration(decoration); RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(this); mRecyclerView.setAdapter(mAdapter); } private class ItemViewHolder extends RecyclerView.ViewHolder { private TextView text; public ItemViewHolder(View itemView) { super(itemView); text = (TextView) itemView.findViewById(android.R.id.text1); text.setTextColor(Color.WHITE); } } public class RecyclerViewAdapter extends RecyclerView.Adapter<ItemViewHolder> { private Context context; public RecyclerViewAdapter(Context context) { super(); this.context = context; } @Override public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View view = View.inflate(context, android.R.layout.simple_list_item_1, null); view.setBackgroundColor(Color.RED); ItemViewHolder holder = new ItemViewHolder(view); return holder; } @Override public void onBindViewHolder(ItemViewHolder viewHolder, int pos) { viewHolder.text.setText(String.valueOf(pos)); } @Override public int getItemCount() { return 15; } } }
布局文件,很簡單,就放一個(gè)RecyclerView,注意背景顏色的設(shè)置:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
最關(guān)鍵的RecyclerViewItemDecoration.java:
package zhangphil.app; /** * Created by Phil on 2016/10/8. */ import android.graphics.Rect; import android.support.v7.widget.RecyclerView; import android.view.View; /** * 為RecyclerView增加間距 * 預(yù)設(shè)2列,如果是3列,則左右值不同 */ public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { private int space = 0; private int pos; public RecyclerViewItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.top = space; //該View在整個(gè)RecyclerView中位置。 pos = parent.getChildAdapterPosition(view); //取模 //兩列的左邊一列 if (pos % 2 == 0) { outRect.left = space; outRect.right = space / 2; } //兩列的右邊一列 if (pos % 2 == 1) { outRect.left = space / 2; outRect.right = space; } } }
代碼運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)自定義標(biāo)題欄的方法
這篇文章主要介紹了Android實(shí)現(xiàn)自定義標(biāo)題欄的方法,需要的朋友可以參考下2015-12-125分鐘快速實(shí)現(xiàn)Android爆炸破碎酷炫動(dòng)畫特效的示例
本篇文章主要介紹了5分鐘快速實(shí)現(xiàn)Android爆炸破碎酷炫動(dòng)效的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Android HTTP網(wǎng)絡(luò)請求的異步實(shí)現(xiàn)
這篇文章主要介紹了Android HTTP網(wǎng)絡(luò)請求的異步實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)用戶登錄記住密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)用戶登錄記住密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法,文章內(nèi)容介紹了如何讀取剛收到的短信的相關(guān)內(nèi)容,以及Android獲取短信驗(yàn)證碼的方法,感興趣的小伙伴們可以參考一下2016-03-03Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例
本篇文章主要介紹了Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03