RecyclerView没有显示任何图像

时间:2015-11-09 11:02:42

标签: android android-adapter android-recyclerview

我在我的应用中实现了RecyclerView,以显示片段中的图像列表。但是每当片段打开时,列表仍然为空,即没有显示图像。我错过了什么?我对android很新,所以请轻松一下:p

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

我的片段:

public class 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>

2 个答案:

答案 0 :(得分:0)

你可以尝试交换这两行

替换此

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

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

答案 1 :(得分:0)

我遇到过这样的问题,就我而言,我已经解决了这个问题。

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

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);
     List<ImageId> emptylist = Collections.emptyList();
     imageAdapter = new ImageAdapter(getActivity(), emptylist);
    sponsorRecyclerView.setAdapter(imageAdapter);
    sponsorRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    setImages();
    return view;
}

public static void setImages() {
    for (int i = 0; i < sponsorImageId.length; i++) {
        ImageId current = new ImageId();
        current.id = sponsorImageId[i];
       imageAdapter.addData(current);
    }

}
}

现在按如下方式更改ImageAdapter: -

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);
    }
}
 public void addData(ImageId current){
      images.add(current);
     notifyDataSetChanged();
  }
  }