当前位置:网站首页>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>
边栏推荐
- Leetcode-22: bracket generation
- C - XOR to all (binary topic)
- 【LeetCode】Day95-有效的数独&矩阵置零
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- JS quickly converts JSON data into URL parameters
- 20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- MySQL advanced part 1: index
- SPI details
- 求组合数 AcWing 887. 求组合数 III
猜你喜欢
SPI details
博弈论 AcWing 893. 集合-Nim游戏
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
MySQL advanced part 1: index
Is it impossible for lamda to wake up?
MySQL advanced part 1: View
P2575 master fight
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
Real time clock (RTC)
[moviepy] unable to find a solution for exe
随机推荐
SQLMAP使用教程(一)
【LeetCode】Easy | 20. Valid parentheses
P3265 [jloi2015] equipment purchase
MySQL advanced part 1: View
Chapter 6 relational database theory
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
[wustctf2020] plain_ WP
MySQL advanced part 2: the use of indexes
Leetcode-22: bracket generation
C Primer Plus Chapter 15 (bit operation)
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
Appium automation test foundation - Summary of appium test environment construction
927. Trisection simulation
[rust notes] 15 string and text (Part 1)
C job interview - casting and comparing - C job interview - casting and comparing
Leetcode recursion
Data visualization chart summary (II)
Liunx starts redis
Multi screen computer screenshots will cut off multiple screens, not only the current screen
1.15 - input and output system