public class MainActivity extends AppCompatActivity {
private List<Sights> sightsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
RecyclerView my_recycler_view = findViewById(R.id.my_recycler_view);
for(int i=0;i<10;i++){
Sights sights = new Sights();
sights.setSightsImage(R.drawable.image1);
sights.setSightsName("sights" + i);
sightsList.add(sights);
}
my_recycler_view.setLayoutManager(new LinearLayoutManager(this));
SightsRecyclerAdapter sightsRecyclerAdapter = new SightsRecyclerAdapter(sightsList);
my_recycler_view.setAdapter(sightsRecyclerAdapter);
}
}
public class Sights {
private int sightsImage;
private String sightsName;
public int getSightsImage() {
return sightsImage;
}
public void setSightsImage(int sightsImage) {
this.sightsImage = sightsImage;
}
public String getSightsName() {
return sightsName;
}
public void setSightsName(String sightsName) {
this.sightsName = sightsName;
}
}
public class SightsRecyclerAdapter extends RecyclerView.Adapter<SightsRecyclerAdapter.ViewHolder> {
private List<Sights> sightsList;
public SightsRecyclerAdapter(List<Sights> sightsList) {
this.sightsList = sightsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_recycler_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Sights sights = sightsList.get(position);
holder.sightsImageView.setImageResource(sights.getSightsImage());
holder.sightsTextView.setText(sights.getSightsName());
}
@Override
public int getItemCount() {
return sightsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView sightsImageView;
private TextView sightsTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
sightsImageView = itemView.findViewById(R.id.item_image);
sightsTextView = itemView.findViewById(R.id.item_name);
}
}
}
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
>
<ImageView
android:id="@+id/item_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/image1"/>
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:textSize="32sp"
android:text="SightsName"/>
</LinearLayout>
文章评论