今天敲代码时,新建了一个项目,原来Recyclerview控件开头的android都变成了androidx,本博客还没有更新,根据实际去写吧1
recyclerview依赖改成了
implementation 'androidx.recyclerview:recyclerview:1.0.0'
效果图如图:
首先加依赖(请读者对应自己的版本号)
implementation 'com.android.support:appcompat-v7:28.0.0'
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/student_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
然后我们先写item,创建空活动item(即RecyclerView中的每一个小部分)
activity_item.xml:(这里的图片和名字都只是表明item图片和名字的位置,并非真正在RecyclerView中展示的item)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/detail"
android:layout_height="150dp"
android:orientation="vertical"
android:elevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="0dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_weight="5"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:text="姓名:"
android:textStyle="bold"
android:textSize="19sp"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="白白白"
android:textStyle="bold"
android:maxLines="1"
android:textSize="19sp" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:text="年级:"
android:textStyle="bold"
android:textSize="19sp"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/student_grade"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="大三"
android:textStyle="bold"
android:maxLines="1"
android:textSize="19sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="专业:"
android:textStyle="bold"
android:textSize="19sp"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/student_major"
android:layout_width="match_parent"
android:layout_height="30dp"
android:maxLines="1"
android:text="计算机科学与技术"
android:textSize="19sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="学号:"
android:textStyle="bold"
android:textSize="19sp"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/student_id"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="201713137040"
android:textStyle="bold"
android:maxLines="1"
android:textSize="19sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#11454545"/>
</LinearLayout>
然后我们写Bean:Student类
package com.example.student_recyclerview.Bean;
public class Student {
String Student_Id;
String Student_Name;
String Student_Major;
String Student_Grade;
public Student(String student_id,String student_name,String student_major,String student_Grade) {
this.Student_Id = student_id;
this.Student_Name = student_name;
this.Student_Major = student_major;
this.Student_Grade = student_Grade;
}
public String getStudent_Id() {
return Student_Id;
}
public void setStudent_Id(String student_Id) {
Student_Id = student_Id;
}
public String getStudent_Name() {
return Student_Name;
}
public void setStudent_Name(String student_Name) {
Student_Name = student_Name;
}
public void setStudent_Grade(String student_Grade) {
Student_Grade = student_Grade;
}
public String getStudent_Grade() {
return Student_Grade;
}
public String getStudent_Major() {
return Student_Major;
}
public void setStudent_Major(String student_Major) {
Student_Major = student_Major;
}
}
然后我们写适配器(连接作用,将我们的代码用图像的方式展示出来)(点击事件我们就在适配器中写)
package com.example.student_recyclerview.Adapter;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.student_recyclerview.R;
import com.example.student_recyclerview.Activity.StudentDetail;
import com.example.student_recyclerview.Bean.Student;
import java.util.ArrayList;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
List<Student> list = new ArrayList<>();
Context context;
LayoutInflater inflater;
public StudentAdapter(List<Student> list,Context context){
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public StudentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.studentitem,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull StudentAdapter.ViewHolder viewHolder, final int i) {
Student student = list.get(i);
viewHolder.student_id.setText(student.getStudent_Id());
viewHolder.student_name.setText(student.getStudent_Name());
viewHolder.student_grade.setText(student.getStudent_Grade());
viewHolder.student_major.setText(student.getStudent_Major());
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(context, StudentDetail.class);
intent.putExtra("position",i);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView student_id;
TextView student_name;
TextView student_major;
TextView student_grade;
LinearLayout linearLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
student_id = itemView.findViewById(R.id.student_id);
student_name = itemView.findViewById(R.id.student_name);
student_major = itemView.findViewById(R.id.student_major);
student_grade = itemView.findViewById(R.id.student_grade);
linearLayout = itemView.findViewById(R.id.detail);
}
}
}
最后在MainActivity中将整体联系起来
package com.example.student_recyclerview.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.student_recyclerview.Adapter.StudentAdapter;
import com.example.student_recyclerview.R;
import com.example.student_recyclerview.Bean.Student;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static List<Student> list = new ArrayList<>();
RecyclerView student_list;
StudentAdapter studentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
student_list = findViewById(R.id.student_list);
initRecyclerView();
}
private void initRecyclerView() {
list.clear();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
student_list.setLayoutManager(linearLayoutManager);
studentAdapter = new StudentAdapter(list, MainActivity.this);
student_list.setAdapter(studentAdapter);
initList();
}
private void initList() {
//只是为了测试
Student student = new Student("201713137040", "刘卫东", "计算机科学与技术", "大三");
list.add(student);
Student student1 = new Student("201713137041", "张明杰", "信息安全", "大二");
list.add(student1);
Student student2 = new Student("201713137042", "Mary", "软件工程", "大一");
list.add(student2);
Student student3 = new Student("201713137043", "百叶", "网络工程", "大四");
list.add(student3);
Student student4 = new Student("201713137044", "托尼", "国际法", "大二");
list.add(student4);
Student student5 = new Student("201713137045", "杰克", "电子信息", "大三");
list.add(student5);
Student student6 = new Student("201713137046", "Tom", "经济法", "大三");
list.add(student6);
Student student7 = new Student("201713137047", "天明和", "法学", "大二");
list.add(student7);
Student student8 = new Student("201713137048", "世纪", "教育", "大三");
list.add(student8);
}
}
最后跳转后的详细信息界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="45dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="你的名字"
android:textSize="18sp" />
<TextView
android:id="@+id/get_name"
android:textSize="18sp"
android:text="2231"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="你的学号"
android:textSize="18sp" />
<TextView
android:id="@+id/get_id"
android:textSize="18sp"
android:text="2231"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的年级"
android:textSize="18sp" />
<TextView
android:id="@+id/get_grade"
android:textSize="18sp"
android:text="2231"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的专业"
android:textSize="18sp" />
<TextView
android:id="@+id/get_major"
android:textSize="18sp"
android:text="2231"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp" />
</LinearLayout>
</LinearLayout>
package com.example.student_recyclerview.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.student_recyclerview.R;
import com.example.student_recyclerview.Bean.Student;
import static com.example.student_recyclerview.Activity.MainActivity.list;
public class StudentDetail extends AppCompatActivity {
TextView getname;
TextView getid;
TextView getmajor;
TextView getgrade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_detail);
getid = findViewById(R.id.get_id);
getname = findViewById(R.id.get_name);
getgrade = findViewById(R.id.get_grade);
getmajor = findViewById(R.id.get_major);
initData();
}
private void initData() {
int position = getIntent().getIntExtra("position",-1);
Student student = list.get(position);
getid.setText(student.getStudent_Id());
getname.setText(student.getStudent_Name());
getmajor.setText(student.getStudent_Major());
getgrade.setText(student.getStudent_Grade());
}
}
怎么获取当前点击的位置?
holder.getAdapterPosition()
代码实例:用于点击某一项item实现一些标识
if (position == curPos) {
holder.rlItemRoot.setBackgroundResource(R.drawable.bg_portrait_selected);
} else {
holder.rlItemRoot.setBackgroundResource(R.drawable.bg_portrait_normal);
}
holder.rlItemRoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
curPos = holder.getAdapterPosition();
notifyDataSetChanged();
}
});