简体   繁体   English

RecyclerView 只显示一张图片而不是多张图片

[英]RecyclerView only shows one image rather than multiple

I am trying to use RecyclerView to show a text and image.When i run my app in emulator it only shows one image that is pic2 and not pic one.我正在尝试使用 RecyclerView 来显示文本和图像。当我在模拟器中运行我的应用程序时,它只显示一张 pic2 而不是 pic1 的图像。 I want both of them to be seen in my app when i run it on emulator.当我在模拟器上运行它时,我希望它们都能在我的应用程序中看到。 It shows no error in my code when i add resource for pic 1 it still shows pic2.当我为 pic 1 添加资源时,它在我的代码中没有显示错误,它仍然显示 pic2。 But when i make some changes in text it does show that change in emulator.但是当我对文本进行一些更改时,它确实显示了模拟器中的更改。

MainActivity.java主活动.java

    import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //private LinearLayoutManager linearLayoutManager;
    //private List<ModelClass> item_list;
    //private modelAdapter mModelAdapter;

    private List<ModelClass2> item_list2;
    private modelAdapter2 mModelAdapter2;
    private LinearLayoutManager linearLayoutManager2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        item_list2 = new ArrayList<>();
        mModelAdapter2 = new modelAdapter2(this, item_list2);

        linearLayoutManager2 = new LinearLayoutManager(this);
        recyclerView.setAdapter(mModelAdapter2);
        recyclerView.setLayoutManager(linearLayoutManager2);


        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shiv", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Andrew", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic1));
        item_list2.add(new ModelClass2("Shivam", R.drawable.pic2));

    }

}

activity_main.xml活动_main.xml

    <?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/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ModelClass2.java模型类2.java

 public class ModelClass2 {
    String Name;
    int imgresource;

    public ModelClass2(String name, int imgresource) {
        Name = name;
        this.imgresource = imgresource;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getImgresource() {
        return imgresource;
    }

    public void setImgresource(int imgresource) {
        this.imgresource = imgresource;
    }
}

ModelAdapter2.java模型适配器2.java

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class modelAdapter2 extends RecyclerView.Adapter<modelAdapter2.MyViewHolder> {
    private Context context;
    private List<ModelClass2> itemlist2;

    public modelAdapter2(Context context, List<ModelClass2> itemlist2) {
        this.context = context;
        this.itemlist2 = itemlist2;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ModelClass2 modelClass2=itemlist2.get(position);


        holder.textView.setText(itemlist2.get(position).getName());
        holder.imageView.setImageResource(R.drawable.pic2);

    }

    @Override
    public int getItemCount() {
        return itemlist2.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.t1);
            imageView = itemView.findViewById(R.id.i1);
        }
    }
}

recycler_item.xml recycler_item.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"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="8dp"
    android:background="#D74A66F1">

    <TextView
        android:id="@+id/t1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:height="70dp"
        android:fontFamily="casual"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#3D3636"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/i1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_weight="2"
        android:scaleType="fitXY"
        app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>

I think the problem is present inside the ModelAdapter2.java as in onBindViewHolder , you are setting image explicitly to R.drawable.pic2我认为这个问题是存在的内部ModelAdapter2.javaonBindViewHolder,你明确地设定图像R.drawable.pic2

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        ModelClass2 modelClass2=itemlist2.get(position);


        holder.textView.setText(itemlist2.get(position).getName());
        holder.imageView.setImageResource(R.drawable.pic2); <-----------

    }

I think it should be as follow:我认为应该如下:

holder.imageView.setImageResource(itemlist2.get(position).getImgresource());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM