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

[学习笔记]Android RecyclerView

程序员文章站 2022-06-14 11:18:31
...
   以下内容纯粹为本人学习笔记【记录】之用,所听课程(Q群群友百度网盘提供)为极客学院一位老师所讲(老师大名我尚未知晓),如有侵权请告知。在此特别感谢这位老师录制的视频资料。

recycle v.回收利用、使再循环 recycler n.反复循环器 view n.视图,看法,领域、风景
wrap v.包、缠绕、用…包裹(覆盖);n.围巾、外套

三者区别
fill_parent
将强制性地使构件扩展,以填充布局单元内尽可能多的空间;
match_parent
Android2.2开始fill_parent更名为match_parent,API Level 8可直接使用match_parent代替fill_parent,不过2.2后的版本仍可使用fill_parent,两者含义是一样的,都是填满整个父窗口;
wrap_content 布局元素将根据内容更改大小。

RecyclerView是android-support-v7里一个新的列表控件,用来替代传统的ListView,能在有限的窗口展现大量的数据集。
首先得添加v7-RecyclerView依赖
[学习笔记]Android RecyclerView
1、使用RecyclerView
MainActivity.java

package com.keen.learnrecyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;//声明

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rv = new RecyclerView(this);//初始化
        setContentView(rv);//将rv作为Activity的内容布局
        rv.setLayoutManager(new LinearLayoutManager(this));//设置布局

        //为布局填充内容
        rv.setAdapter(new RecyclerView.Adapter(){

            //创建类
            class ViewHolder extends RecyclerView.ViewHolder {

                private TextView tv;//绑定子对象视图
                public ViewHolder(TextView itemView) {
                    super(itemView);

                    tv = itemView;
                }

                public TextView getTv() {
                    return tv;
                }
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//                return null;
              return new ViewHolder(new TextView(viewGroup.getContext()));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
                //对TextView进行赋值
                ViewHolder vh = (ViewHolder) viewHolder;
//                vh.getTv().setText("Item" + i);//i为索引
                vh.getTv().setText(data[i]);

            }

            @Override
            public int getItemCount() {//获取子对象的数量
//                return 10;//10代表子对象数量
                return data.length;
            }

            private String[] data = new String[]{"陈大哥", "陈二哥", "陈三哥"};
        });

    }
}

AndroidManifest.xml配置文件如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.keen.learnrecyclerview">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data  tools:replace="android:value" />
        </activity>
    </application>
</manifest>

但一直未运行成功,提示错误:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed with multiple errors, see logs

目前暂无找到合适的方法解决

2、使用资源文件自定义列表项
在程序里写[界面],有时修改时是很麻烦的。so,要学会用资源文件来配置。
创建资源文件
[学习笔记]Android RecyclerView
[学习笔记]Android RecyclerView
[学习笔记]Android RecyclerView
这样,通过重构将 全自动地将匿名类提取成一个内部类。如下:

private class MyAdapter extends RecyclerView.Adapter {

        //创建类
        class ViewHolder extends RecyclerView.ViewHolder {

            private TextView tv;//绑定子对象视图
            public ViewHolder(TextView itemView) {
                super(itemView);

                tv = itemView;
            }

            public TextView getTv() {
                return tv;
            }
        }

还可继续移动到一个单独的文件里,选择上面其中一处,右键/Refactor/Move/
[学习笔记]Android RecyclerView
MyAdapter.java

package com.keen.learnrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

class MyAdapter extends RecyclerView.Adapter {

    //创建类
    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv;//绑定子对象视图

        public ViewHolder(TextView itemView) {
            super(itemView);

            tv = itemView;
        }

        public TextView getTv() {
            return tv;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//                return null;
        return new ViewHolder(new TextView(viewGroup.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        //对TextView进行赋值
        ViewHolder vh = (ViewHolder) viewHolder;
//                vh.getTv().setText("Item" + i);//i为索引
        vh.getTv().setText(data[i]);

    }

    @Override
    public int getItemCount() {//获取子对象的数量
//                return 10;//10代表子对象数量
        return data.length;
    }

    private String[] data = new String[]{"陈大哥", "陈二哥", "陈三哥"};
}

修改原有代码

package com.keen.learnrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

class MyAdapter extends RecyclerView.Adapter {

    //创建类
    class ViewHolder extends RecyclerView.ViewHolder {

        private View root;//绑定子对象视图
        private TextView tvTitle,tvContent;

        public ViewHolder(View root) {
            super(root);
            tvTitle = (TextView) root.findViewById(R.id.tvTitle);
            tvContent = (TextView) root.findViewById(R.id.tvContent);
        }

        public TextView getTvTitle() {
            return tvTitle;
        }
        public TextView getTvContent() {
            return tvContent;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.id.layout.list_cell, null));//LayoutInflater是一个布局解释器
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        //对TextView进行赋值
        ViewHolder vh = (ViewHolder) viewHolder;
//                vh.getTv().setText("Item" + i);//i为索引
//        vh.getTv().setText(data[i]);

        CellData cd = data[i];
        vh.getTvTitle().setText(cd.title);
        vh.getTvContent().setText(cd.content);


    }

    @Override
    public int getItemCount() {//获取子对象的数量
//                return 10;//10代表子对象数量
        return data.length;
    }

    private CellData data = new CellData[]{
            new CellData("清承堂", "龙井茶"), new CellData("这茶", "真好喝")};

//    private String[] data = new String[]{"陈大哥", "陈二哥", "陈三哥"};
}

CellData.java

package com.keen.learnrecyclerview;

public class CellData {

    public CellData(String title, String content) {
        this.title =title;
        this.content =content;
    }

    public String title = "title";
    public String content = "content";
}

list_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvTitle" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvContent" />
</LinearLayout>

3、RecyclerView的布局样式
adapt v.适应于、改编、改写;adapter n.适配器、改编者
scrap n.废料、残余物、小片、吵架;v.报废、废弃、取消、抛弃 scrapped v.废弃,打架 a.废弃的
attached v.附加的、附属的、依恋的;v.贴上、系上

RecyclerView自带3种布局:线性布局、网格布局、交错网络布局

参考资料:
1)RecyclerView源码解析http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0307/4032.html
2)RecyclerView技术栈http://www.jianshu.com/p/16712681731e
3)这是一篇Android RecyclerView使用介绍哦http://www.jianshu.com/p/12ec590f6c76#