安卓——RecyclerView使用(最新版)面向小白,要多白就有多白

下面是自定义RecycleView的用法
主要思想来自郭神的书,但是因为当时的AS版本在2.2左右,AS的添加依赖的方法还是用compile,并且书里面用的SDK是24api的,所以现在给郭神更新一下下。

声明:这是我结合书上的介绍所产生的自己的想法,不对的地方可以评论指出

对象
0、在app/build.gradle文件里,在dependencies中添加依赖

 dependencies{
 implementation 'com.android.support:recyclerview-v7:28.0.0'//这里面的28,要与上面ompileSdkVersion 28 的后面数字相同
 //这是新版本添加依赖的方法
 }

添加完成后点击sync now让软件下载依赖

在主活动的layout中父布局为LinearLayout,长宽均匹配父布局,之后将RecyclerView嵌入LinerLayout中,这里布局大小匹配问题以后再说

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

    <android.support.v7.app.AlertController.RecycleListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyceler"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.app.AlertController.RecycleListView>
</LinearLayout>

成功添加依赖后,在布局中加入会是这个样子
添加依赖成功后,在布局中加载的样子
1、实体类
首先我们创建一个实体类,这个类是给RecycleView的适配器(Adapter)适配的,其实质上类似JavaBean,有成员变量,用来赋值成员变量的构造器,还有用来返回已经赋值成员变量的get方法;
这是我做的一个带头像,带名字的实体类

public class Tx {
    private String name;
    private int imageId;//两个成员变量

    public Tx(String name, int imageId) {//构造方法,用以赋值
        this.name = name;
        this.imageId = imageId;//赋予变量值
    }

    public String getName() {//获得Name的值
        return name;
    }

    public int getImageId() {//用以获得图片ID的值
        return imageId;
    }
}

2、每个子项的Layout
接下来就是每个子项的布局,这里是最可以脑洞大开的地方,在文章末尾有我做的一点小改,也会附上代码
可以按照正常布局文件来编写,最好将根布局的长宽设置为包含内容即可
我的代码如下

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
        //图像
            android:id="@+id/tx_image"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginBottom="10dp"
            android:background="@color/colorAccent"
            android:scaleType="fitCenter" />

        <TextView
        //名字
            android:id="@+id/tx_name"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="ceshixinxi"
            android:textAlignment="center"
            android:textSize="30sp" />
  
    </LinearLayout>

这里大家可以做各种微调,原理同HTML的盒子模型,安卓也具有Margin和Padding属性
效果图如下
效果图
3、适配器
接下来就是最难的地方了
创建用以适配RecycleView的适配器
让代码飞一会~~~~~~~~~~~~

public class TxAdapter extends RecyclerView.Adapter<TxAdapter.ViewHolder> {//该适配器继承字RecyclerView的Adapter适配器,因其具有可以指定
    private List<Tx> mTxList;//用以将适配完的子项储存的链表,它的泛型是之前的实体类

    static class ViewHolder extends RecyclerView.ViewHolder {
    //内部静态类,用以定义TxApter.View的泛型
        ImageView txImage;
        TextView txName;//这两个是在子项布局里面具体的控件
        View txView;//这个是用于整个子项的控制的控件

        public ViewHolder(View view) {
            super(view);
            txView = view;//这个是整个子项的控件
            txImage = view.findViewById(R.id.tx_image);
            txName = view.findViewById(R.id.tx_name);//通过R文件的id查找,找出子项的具体控件
        }
    }

    public TxAdapter(List<Tx> txList) {
    //链表的赋值
        mTxList = txList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //ViewHode方法,我的理解就是对某个具体子项的操作,包括对具体控件的设置,包括且不限于的点击动作两个参数
    A:ViewGroup parent主要用于调用其整个RecyclerView的上下文
    B:第二个参数因为在方法里面没有调用,所以我也没看懂,从字面上看,这个参数是一个整型的控件类型???
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tx_item, parent, false);
        //将子项的布局通过LayoutInflater引入
        final ViewHolder holder = new ViewHolder(view);
        holder.txView.setOnClickListener(new View.OnClickListener() {
        //这里是子项的点击事件,RecyclerView的特点就是可以对子项里面单个控件注册监听,这也是为什么RecyclerView要摒弃ListView的setOnItemClickListener方法的原因
            @Override
            public void onClick(View v) {
                Tx tx = mTxList.get(holder.getAdapterPosition());
                Toast.makeText(v.getContext(), "已经删除!", Toast.LENGTH_LONG).show();
                mTxList.remove(tx);//所谓的删除就是将子项从链表中remove
            }
        });
        return holder;返回一个holder对象,给下个方法使用
    }

    @Override
    
    public void onBindViewHolder(ViewHolder holder, int position) {
    //用以将滚入屏幕的子项加载图片等的方法,两个参数
    A:前面方法ViewHolder的对象;
    B:子项的id
        Tx tx = mTxList.get(position);//创建前面实体类的对象
        holder.txImage.setImageResource(tx.getImageId());
        holder.txName.setText(tx.getName());//将具体值赋与子项对应的控件
    }

    @Override
    public int getItemCount() {
    //用以返回RecyclerView的总共长度,这里直接使用了链表的长度(size)
        return mTxList.size();
    }
}

