gpt4 book ai didi

安卓| RecyclerView 不起作用

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

我想创建一个 RecyclerView有几个CardView在里面。为此,我开始 ThreadonCreate我的Activity.在那里我从我的服务器获取数据并将其放入列表中。然后我为 RecyclerView, 创建适配器但它不起作用。这是我的代码:

我在这里创建适配器,它应该填充所有 CardView在:

ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setAdapter(adapter);

这是我的适配器类

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {

List<Post> posts;

public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}

@Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}

@Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}

@Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}


public static class PostViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;

PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}
}

这是我的回收 View :

<android.support.v7.widget.RecyclerView
android:id="@+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent">


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

和我的卡片 View :

<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/postPhoto"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textText"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/usernameText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeText"/>

</RelativeLayout>

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

这里有 2 个错误:

E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No layout manager attached; skipping layout

但我两者都做,不是吗?

最佳答案

首先设置LayoutManager

来自您的代码:

ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setHasFixedSize(true);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);

rv.setAdapter(adapter);

希望这对你有帮助!!..

关于安卓| RecyclerView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38404794/

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