RecyclerView简单示例
Posted buyishi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView简单示例相关的知识,希望对你有一定的参考价值。
MainActivity.java
package com.example.buish.myapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private Button showButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); showButton = findViewById(R.id.showButton); showButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList<Person> persons = new ArrayList<>(); persons.add(new Person("成龙", "男", "成龙大哥", R.mipmap.jackie_chan)); persons.add(new Person("Bruce Lee", "男", "I know that Bruce Lee is the best!", R.mipmap.bruce_lee)); persons.add(new Person("吴京", "男", "战狼", R.mipmap.wu_jing)); persons.add(new Person("赵文卓", "男", "生死拳速", R.mipmap.zhao_wenzhuo)); persons.add(new Person("Android", "Male", "I‘m a robot!", R.mipmap.ic_launcher_round)); PersonRecyclerViewAdapter adapter = new PersonRecyclerViewAdapter(MainActivity.this, persons); recyclerView.setAdapter(adapter); } }); } }
Person.java
package com.example.buish.myapp; public class Person { public String name; public String gender; public String whatsup; public int imageId; public Person(String name, String gender, String whatsup, int imageId) { this.name = name; this.gender = gender; this.whatsup = whatsup; this.imageId = imageId; } }
PersonRecyclerViewAdapter.java
package com.example.buish.myapp; import android.content.Context; 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.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class PersonRecyclerViewAdapter extends RecyclerView.Adapter<PersonRecyclerViewAdapter.PersonRecyclerViewHolder> { private Context context; private ArrayList<Person> persons; public PersonRecyclerViewAdapter(Context context, ArrayList<Person> persons) { this.context = context; this.persons = persons; } @NonNull @Override public PersonRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_person, viewGroup, false); return new PersonRecyclerViewHolder(view); } @Override public void onBindViewHolder(@NonNull PersonRecyclerViewHolder holder, int i) { final Person person = persons.get(i); holder.personImageView.setImageResource(person.imageId); holder.personNameTextView.setText(person.name); holder.personGenderTextView.setText(person.gender); holder.personWhatsupTextView.setText(person.whatsup); holder.personLinearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, person.whatsup, Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return persons.size(); } class PersonRecyclerViewHolder extends RecyclerView.ViewHolder { LinearLayout personLinearLayout; ImageView personImageView; TextView personNameTextView, personGenderTextView, personWhatsupTextView; public PersonRecyclerViewHolder(@NonNull View itemView) { super(itemView); personLinearLayout = itemView.findViewById(R.id.personLinearLayout); personImageView = itemView.findViewById(R.id.personImageView); personNameTextView = itemView.findViewById(R.id.personNameTextView); personGenderTextView = itemView.findViewById(R.id.personGenderTextView); personWhatsupTextView = itemView.findViewById(R.id.whatsupTextView); } } }
activity_main.xml
<?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"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/showButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="显示" /> </LinearLayout>
item_person.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/personLinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@android:color/white" android:orientation="horizontal"> <ImageView android:id="@+id/personImageView" android:layout_width="50dp" android:layout_height="50dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名称:" /> <TextView android:id="@+id/personNameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:" /> <TextView android:id="@+id/personGenderTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个性签名:" /> <TextView android:id="@+id/whatsupTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </LinearLayout>
以上是关于RecyclerView简单示例的主要内容,如果未能解决你的问题,请参考以下文章
在垂直 recyclerView 中带有 wrap_content 的水平 recyclerView