[英]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.