[英]update RecyclerView after fetching data from server
我的 RecyclerView 有问题。 从服务器获取数据后,我想更新卡片(在 RecyclerView 内)。 首先,我认为获取或解析 JSON 数据或通过 EventBus 将数据发送到 Fragment 存在问题,但是当我在 Fragment 的 TextView 中显示解析的对象时,一切都会显示出来。 所以真正的问题是 RecyclerView / Adapter。 我已经用一个空的 ArrayList 初始化了 RecyclerView,但是我何时以及如何更新 RecyclerView 的数据? 我也在 Stackoverflow 上阅读了一些帖子,但我没有找到合适的解决方案......
这是来自 UserFragment 的onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View mView = inflater.inflate(R.layout.fragment_users, container, false);
mRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_user);
//set LayoutManager for View
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
//set Adapter for View
mAdapter = new UserAdapter(userList);
mRecyclerView.setAdapter(mAdapter);
Log.d(TAG, "onCreateView");
return mView;
}
这是我的UserAdapter
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
public static final String TAG = "UserAdapter";
private ArrayList<User> mUser;
public UserAdapter(ArrayList<User> user) {
mUser = user;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public CardView cv;
public TextView user_name;
public TextView user_lastName;
public TextView user_city;
public ViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.rv_user);
user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
user_lastName = (TextView) itemView.findViewById(R.id.tv_user_lastname);
user_city = (TextView) itemView.findViewById(R.id.tv_user_city);
Log.d(TAG, "Viewholder");
}
}
//initialize Viewholder
@Override
public UserAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout_person, viewGroup, false);
Log.d(TAG, "onCreateViewHolder");
return new ViewHolder(mView);
}
//Bind data to view
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.user_name.setText(mUser.get(position).getUser().getFirstname());
holder.user_lastName.setText(mUser.get(position).getUser().getLastname());
holder.user_city.setText(mUser.get(position).getUser().getCity());
Log.d(TAG, "onBindViewHolder");
}
public void setUserList(ArrayList<User> user){
this.mUser = user;
notifyItemRangeChanged(0, user.size());
notifyDataSetChanged();
}
public void updateData(ArrayList<User> userList) {
mUser.clear();
mUser.addAll(userList);
notifyDataSetChanged();
}
public void removeItem(int position) {
mUser.remove(position);
notifyItemRemoved(position);
}
@Override
public int getItemCount() {
Log.d(TAG, "getItemCount");
if (mUser == null) {
return 0;
} else {
return mUser.size();
}
} }
这是 MainActivity 中的 onResponse:
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseStr = response.body().string();
Gson gson = new Gson();
Type userListType = new TypeToken<ArrayList<User>>() {
}.getType();
final ArrayList<User> userList = gson.fromJson(responseStr, userListType);
CustomMessageEvent event = new CustomMessageEvent();
event.setUserList(userList);
EventBus.getDefault().post(event);
System.out.print(responseStr);
response.body().close();
}
}
更新
XML 文件 (CardView)
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rv_user"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="3dp"
android:elevation="11dp"
android:foregroundGravity="center_horizontal"
android:scrollbars="vertical"
android:theme="@style/AppThemeFSF"
app:cardBackgroundColor="@color/cardview_light_background"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/iv_profile_picture"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Name"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Nachname"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Stadt"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_user_lastname"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Nachname"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_user_city"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="Stadt"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
在此先感谢各位,祝您有美好的一天!
问题是您没有订阅事件回传。 因此,当您致电:
EventBus.getDefault().post(event);
没有人得到这个事件! 并且您没有填充列表。 因此,您的适配器没有任何显示。
订阅这样的事件:
@Subscribe
public void receiveEvent(CustomMessageEvent event){
mAdapter.updateData(event.getUserList())
}
此方法应该在您的片段中。 如果将其放入适配器中,将违反单一责任原则。 适配器应该唯一负责的是将列表链接到RecyclerView。
编码愉快!
好的,我有解决方案! 这让我有些疯狂……问题不是Adapter或ViewHolder或Fragment中的任何东西。 “真正的”问题是CardView-Elements中TextView的文本大小。 我删除了此属性,现在每个数据集都可用! 非常感谢您的帮助和提示! 你很棒!
快乐编码;)
尝试改变这个
public void setUserList(ArrayList<User> user){
this.mUser = user;
notifyItemRangeChanged(0, user.size());
notifyDataSetChanged();
}
对此
public void setUserList(ArrayList<User> user){
mUser.add(user);
notifyItemInserted(user.size());
notifyDataSetChanged();
}
然后在您的片段中,调用
mAdapter.setUserList(用户列表);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.