gpt4 book ai didi

android - 无法查看 RecyclerView 中的条目

转载 作者:行者123 更新时间:2023-11-29 17:32:24 25 4
gpt4 key购买 nike

我目前正在使用 RecyclerView 在水平方向的列表中显示一组带有文本的图像。问题是在设置适配器并通知它数据更改后,我没有看到正在填充的网格。我以前使用 gridView 和 arrayAdapter 没有任何问题。我错过了一些基本的东西吗?

** 另外,我查看了 logcat,但没有发现任何异常。但是,如果您需要查看它,请告诉我,我会在此处添加 **

我已经按照以下方式设置了我的数据和适配器:

模型:CastViewObject

public class CastViewObject {


String castImageUrl;
String castName;
String castCharName;

public CastViewObject(String castName, String castCharName, String castImageUrl) {
this.castImageUrl = castImageUrl;
this.castName = castName;
this.castCharName = castCharName;
}

public String getCastImageUrl() {
return castImageUrl;
}

public void setCastImageUrl(String castImageUrl) {
this.castImageUrl = castImageUrl;
}

public String getCastCharName() {
return castCharName;
}

public void setCastCharName(String castCharName) {
this.castCharName = castCharName;
}

public String getCastName() {
return castName;
}

public void setCastName(String castName) {
this.castName = castName;
}

}

适配器:CastViewAdapter(扩展 RecyclerView.Adapter)- 目前仅显示覆盖的方法。

public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;
public TextView textView;


public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.cast_image);
textView = (TextView) itemView.findViewById(R.id.cast_name);
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View castView = layoutInflater.inflate(R.layout.cast_item_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(castView);
return viewHolder;
}

@Override
public void onBindViewHolder(CastViewAdapter.ViewHolder holder, int position) {
final String LOG_TAG = getClass().getSimpleName();
CastViewObject castObject = castViewList.get(position);
TextView textView = holder.textView;
textView.setText(castObject.getCastName());

Picasso
.with(mContext)
.load(castViewList.get(position).getCastImageUrl())
.resize(500, 750)
.error(R.drawable.user_placeholder_image)
.into(holder.imageView, new Callback() {
@Override
public void onSuccess() {
count++;
}

@Override
public void onError() {
Log.e(LOG_TAG, "Error in loading images");
}
});
}

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

自定义行:cast_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/cast_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/cast_name"
android:layout_alignParentBottom="true"
android:background="@color/title_background_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:textColor="@color/title_text_color"/>
</LinearLayout>

fragment 布局中的 RecyclerView:

      <android.support.v7.widget.RecyclerView
android:id="@+id/cast_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/movie_image_detail"
android:layout_alignStart="@+id/movie_image_detail"
android:layout_below="@+id/cast_divider"
android:layout_marginTop="10dp"
android:clickable="false" />

这是我在 onCreateView 方法中设置适配器的方式:

    RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.cast_list_view);
castAdapter = new CastViewAdapter(new ArrayList<CastViewObject>());
recyclerView.setAdapter(castAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
requestMovieDetails();

.. 并通知适配器数据的变化:

    public void setCastView(List<CastViewObject> castViewList){
castAdapter.setCastViewList(castViewList);
castAdapter.notifyDataSetChanged();
}

最佳答案

你必须 setAdapter在这一行之后:

recyclerView.setLayoutManager(linearLayoutManager);

更新:

您刚刚将新对象分配给 this.castViewList , 但您的适配器绑定(bind)到空的旧对象,因此 RecyclerView 中没有任何内容可显示.你必须做这样的事情:

this.castViewList.clear();
this.castViewList.addAll(castViewList);

关于android - 无法查看 RecyclerView 中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322171/

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