基本上这个适配器的所有我都注释上了
biu特佛

6、主类
需要在Drwable中,提前储存上图片
添加的图片
那么
去吧!代码!

public class MainActivity extends AppCompatActivity {
    private List<Tx> txList = new ArrayList<>();//一个全局的链表


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//书的前面活动的生存周期里面讲过了
        setContentView(R.layout.activity_main);//设置布局
        initTxs();//下面的初始化方法
        RecyclerView recyclerView = findViewById(R.id.recycler_view);//找到RecyclerView控件
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);//布局管理器
        recyclerView.setLayoutManager(layoutManager);
        TxAdapter adapter = new TxAdapter(txList);//适配器对象
        recyclerView.setAdapter(adapter);//设置适配器为上面的对象
    }

    private void initTxs() {
    //初始化方法,为了能够创造出具体的子项
        for (int x = 0; x < 2; x++) {
        //嫌少?多来几次循环
            Tx a = new Tx("a", R.drawable.image_1);
            txList.add(a);//加入到链表
            Tx b = new Tx("b", R.drawable.image_2);
            txList.add(b);
            Tx c = new Tx("c", R.drawable.image_3);
            txList.add(c);
            Tx d = new Tx("d", R.drawable.image_4);
            txList.add(d);
            Tx e = new Tx("e", R.drawable.image_5);
            txList.add(e);
            Tx f = new Tx("f", R.drawable.image_6);
            txList.add(f);
            Tx g = new Tx("g", R.drawable.image_7);
            txList.add(g);
            Tx h = new Tx("h", R.drawable.image_8);
            txList.add(h);
            Tx i = new Tx("i", R.drawable.image_9);
            txList.add(i);
            Tx j = new Tx("j", R.drawable.image_10);
            txList.add(j);
            Tx k = new Tx("k", R.drawable.image_11);
            txList.add(k);
            Tx l = new Tx("l", R.drawable.image_12);
            txList.add(l);
            Tx m = new Tx("m", R.drawable.image_13);
            txList.add(m);
            Tx n = new Tx("n", R.drawable.image_14);
            txList.add(n);
            Tx o = new Tx("o", R.drawable.image_15);
            txList.add(o);
            Tx p = new Tx("p", R.drawable.image_16);
            txList.add(p);
            Tx q = new Tx("q", R.drawable.image_17);
            txList.add(q);
            Tx r = new Tx("r", R.drawable.image_18);
            txList.add(r);
            Tx s = new Tx("s", R.drawable.image_19);
            txList.add(s);
            Tx t = new Tx("t", R.drawable.image_1);
            txList.add(t);
        }
    }
}

哦吼
最后下面就是给你们看看效果图

在这里插入图片描述
这是前面说的可以脑洞大开的地方在这里
啊哈
最后的代码飞一会~~~~~~~~~~

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/im_tx"
                        android:layout_width="160dp"
                        android:layout_height="160dp"
                        android:background="@drawable/image_1" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/tx_name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="20dp"
                            android:text="森林里的麋鹿"
                            android:textColor="#282626"
                            android:textSize="30sp" />

                        <TextView
                            android:id="@+id/tx_gq"
                            android:layout_width="190dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/tx_name"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="30sp"
                            android:text="回到最初相见的地方,才知原来你在我心里已刻下一道深深的伤痕。" />
                    </RelativeLayout>

                    <Button
                        android:id="@+id/bt1"
                        android:layout_width="160dp"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="25dp"
                        android:background="@drawable/btn"
                        android:text="删除"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt2"
                        android:layout_width="160dp"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="25dp"
                        android:background="@drawable/btn"
                        android:text="修改"
                        android:textSize="30sp" />
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">

            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/im_tx2"
                        android:layout_width="160dp"
                        android:layout_height="160dp"
                        android:background="@drawable/image_9" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/tx_name2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="20dp"
                            android:text="花淡纱窗残月明"
                            android:textColor="#282626"
                            android:textSize="30sp" />

                        <TextView
                            android:id="@+id/tx_gq2"
                            android:layout_width="190dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/tx_name2"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="30sp"
                            android:text="我习惯孤独,并不是我喜欢孤独。" />
                    </RelativeLayout>

                    <ScrollView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:scrollbars="none">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <Button
                                android:id="@+id/bt3"
                                android:layout_width="160dp"
                                android:layout_height="160dp"
                                android:layout_marginLeft="25dp"
                                android:background="@drawable/btn"
                                android:text="删除"
                                android:textSize="30sp" />

                            <Button
                                android:id="@+id/bt4"
                                android:layout_width="160dp"
                                android:layout_height="160dp"
                                android:layout_marginLeft="25dp"
                                android:layout_marginTop="20dp"
                                android:background="@drawable/btn"
                                android:text="修改"
                                android:textSize="30sp" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

我挥舞着键盘和本子,发誓要把世界写个明明白白。
|

(斌赋在此)

发布了13 篇原创文章 · 获赞 8 · 访问量 1108

猜你喜欢

转载自blog.csdn.net/qq_45000228/article/details/100655917