gpt4 book ai didi

android - RecyclerView 不显示任何图像

转载 作者:行者123 更新时间:2023-12-05 07:52:26 27 4
gpt4 key购买 nike

我正在我的应用程序中实现 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com