当前位置:网站首页>RecyclerView的应用

RecyclerView的应用

2022-07-05 06:20:00 v_3483608762

  1. 简单记录recyclerView

Adapter有些绕。
viewholder-----连接-------Adapter---------oncreate(里面的setAdapter)。

Person类作为存储数据在Adapter里面要将数据传给viewholder中的各个组件里。

主activity

package com.example.alertdialog;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    
    private String[] strings = {
    "xxxx", "wangwnagwnag", "Dddddd"};
    private String[] decs = {
    "aaaa", "aabbbb", "aabbc"};
    private List<Person> datalist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.datalist = datalist;
        for (int i = 0; i < strings.length; i++) {
    
            this.datalist.add(new Person(strings[i],decs[i]));
        }
        RecyclerView view= findViewById(R.id.re_view);
        view.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this );
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        view.setLayoutManager(linearLayoutManager);
        Adapter adapter = new Adapter();
        view.setOnClickListener(view1 -> {
    
            String x = String.valueOf(Math.random()*10);
            Person person = new Person(x, x + "");
              adapter.notifyItemInserted(3);
            datalist.add(3,person);
            adapter.notifyItemRangeChanged(3,adapter.getItemCount());
        });
        view.setAdapter(adapter);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
    
        private final RecyclerView.Adapter adapter;
        TextView v1;
        TextView v2;
        public ViewHolder(@NonNull View itemView,RecyclerView.Adapter adapter) {
    
            super(itemView);
            v1 = itemView.findViewById(R.id.tv1);
            v2 = itemView.findViewById(R.id.tv2);
            this.adapter=adapter;
        }


    }

    private class Adapter extends RecyclerView.Adapter<ViewHolder>{
    

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
            return new ViewHolder(view,this);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.v1.setText((CharSequence) datalist.get(position).v1);
            holder.v2.setText((CharSequence) datalist.get(position).v2);
        }


        @Override
        public int getItemCount() {
    
            return datalist.size();
        }
    }


}

class Person类

package com.example.alertdialog;

import android.widget.TextView;

public class Person {
    
    String v1;
    String  v2;

    public Person(String string, String dec) {
    
           v1=string;
           v2=dec;
    }
}

数据一项一项的排列方法

<?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">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000" />

</LinearLayout>

主排列

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

    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/re_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
原网站

版权声明
本文为[v_3483608762]所创,转载请带上原文链接,感谢
https://blog.csdn.net/v_3483608762/article/details/125247424