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

【RecycleView加分割线】Android RecycleView列表布局添加分割线如此简单

程序员文章站 2022-06-01 18:22:56
...

效果图

【RecycleView加分割线】Android RecycleView列表布局添加分割线如此简单

简单起见我还是给出文件的下载地址自己下载一下
文件地址

配置

拷贝类到项目后有一处报红那就是还需要添加分割线的样式文件名字自己取然后把类里的样式名改成你的就O啦

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <!-- 显示虚线,破折线的宽度为dashWith,空隙的宽度为dashGap, darkgray -->
    <stroke
        android:width="1px"
        android:color="@color/common_divider_wide"
        android:dashGap="5dp"
        android:dashWidth="5dp" />
    <size android:height="1dp" />
</shape>

使用案例

结合列子体会一下用法吧

主布局

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycleview"
                android:foregroundGravity="center"
                android:layerType="software"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>

主页面

package com.daqsoft.baseproject;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.daqsoft.baseproject.adapter.recyclerview.CommonAdapter;
import com.daqsoft.baseproject.adapter.recyclerview.base.ViewHolder;
import com.daqsoft.baseproject.base.BaseActivity;
import com.daqsoft.baseproject.bean.Base;
import com.daqsoft.baseproject.view.recycleview.SupportDividerItemDecoration;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    @BindView(R.id.recycleview)
    RecyclerView mRecycleview;
    private List<Base> mDatas = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public int getTitleId() {
        return R.string.mainPageTitle;
    }

    @Override
    public int getModeId() {
        return MODE_BACK_NAVIGATION;
    }

    @Override
    public void initDatas() {
        mDatas.clear();
        mDatas.add(new Base("视屏",R.mipmap.ic_launcher));
        mDatas.add(new Base("视屏1",R.mipmap.ic_launcher));
        mDatas.add(new Base("视屏2",R.mipmap.ic_launcher));
        mDatas.add(new Base("视屏3",R.mipmap.ic_launcher));
    }

    @Override
    public void configViews() {
        mRecycleview.setHasFixedSize(true);
        mRecycleview.setLayoutManager(new LinearLayoutManager(this));
        mRecycleview.addItemDecoration(new SupportDividerItemDecoration(this, LinearLayoutManager.VERTICAL, true));
        mRecycleview.setAdapter(new CommonAdapter<Base>(this,R.layout.item_find,mDatas) {
            @Override
            protected void convert(ViewHolder holder, Base base, int position) {
                holder.setText(R.id.tvTitle,base.getName());
                holder.setImageResource(R.id.ivIcon,base.getIconResId());
            }
        });
    }


}

item_find布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="25dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="25dp">


    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/ivIcon"
        android:text="排行榜"
        android:textColor="@color/white"
        android:textSize="18sp" />

</RelativeLayout>
相关标签: recyclerview使用