简体   繁体   中英

How to update data in RecyclerView on android

I want develop android application for one site. for load data i use RecyclerView and i want when user swipe Recyclerview update datas. i write this code, but i don't know write update code and use in onRefresh method !
My Adapter codes:

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private List<MainDataModel> mDateSet;
    private Context mContext;

    public MainAdapter(Context context) {
        this.mContext = context;
        this.mDateSet = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.post_card_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.main_post_title.setText(mDateSet.get(position).getTitle());
        Glide.with(mContext)
                .load(mDateSet.get(position).getThumbnail())
                .placeholder(R.drawable.post_image)
                .into(holder.main_post_image);
    }

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

    public void remove(int position){
        mDateSet.remove(position);
        notifyItemRemoved(position);
    }

    public void add(List<MainDataModel> models){
        mDateSet.addAll(models);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView main_post_title;
        private ImageView main_post_image;
        public ViewHolder(View itemView) {
            super(itemView);

            main_post_title = (TextView) itemView.findViewById(R.id.post_content_title);
            main_post_image = (ImageView) itemView.findViewById(R.id.post_picture_image);
        }
    }
}

Main activity codes:

public class Main_page extends AppCompatActivity {

    private static final long RIPPLE_DURATION = 250;
    private Toolbar toolbar;
    private RelativeLayout root;
    private ImageView menu_image;
    private SuperRecyclerView main_recyclerView;
    private MainAdapter mAdaper;
    private List<MainDataModel> dataModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(null);
        }
        // RecyclerView and setData
        main_recyclerView = (SuperRecyclerView) findViewById(R.id.main_recycler);
        //main_recyclerView.setHasFixedSize(true);
        main_recyclerView.setLayoutManager(new LinearLayoutManager(this));

        MainDataInfo dataInfo = new MainDataInfo();
        dataInfo.getMainDataInfo(this);

        mAdaper = new MainAdapter(this);
        main_recyclerView.setAdapter(mAdaper);

        main_recyclerView.setupMoreListener(new OnMoreListener() {
            @Override
            public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {

            }
        }, 3);

        main_recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh () {

            }});
    }

    @Subscribe
    public void onEvent(List<MainDataModel> mainInfoModels) {
        mAdaper.add(mainInfoModels);
    }
}

How can write update method in Adapter and use it onRefresh method?

Attention : Please don't give me negative points, i amateur and i really need you! thanks all <3

A couple of things I recommend you start off doing to get your RecyclerView to work:

  1. Put your List variable in your Activity and manage what gets added and removed from the adapter via the activity.
  2. Pass the List into the adapter via the adapter's constructor.
  3. Use mAdapter.notifyItemChanged(...) , mAdapter.notifyItemInserted(...) and mAdapter.notifyItemRemoved(...) whenever you change/add/remove things from the list. These cause the associated ViewHolder to rebind their view data.

So your code should look like this:
Activity

// I recommend calling this class MainPageActivity instead for consistency's sake
public class Main_page extends AppCompatActivity {
    MainAdapter mAdapter;
    List<MainDataModel> mItems = new ArrayList<MainDataModel>();
    // Use this to change an item in the list
    public void setItem(int index,MainDataModel item)
    {
        mItems.set(index, item);
        mAdapter.notifyItemChanged(index);
    }
    // Use this to add an item
    public void addItem(MainDataModel item)
    {
        mItems.add(item);
        mAdapter.notifyItemInserted(mItems.size() - 1);
    }
    // Use this to remove an item
    public void removeItem(int index)
    {
        mItems.remove(index);
        mAdapter.notifyItemRemoved(index);
    }
    // ... The rest of your code
}

Adapter

class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>
{
    private Context mContext;
    private List<MainDataModel> mDateSet;
    public MainAdapter(Context context, List<MainDataModel> dataSet)
    {
        this.mContext = context;
        this.mDataSet = dataSet;
    }
    // ... The rest of your code
}

You simply need to change the item in your dataset and call the relevant notify method in your adapter.

In your example you have mAdaper as your adapter and dataModels as your dataset.

Change say, index 3 of dataModels . dataModels.get(2).changeSomething();

You need to then tell the adapter that you changed that index. mAdaper.notifyItemChanged(2) .

Incidentally, I recommend you stick to one code style standard when writing your code (and do some spellchecking). In some places you prefix with m sometimes you don't, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM