欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-01-21 10:52:23|栏目:Android代码|点击:

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开发实现的简单媒体播放器功能示例

栏    目:Android代码

下一篇:Android巧用Fragment解耦onActivityResult详解

本文标题:Android RecyclerView实现水平、垂直方向分割线

本文地址:http://www.codeinn.net/misctech/48707.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有