我按照以下方式将值插入RecyclerView
public class UserListFragment extends Fragment implements UserEvents {
private View rootView;
private Activity mActivity;
private RecyclerView recyclerView;
ArrayList<User> mUserList;
RoomOrUserRecyclerViewAdapter adapter;
public static Fragment newInstance() {
UserListFragment f = new UserListFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.user_list_fragment, container, false);
findViews(rootView);
return rootView;
}
private void findViews(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mUserList = ((SocketApplication) mActivity.getApplication()).getUserListExcludeMineId();
if (mUserList != null && mUserList.size() > 0) {
adapter = new RoomOrUserRecyclerViewAdapter(getActivity(), mUserList, null);
recyclerView.setAdapter(adapter);
}
((SocketApplication) mActivity.getApplication()).setmUserEventsInterface(this);
}
@Override
public void onAttach(Activity activity) {
mActivity = activity;
super.onAttach(activity);
}
@Override
public void onUserLeft(int position) {
if (position != -1) {
adapter.notifyItemRemoved(position);
adapter.notifyItemRangeChanged(position, mUserList.size());
}
}
@Override
public void onUserJoin(final User user) {
mUserList.add(user);
adapter.notifyItemInserted(mUserList.size() - 1);
}
}
但是没有插入该项目,或者我在UI中没有显示任何更新
任何人都可以帮我解决这个问题
当新用户插入或删除时,此UserEvents接口的方法将执行
@Override
public void onUserLeft(int position) {
if (position != -1) {
adapter.notifyItemRemoved(position);
adapter.notifyItemRangeChanged(position, mUserList.size());
}
}
@Override
public void onUserJoin(final User user) {
mUserList.add(user);
adapter.notifyItemInserted(mUserList.size() - 1);
}
}
但不知道为什么Recycler没有更新
下面是截取我的调试器点
答案 0 :(得分:0)
你能试试这段代码吗?我尝试这个代码我的项目和工作。
@Override
public void onUserLeft(final int position) {
if (position != -1) {
mUserList.remove(position);
adapter.notifyItemRangeChanged(position,1);
}
}
@Override
public void onUserJoin(final User user) {
mUserList.add(user);
adapter.notifyItemRangeChanged(mUserList.size()-1,1);
}
答案 1 :(得分:0)
试试这个
mUserList.remove(position);
recycler.removeViewAt(position);
adapter.notifyItemRemoved(position);
adapter.notifyItemRangeChanged(position, mUserList.size());
答案 2 :(得分:0)
删除或添加后尝试使用此功能。
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});