欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android RecyclerView实现水平、垂直方向分割线

程序员文章站 2022-04-13 22:46:29
android recyclerview不像过去的listview那样随意的设置水平方向的分割线,如果要实现recyclerview的水平/垂直分割线,则需要继承自recy...

android recyclerview不像过去的listview那样随意的设置水平方向的分割线,如果要实现recyclerview的水平/垂直分割线,则需要继承自recyclerview.itemdecoration重写getitemoffsets方法,从而增加水平/垂直分割线。

写一个例子。

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;
    }
  }
}

布局文件,很简单,就放一个recyclerview,注意背景颜色的设置:

<?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>

最关键的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增加间距
 * 预设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在整个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;
    }
  }
}

代码运行结果:

Android RecyclerView实现水平、垂直方向分割线

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。