RecyclerView的应用

  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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值