Android仿TIM、QQ的好友列表的实现(recycleview实现)

效果展示 :

11173460-4646e7bcd13df583.gif
recycleview.gif

整体思路:
RecyclerView 是一个增强版的ListView,不仅可以实现和ListView同样的效果,还优化了ListView中存在的各种不足之处。
这里使用recycleviewAdapter的BRAVH框架中的树状列表,在adapter中添加两个样式,一个是分组的样式,一个是好友信息的样式。

1.引入依赖
这里要引入三个依赖:一个是recycleview的依赖,一个是adapter(BRAVH)的依赖,一个是圆形样式用于让头像显示为圆形。
先在 build.gradle(Project:XXXX) 的 repositories 添加:

 allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
            maven { url "http://lib.gcssloop.com/repository/gcssloop-central/" }
        }
    }

然后在 build.gradle(Module:app) 的 dependencies 添加:

 dependencies {
            compile 'com.android.support:recyclerview-v7:26.1.0'
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
            compile 'com.gcssloop.widget:rclayout:1.4.1@aar'
    }

2.布局
在主界面中添加recycleView的布局

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>

新建两个layout样式分别为好友列表的样式和好友信息的样式,在树状列表中引用这两个样式。

这里列出"好友信息"的样式,好友样式中使用圆角布局让好友头像展示为圆形。这个布局中也可以添加文字。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:background="#ffffff"
    android:layout_marginBottom="2dp"
    >
    <com.gcssloop.widget.RCRelativeLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:round_corner="10dp"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/Lv1_iv"
            android:scaleType="centerCrop"
            />
    </com.gcssloop.widget.RCRelativeLayout>
    <TextView
        android:id="@+id/title_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</LinearLayout>
public class Levelone implements MultiItemEntity {

    public String friendName;
    public int friendSculpture;

    public Levelone(String friendName, int friendSculpture) {
        this.friendName = friendName;
        this.friendSculpture = friendSculpture;
    }

    @Override
    public int getItemType() {
        return Adapter.TYPE_LEVEL_1;
    }
}

3.Adapter
Adapter采用BRAVH
https://www.jianshu.com/p/b343fcff51b0
这个adapter里面可以改变item加载的动画,一些单击事件,如果要实现点击好友头像进入好友信息界面,就可以在adapter中添加点击事件。如果要显示好友头像为网络图片可以在adapter中使用Glide加载自己服务器中的图片url。

public class Adapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;

    public Adapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.levelzero);
        addItemType(TYPE_LEVEL_1, R.layout.levelone);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case 0:
                final Levelzero lv0 = (Levelzero) item;
                helper.setText(R.id.Lv0_tv, lv0.friendGroup);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case 1:
                final Levelone lv1 = (Levelone) item;
                helper.setImageResource(R.id.Lv1_iv, lv1.friendSculpture);
                helper.setText(R.id.title_one, lv1.friendName);
                break;
        }
    }
}

4.activity中绑定adapter

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mrecycleView = findViewById(R.id.rv);
        addData();
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        mrecycleView.setAdapter(new Adapter(res));
        mrecycleView.setLayoutManager(manager);
    }

最后附上github
https://github.com/yrp666/recycleViewdemo

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值