【问题标题】:My images are not displayed using RecyclerView?我的图像没有使用 RecyclerView 显示?
【发布时间】:2020-04-14 15:57:04
【问题描述】:

我有一个小问题,我正在尝试显示项目列表,但它不起作用,每个项目都包含一个图像,所以这是我的活动:

public class TestRecyclerImagesActivity extends AppCompatActivity {
    @BindView(R.id.listImages)
    RecyclerView listImages;


    RecyclerView.Adapter adapter;

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

        ButterKnife.bind(this);

        ImageModel img = new ImageModel("test1");
        List list = new ArrayList<ImageModel>();

        list.add(img);
        list.add(img);

        list.add(img);
        list.add(img);


        adapter = new ImageListAdapter(this, list);

        this.listImages.setAdapter(adapter);

        this.listImages.setLayoutManager(new LinearLayoutManager(this));

    }
}

这是我的适配器:

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ImageViewHolder>{

    List<ImageModel> images;
    Context context;

    public ImageListAdapter(Context context, List<ImageModel> contracts){
        this.context = context;
        this.images = contracts;

    }

    public void updateImages(List<ImageModel> newImages){
        images.clear();
        images.addAll(newImages);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ImageListAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image, parent, false); // ContratBinding >> as your list item layout named "contrat"
        return new ImageListAdapter.ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageListAdapter.ImageViewHolder holder, int position) {
        holder.bind(images.get(position));
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img)
        ImageView image;

        public ImageViewHolder(View iview){
            super(iview);
            ButterKnife.bind(this, iview);
        }

        void bind(ImageModel imageModel){
            image.setImageResource(R.drawable.newimage);
        }

    }


}

这是我的 listImages 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/listImages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteX="1dp"
            tools:layout_editor_absoluteY="1dp" />
</LinearLayout>

这是我的物品:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" >
    <ImageView
        android:id="@+id/img"
        android:src="@drawable/newimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

那么为什么它什么都不显示呢?我觉得我做的一切都是正确的。

对于Image src,它现在不是动态的,我直接在我的drawable中使用了一个图像,我用代码设置了源,没有用,在XML中设置它,没有用,在两者中都设置它大声笑没有用。

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-recyclerview imagesource


    【解决方案1】:

    在图像视图中,你有包装内容在 match_constrain 中尝试,你需要在 onCreateViewHolder 上返回你的视图并在视图中这样说

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image, parent, false);
    ImageView object = view.findViewById<ImageView>(R.id.img);
    

    您现在有了图像视图,现在选择图像源并将其传递给对象的源。

    【讨论】:

    • 我正在使用 ButterKnife 注释来获取我的元素
    • 从未使用过黄油刀注释,但在代码中他们说您需要同时返回视图
    • public class MyAdapter extends BaseAdapter { @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false);持有人=新的ViewHolder(视图); view.setTag(持有人); } holder.name.setText("John Doe"); // 等等...返回视图; }
    • 我已经返回了新的 ImageViewHolder,我想你只是没有看到
    • ImageModel 作为参数存在,但尚未使用它。我的图像是 imageView image.setImageResource(R.drawable.newimage);
    【解决方案2】:

    第一件事 尝试在适配器之前设置布局管理器

    this.listImages.setLayoutManager(new LinearLayoutManager(this)); 
    this.listImages.setAdapter(adapter); 
    

    第二件事

    使recyclerview的宽度match_parent和它的父LinearLayout的宽度也match_parent

    第三件事

    为您的布局管理器提供所需的方向,例如垂直/水平

    第四件事

    在适配器本身中初始化你的数组列表,并在更新时清除并添加所有新列表

    公共类 ImageListAdapter 扩展 RecyclerView.Adapter{

    List<ImageModel> images = new ArrayList();
    Context context;
    
    public ImageListAdapter(Context context, List<ImageModel> contracts){
        this.context = context;
        this.images.clear();
        this.addAll(contracts);
    }
    

    第五件事

    尝试给imageview固定高度,目前是wrap_content

    【讨论】:

    • 使recyclerview的宽度match_parent与其父LinearLayout的宽度也match_parent
    • 嗯,没有没有用,我不认为这是一个样式问题
    • @TaouBen哈哈哈..我投降了!!
    • 没问题,我也投降了哈哈,感觉自己做的都对了,很奇怪
    • 大声笑!也只是最后一个指针,您可以使用“开发人员选项”设置中的“显示布局绑定”选项检查视图。当你启用它时,你将能够看到所有视图的轮廓,也许它可以为你提供一些线索。
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-12-11
    • 2019-01-13
    • 2021-03-10
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多