当前位置:网站首页>RecyclerView的应用
RecyclerView的应用
2022-07-05 06:20:00 【v_3483608762】
- 简单记录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>
边栏推荐
- WordPress switches the page, and the domain name changes back to the IP address
- MySQL advanced part 2: optimizing SQL steps
- [leetcode] day94 reshape matrix
- Shutter web hardware keyboard monitoring
- 1.15 - input and output system
- MPLS experiment
- 【LeetCode】Day95-有效的数独&矩阵置零
- Leetcode dynamic programming
- Currently clicked button and current mouse coordinates in QT judgment interface
- 【LeetCode】Easy | 20. Valid parentheses
猜你喜欢

Client use of Argo CD installation

博弈论 AcWing 891. Nim游戏

MySQL advanced part 2: the use of indexes

Doing SQL performance optimization is really eye-catching

4. Object mapping Mapster

博弈论 AcWing 894. 拆分-Nim游戏

QQ computer version cancels escape character input expression

Leetcode array operation

Operator priority, one catch, no doubt

There are three kinds of SQL connections: internal connection, external connection and cross connection
随机推荐
Winter messenger 2
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
Basic explanation of typescript
Leetcode-556: the next larger element III
Operator priority, one catch, no doubt
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
C job interview - casting and comparing - C job interview - casting and comparing
Leetcode-6110: number of incremental paths in the grid graph
TCP's understanding of three handshakes and four waves
Quickly use Amazon memorydb and build your own redis memory database
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Liunx starts redis
Sqlmap tutorial (1)
The difference between CPU core and logical processor
Leetcode recursion
3.Oracle-控制文件的管理
求组合数 AcWing 888. 求组合数 IV
1.13 - RISC/CISC
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
How to generate an image from text on fly at runtime