我的图像不使用RecyclerView显示吗?

问题描述 投票:1回答:2

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

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,它现在不是动态的,我在可绘制对象中直接使用图像,我用代码设置了源代码,没有用,在XML中设置了,没有用,在两者中都设置了,哈哈没有用。

任何帮助将不胜感激。

android android-recyclerview recycler-adapter imagesource
2个回答
1
投票
在imageview中,您具有换行内容,请在match_constrain中进行尝试,您需要在onCreateViewHolder上返回视图并在类似的视图中说出来,]

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

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

1
投票

第一件事

© www.soinside.com 2019 - 2024. All rights reserved.