当前位置:网站首页>Application of recyclerview
Application of recyclerview
2022-07-05 06:27:00 【v_ three billion four hundred and eighty-three million six hund】
- Simple record recyclerView
Adapter Some winding .
viewholder----- Connect -------Adapter---------oncreate( Inside setAdapter).
Person Class as stored data in Adapter It needs to transmit data to viewholder In each component of .
Lord 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 class
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;
}
}
The arrangement of data item by item
<?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>
Main arrangement
<?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>
边栏推荐
- Bit of MySQL_ OR、BIT_ Count function
- [wustctf2020] plain_ WP
- 栈 AcWing 3302. 表达式求值
- Leetcode-9: palindromes
- NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
- 【LeetCode】Day94-重塑矩阵
- Winter messenger 2
- [leetcode] day95 effective Sudoku & matrix zeroing
- Genesis builds a new generation of credit system
- 微信小程序路由再次跳转不触发onload
猜你喜欢

【高德地图POI踩坑】AMap.PlaceSearch无法使用

MPLS experiment

Find the combination number acwing 887 Find combination number III

Operator priority, one catch, no doubt

MPLS experiment

Game theory acwing 894 Split Nim game

1.手动创建Oracle数据库

高斯消元 AcWing 884. 高斯消元解异或線性方程組

Game theory acwing 893 Set Nim game

SQL三种连接:内连接、外连接、交叉连接
随机推荐
容斥原理 AcWing 890. 能被整除的数
2021apmcm post game Summary - edge detection
LeetCode 0107. Sequence traversal of binary tree II - another method
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
TCP's understanding of three handshakes and four waves
Usage scenarios of golang context
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
Game theory acwing 891 Nim games
Presentation of attribute value of an item
La redirection de l'applet Wechat ne déclenche pas onload
Leetcode dynamic programming
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
博弈论 AcWing 893. 集合-Nim游戏
2022-5-第四周日报
Chapter 6 relational database theory
MySQL advanced part 2: optimizing SQL steps
Game theory acwing 893 Set Nim game
如何正确在CSDN问答进行提问
P3265 [jloi2015] equipment purchase
MySQL advanced part 1: triggers