前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android仿TIM、QQ的好友列表的实现(recycleview实现)

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

作者头像
祝你万事顺利
发布2019-05-28 23:16:20
2.4K0
发布2019-05-28 23:16:20
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

效果展示 :

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档