项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

示例如下:

/view/recyclerview/RecyclerViewDemo2.java

/**
 * RecyclerView 分隔线
 *
 * 关于本例中使用的自定义的垂直线性布局的分隔线请参见
 */

package com.webabcd.androiddemo.view.recyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.webabcd.androiddemo.R;

public class RecyclerViewDemo2 extends AppCompatActivity {

    private RecyclerView _recyclerView;
    private Button _button1;
    private Button _button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_recyclerview_recyclerviewdemo2);

        _recyclerView = findViewById(R.id.recyclerView1);
        _button1 = findViewById(R.id.button1);
        _button2 = findViewById(R.id.button2);

        sample();
    }

    private void sample() {
        _recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewDemo2.this));
        _recyclerView.setAdapter(new MyRecyclerViewAdapter(MyData.generateDataList()));

        _button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                while (_recyclerView.getItemDecorationCount() > 0) {
                    _recyclerView.removeItemDecorationAt(0);
                }
                // 指定自定义分隔线(指定高度和颜色)
                _recyclerView.addItemDecoration(new MyVerticalLinearLayoutManagerDivider(20, getResources().getColor(R.color.orange)));
            }
        });

        _button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                while (_recyclerView.getItemDecorationCount() > 0) {
                    _recyclerView.removeItemDecorationAt(0);
                }
                // 指定自定义分隔线(指定高度和 Drawable 对象)
                _recyclerView.addItemDecoration(new MyVerticalLinearLayoutManagerDivider(20, getResources().getDrawable(R.drawable.shape_recyclerview_divider)));
            }
        });
    }
}

/layout/activity_view_recyclerview_recyclerviewdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="指定自定义分隔线(指定高度和颜色)"/>

    <Button
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="指定自定义分隔线(指定高度和 Drawable 对象)"/>

    <androidx.recyclerview.widget.RecyclerView
        android:
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

/drawable/shape_recyclerview_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro
    android:shape="rectangle">
    <size android:height="20dp" />
    <solid android:color="#ff00ff00" />
</shape>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

相关文章:

  • 2021-10-12
  • 2021-11-30
  • 2018-05-18
  • 2021-08-16
  • 2021-12-19
  • 2022-01-28
  • 2021-05-25
  • 2021-12-10
猜你喜欢
  • 2022-01-22
  • 2021-06-24
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2022-01-25
相关资源
相似解决方案