<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/c1"
android:layout_height="180dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ss"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@mipmap/ss" />
<TextView
android:id="@+id/textView2"
android:layout_width="209dp"
android:layout_height="40dp"
android:layout_marginStart="36dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="62dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/textView2"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="371dp"
android:layout_height="79dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我们可以看出上面的布局类似,只是内容不同。我们在写的时候不可能一个一个写,所以怎么解决?
步骤:
一:RecyclerView
二:我们新建布局文件:
三:修改布局文件的内容:
方法:我们需要单独创建一个内部类:
1:首先需要找到要修改的部件
2:然后进行修改
//列表控件里面的内容修改,需要单独创建一个类
//修改操作Item里面的控件
public class MyViewHolder extends RecyclerView.ViewHolder{//alt+enter显示修补程序
TextView titleText;
TextView contentText;
ConstraintLayout constraintLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titleText=itemView.findViewById(R.id.textView2);
contentText=itemView.findViewById(R.id.textView3);
constraintLayout=itemView.findViewById(R.id.c1);
}
}
这些是找到修改的部件。
怎么修改我们需要看下面:
//将列表控件复制n个并且填充到recycleview
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
//返回列表布局
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载布局
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_chat,parent,false);
MyViewHolder myViewHolder=new MyViewHolder(view);
return myViewHolder;
}
//position 位置
//这里赋值
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
String title=stringList.get(position);
holder.titleText.setText(title);
holder.contentText.setText("20crq");
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("poss","sss");
}
});
}
//列表布局显示个数
@Override
public int getItemCount() {
return stringList.size();
}
}
到这里我们既可以将组件加入了。当我们又很多这样的组件时,我们一个一个修改他的内容很麻烦,我们这里可以用一个数组来存储你需要填充的内容。
列布局显示的个数我们这时也可以用数组大小来显示。
怎么把数组的内容一个一个分配给配件属性:
position 为组建的位置,第一个组件位置为0
这样的话,就可以给很多组件进行赋值。
下面我们来看看上拉,下拉怎么刷新:
一:我们需要加入依赖
代码:
implementation 'io.github.scwang90:refresh-layout-kernel:2.0.5'//核心必须依赖
implementation 'io.github.scwang90:refresh-header-classics:2.0.5'//经典刷新头
implementation 'io.github.scwang90:refresh-header-radar:2.0.5'//雷达刷新头
implementation 'io.github.scwang90:refresh-header-falsify:2.0.5'//虚拟刷新头
implementation 'io.github.scwang90:refresh-header-material:2.0.5' //谷歌刷新头
implementation 'io.github.scwang90:refresh-header-two-level:2.0.5' //二级刷新头
implementation 'io.github.scwang90:refresh-footer-ball:2.0.5' //球脉冲加载
implementation 'io.github.scwang90:refresh-footer-classics:2.0.5' //经典加载
二:添加:android.enableJetifier=true
三:我们需要在布局文件:activity_main文件中加入布局
四:在MainActivity中加入上拉,下拉
smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
smartRefreshLayout.finishRefresh(2000);
stringList.clear();
stringList.add("下拉刷新");
stringList.add("aaaa");
stringList.add("bbbb");
stringList.add("ccc");
stringList.add("ddd");
stringList.add("wwww");
myAdapter.notifyDataSetChanged();
}
});
smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
smartRefreshLayout.finishLoadMore(2000);
stringList.add("上拉加载");
stringList.add("aaaa");
stringList.add("bbbb");
stringList.add("ccc");
stringList.add("ddd");
stringList.add("wwww");
myAdapter.notifyDataSetChanged();
}
});
最后完整代码:
MainActivity:
package com.example.class52;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter myAdapter;
SmartRefreshLayout smartRefreshLayout;
//ArraryList 可变长度数组
List<String> stringList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//添加数据
stringList.add("text1");
stringList.add("text2");
stringList.add("text3");
stringList.add("text4");
stringList.add("text5");
recyclerView= findViewById(R.id.r1);
myAdapter=new MyAdapter();
recyclerView.setAdapter(myAdapter);
smartRefreshLayout=findViewById(R.id.refresh);
//smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(this));
smartRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
smartRefreshLayout.finishRefresh(2000);
stringList.clear();
stringList.add("下拉刷新");
stringList.add("aaaa");
stringList.add("bbbb");
stringList.add("ccc");
stringList.add("ddd");
stringList.add("wwww");
myAdapter.notifyDataSetChanged();
}
});
smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
smartRefreshLayout.finishLoadMore(2000);
stringList.add("上拉加载");
stringList.add("aaaa");
stringList.add("bbbb");
stringList.add("ccc");
stringList.add("ddd");
stringList.add("wwww");
myAdapter.notifyDataSetChanged();
}
});
//一次显示一行
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
//按钮添加点击事件,添加数据
findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stringList.add("text11");
stringList.add("text21");
stringList.add("text31");
stringList.add("text41");
stringList.add("text51");
//刷新数据
myAdapter.notifyDataSetChanged();
}
});
}
//将列表控件复制n个并且填充到recycleview
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
//返回列表布局
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载布局
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_chat,parent,false);
MyViewHolder myViewHolder=new MyViewHolder(view);
return myViewHolder;
}
//position 位置
//这里赋值
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
String title=stringList.get(position);
holder.titleText.setText(title);
holder.contentText.setText("20crq");
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("poss","sss");
}
});
}
//列表布局显示个数
@Override
public int getItemCount() {
return stringList.size();
}
}
//列表控件里面的内容修改,需要单独创建一个类
//修改操作Item里面的控件
public class MyViewHolder extends RecyclerView.ViewHolder{//alt+enter显示修补程序
TextView titleText;
TextView contentText;
ConstraintLayout constraintLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titleText=itemView.findViewById(R.id.textView2);
contentText=itemView.findViewById(R.id.textView3);
constraintLayout=itemView.findViewById(R.id.c1);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="354dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/r1"
android:layout_width="match_parent"
android:layout_height="354dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加数据"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>