标签:int 安卓 Person 开发 mPersonList new RecyclerView public
目录:
一、.准备数据:
1.新建一个类,规范化数据和函数:
//在Activity类的包下再创建一个包data,并在里面new一个java类:Person
package com.example.myemptyapplication.data;
public class Person {
//数据域:
private int imageId;//头像(图片的id是int型)
private String name;//名字
private String introduction;//简介
//构造函数:用于初始化对象的数据域!
public Person(int imageId,String name,String introduction){
this.imageId=imageId;
this.name=name;
this.introduction=introduction;
}
//下面这几个方法都是上面三个数据域的set和get
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}
2、创建RecyclerView每一个子项:
注意:布局大小需要斟酌:“wrap_content”
<?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="wrap_content"
android:layout_height="wrap_content">>
<ImageView
android:id="@+id/imageView_item1"
android:layout_width="77dp"
android:layout_height="57dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/btn_star_big_on" />
<TextView
android:id="@+id/textView_item2"
android:layout_width="325dp"
android:layout_height="25dp"
android:layout_marginTop="4dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView_item1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_item3"
android:layout_width="324dp"
android:layout_height="27dp"
android:layout_marginTop="4dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView_item1"
app:layout_constraintTop_toBottomOf="@+id/textView_item2" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.在Activity类中创建一个List集合,用于存放对应的数据:
//数据域:
List<Person> mPersonList=new ArrayList<>();
//初始化集合
private void initeListset(){
for (int i=0;i<50;i++){//50组相同的数据
Person p1=new Person(R.drawable.n1,"帅哥1号","史蒂夫多部话剧都不回家第三步二班");
Person p2=new Person(R.drawable.n2,"帅哥2号","好的发电设备的不得不");
Person p3=new Person(R.drawable.n3,"帅哥3号","i的撒手锏警方的说法");
Person p4=new Person(R.drawable.n4,"帅哥4号","我和我那些的厚度和接收方划分");
Person p5=new Person(R.drawable.m1,"美女","四驱蚊器不得不说反对反对我");
Person p6=new Person(R.drawable.dm1,"动漫","问问去恶趣味合法的二恶而我却额认为无参 手动");
mPersonList.add(p1);
mPersonList.add(p2);
mPersonList.add(p3);
mPersonList.add(p4);
mPersonList.add(p5);
mPersonList.add(p6);
}
}
二、将RecyclerView加入到主页面上:
点击如图按钮拖动即可:
然后添加一个id,方便我们在代码中引用它:
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myrecyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="110dp"
tools:layout_editor_absoluteY="373dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
三、配置适配器,作为数据转移的中介:
在Activity类上加一个外部类PersonAdapter,并继承于RecyclerView.Adapter:
然后再写一个内部类,继承于RecyclerView.ViewHolder
/******上面的Activity的代码*************************************************/
class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder>{
//数据域:
private List<Person> mPersonList;
//内部类:
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView;
TextView mTextView1;
TextView mTextView2;
//重写父类的构造函数,对上面的数据域进行赋值:
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImageView=itemView.findViewById(R.id.imageView_item1);
mTextView1=itemView.findViewById(R.id.textView_item2);
mTextView2=itemView.findViewById(R.id.textView_item3);
}
}
//构造函数:对数据域(一个集合)进行赋值
public PersonAdapter(List<Person> personList){
mPersonList=personList;
}
@NonNull
@Override
public PersonAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//关联到之前我们创建的子项:
View mView= LayoutInflater.from(parent.getContext()).inflate(R.layout.person_iten,
parent, false);
//实例化一个对象并返回(给系统)
ViewHolder mViewHodler=new ViewHolder(mView);
return mViewHodler;
}
@Override
public void onBindViewHolder(@NonNull PersonAdapter.ViewHolder holder, int position) {
//获取当前位置的子项实例
Person mPerson=mPersonList.get(position);
//代码动态设置头像、名字、简介
holder.mImageView.setImageResource(mPerson.getImageId());
holder.mTextView1.setText(mPerson.getName());
holder.mTextView2.setText(mPerson.getIntroduction());
}
@Override
public int getItemCount() {
//返回集合的长度,即Person个数:
return mPersonList.size();
}
}
四、整合:在在Activity类中引用!
在Activity类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//添加如下代码:
initeListset();//初始化集合
RecyclerView mRecyclerView=findViewById(R.id.myrecyclerview1);//获取
/*此处是控制子项的布局,可改成其他的:
1、LinearLayoutManager 是线性的(垂直/水平)。
2、GridLayoutManager 是二维网格的,就像旧版 GridView 布局的功能。
3、StaggeredGridLayoutManager 将各项内容排列在二维网格中,
每一列都在前一列基础上稍微偏移。
4、如果上面的三种布局管理器都不符合您的需求,
您可以通过扩展 RecyclerView.LayoutManager 抽象类来创建自己的布局管理器。
如:网格形式:
StaggeredGridLayoutManager mLinearLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
*/
LinearLayoutManager mLinearLayoutManager=new LinearLayoutManager(this);
mLinearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);//水平滚动
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(new PersonAdapter(mPersonList));
}
到此就可以拿出去显摆了。
五、添加事件:
1、在内部类中添加一个View变量,保存itemView实例:
2、还记得我们return过一个没动过的对象吗:
更多请访问官网文档:
(https://developer.android.google.cn/guide/topics/ui/layout/recyclerview)
标签:int,安卓,Person,开发,mPersonList,new,RecyclerView,public 来源: https://blog.csdn.net/zhinengxiong6/article/details/111122871
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。