本文主要是介绍Android RecyclerView的使用(二)——添加分割线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android RecyclerView的使用(二)——添加分割线
前言: RecyclerView提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现绚丽的效果。
- 通过布局管理器LayoutManager,可以控制其显示的方式;
- 通过ItemDecoration,可以控制Item间的间隔(可定制);
- 通过ItemAnimator,可以控制Item增删的动画;
- 至于点击、长按事件,RecyclerView并没有给我们提供相应的方法接口,需要我们自己实现。
上一节我们了解了RecyclerView的基本使用,这节我们将介绍如何为RecyclerView 添加分割线。
效果图:
第一步:activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"/></android.support.constraint.ConstraintLayout>
第二步:定义item的样式,item_single_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:background="#442F2F2F"android:layout_height="72dp"><ImageView
android:id="@+id/imageView"android:layout_margin="20dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextView
android:id="@+id/textView"android:layout_gravity="center_vertical"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
第三步:自定义Adapter适配器
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {private LayoutInflater mInflater;private Context mContext;private List<String> mDatas;private int[] imgIds;public MyAdapter(Context context, List<String> datas, int[] imgIds) {this.mContext = context;this.mDatas = datas;this.imgIds = imgIds;mInflater = LayoutInflater.from(context);}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = mInflater.inflate(R.layout.item_single_textview, parent, false);MyViewHolder viewHolder = new MyViewHolder(view);return viewHolder;}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position) {holder.textView.setText(mDatas.get(position));holder.imageView.setBackgroundResource(imgIds[ position % 7]);}@Overridepublic int getItemCount() {return mDatas.size();}
}
class MyViewHolder extends RecyclerView.ViewHolder {ImageView imageView;TextView textView;public MyViewHolder(View itemView) {super(itemView);textView = itemView.findViewById(R.id.textView);imageView = itemView.findViewById(R.id.imageView);}
}
第四步:自定义分割线类(说明,我所使用的分割线类是从网上找的,不是本人实现的)
public class DividerItemDecoration extends RecyclerView.ItemDecoration {public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;private static final int[] ATTRS = new int[]{android.R.attr.listDivider};private Drawable mDivider;private int mOrientation;public DividerItemDecoration(Context context, int orientation) {final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();setOrientation(orientation);}public void setOrientation(int orientation) {if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);if (mOrientation == VERTICAL_LIST) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}public void drawVertical(Canvas c, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getWidth() - parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);RecyclerView v = new RecyclerView(parent.getContext());final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int top = child.getBottom() + params.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawHorizontal(Canvas c, RecyclerView parent) {final int top = parent.getPaddingTop();final int bottom = parent.getHeight() - parent.getPaddingBottom();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);if (mOrientation == VERTICAL_LIST) {outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else {outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);}}}
第五步:MainActivity.java
public class MainActivity extends AppCompatActivity {private RecyclerView recyclerView;private List<String> mDatas;private int[] imgIds;private MyAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initDatas();recyclerView = (RecyclerView) findViewById(R.id.recyclerView);mAdapter = new MyAdapter(this, mDatas, imgIds);recyclerView.setAdapter(mAdapter);//设置RecyclerView的布局管理LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false);recyclerView.setLayoutManager(manager);//设置RecyclerView的item间分割线recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));}private void initDatas() {imgIds = new int[]{R.mipmap.pic01, R.mipmap.pic02, R.mipmap.pic03, R.mipmap.pic04,R.mipmap.pic05, R.mipmap.pic06, R.mipmap.pic07};mDatas = new ArrayList<>();mDatas.add("亳州"); // 亳[bó]属于不常见的二级汉字mDatas.add("大娃");mDatas.add("二娃");mDatas.add("三娃");mDatas.add("四娃");mDatas.add("五娃");mDatas.add("六娃");mDatas.add("七娃");mDatas.add("喜羊羊");mDatas.add("美羊羊");mDatas.add("懒羊羊");mDatas.add("沸羊羊");mDatas.add("暖羊羊");mDatas.add("慢羊羊");mDatas.add("灰太狼");mDatas.add("红太狼");mDatas.add("孙悟空");mDatas.add("黑猫警长");mDatas.add("舒克");mDatas.add("贝塔");mDatas.add("海尔");mDatas.add("阿凡提");mDatas.add("邋遢大王");mDatas.add("哪吒");mDatas.add("没头脑");mDatas.add("不高兴");mDatas.add("蓝皮鼠");mDatas.add("大脸猫");mDatas.add("大头儿子");mDatas.add("小头爸爸");mDatas.add("蓝猫");mDatas.add("淘气");mDatas.add("叶峰");mDatas.add("楚天歌");mDatas.add("江流儿");mDatas.add("Tom");mDatas.add("Jerry");mDatas.add("12345");mDatas.add("54321");mDatas.add("_(:з」∠)_");mDatas.add("……%¥#¥%#");}
}
拓展: 同样,我们也可以自定义分割线的样式。
在drawable目录下自定义样式divider_item.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><size android:height="2dp"/><gradient android:startColor="#00FF00"android:centerColor="#FFFF00"android:endColor="#FF0000"android:type="linear"/></shape>
在values目录的styles.xml文件中引用自定义的分割线样式
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item><item name="android:listDivider">@drawable/divider_item</item></style></resources>
item name=”android:listDivider”>@drawable/divider_item
就这样我们完成了RecyclerView分割线的定义,我们可以自定义我们分割线的样式,让分割线更加绚丽多彩。
这篇关于Android RecyclerView的使用(二)——添加分割线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!