android - RecyclerView 不显示任何图像

标签 android android-adapter android-recyclerview

我正在我的应用程序中实现 RecyclerView 以在 fragment 中显示图像列表。但是无论何时打开 fragment ,列表都是空的,即不显示任何图像。我错过了什么?我是 android 的新手,所以放轻松 :p

如果您需要任何其他文件,也请告诉我。

我的 fragment :

公共(public)类 SponsorsFragment 扩展 fragment {

    public static int[] sponsorImageId = {
            R.drawable.apr,
            R.drawable.bigsandwich,
            R.drawable.bodal,
            R.drawable.brijeel
    };
    RecyclerView sponsorRecyclerView;

    public SponsorsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sponsors, container, false);

        sponsorRecyclerView = (RecyclerView) view.findViewById(R.id.sponsor_recycler_view);
        ImageAdapter imageAdapter = new ImageAdapter(getActivity(), getImageData());
        sponsorRecyclerView.setAdapter(imageAdapter);
        sponsorRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    public static List<ImageId> getImageData() {
        List<ImageId> data = new ArrayList<>();
        for (int i = 0; i < sponsorImageId.length; i++) {
            ImageId current = new ImageId();
            current.id = sponsorImageId[i];
            data.add(current);
        }
        return data;
    }
}

list_for_sponsors.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_for_sponsors"
        android:src="@drawable/brijeel">
    </ImageView>

</LinearLayout>

ImageAdapter.java:

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyImageViewHolder> {

    private LayoutInflater inflater;
    private Context context;
    private List<ImageId> images = Collections.emptyList();

    public ImageAdapter(Context c, List<ImageId> data) {
        context = c;
        inflater = LayoutInflater.from(context);
        images = data;
        Log.e("RecyclerView", "Error " + images.size());

    }

    @Override
    public MyImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_for_sponsors, parent, false);
        MyImageViewHolder holder = new MyImageViewHolder(view);
        return holder;
    }

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

    @Override
    public void onBindViewHolder(MyImageViewHolder holder, int position) {
        ImageId current = images.get(position);
        Log.e("RecyclerView", "" + current.id);
        holder.imageView.setImageResource(current.id);
    }

    class MyImageViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;

        public MyImageViewHolder(View view) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.image_for_sponsors);
        }
    }
}

fragment .xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.djtrinity.www.trinity.AboutFragment">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:id="@+id/sponsor_recycler_view">

    </android.support.v7.widget.RecyclerView>

</FrameLayout>

最佳答案

你能试试交换这两行吗

替换这个

sponsorRecyclerView.setAdapter(imageAdapter);
    sponsorRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

sponsorRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
sponsorRecyclerView.setAdapter(imageAdapter);

关于android - RecyclerView 不显示任何图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607712/

相关文章:

java - ListView 适配器中的 ViewHolder 不起作用

android - 如何通过ID删除recyclerview项目?

android - RecyclerView RecyclerViewDataObserver 未注册

android - 如何在 android 中将 imageView 设置为方形 View ?

android - 如何将两个适配器设置为一个 RecyclerView?

android - 如何在 Android 中管理 Activity 的 onPause、onResume 自定义适配器

android - 我的 recyclerview 数据在向下滚动时丢失

android - 为什么我的这个 ImageView 的 OnClick 不起作用?

java - 未从项目单击监听器上的 gridview 获取所有图像

android - 我可以管理生命周期所有者,用 Koin 注入(inject) viewModel 吗?