简体   繁体   English

刷新 RecyclerView 中的数据并保持其滚动 position 将用户带到活动的顶部

[英]Refreshing data in RecyclerView and keeping its scroll position take user to the top of the activity

I'm trying to implement app like Instagram where user can like and share but when he press on a button (like image heart).我正在尝试实现像 Instagram 这样的应用程序,用户可以在其中喜欢和分享,但是当他按下按钮时(比如图像心)。

 ///interface to get position
@Override
public void onitemclick(int position) {

     position_of_image=position;
    int num1=uploads.get(position_of_image).getNumber_likes();
    uploads.get(position_of_image).setNumber_likes(num1+1);

   String id=uploads.get(position_of_image).getId();
   int number= uploads.get(position_of_image).getNumber_likes();
   String name= uploads.get(position_of_image).getName();
   String url=uploads.get(position_of_image).getImageUrl();

   //updating the tables

   Map<String,Object> map=new HashMap<>();
   map.put(id,new Upload(name,url,number,id));
//  mDatabaseRef.child(id).child("number_likes").setValue(number);
    uploads.clear();
    mDatabaseRef.updateChildren(map);
    mRecyclerView.smoothScrollToPosition(position_of_image);
}

**and this is my interface on click in my adapter I want to make the like btn is red it done but disappears quickly ** **这是我在我的适配器中单击时的界面我想让 btn 像红色一样完成但很快消失**

 @Override
        public void onClick(View v) {
        if (mlistener!=null){
            int position=getAdapterPosition();
            if (position!=RecyclerView.NO_POSITION){
                mlistener.onitemclick(position);

            }
           like.setImageResource(R.drawable.red_heart);

        }


public interface  onlikeclic{
        void onitemclick(int position);


    }
    public void setonitemclicklistener(onlikeclic listener){
        mlistener=listener;
    }

On item click, use call RecyclerView 's smoothScrollToPosition(0) method.单击项目时,使用调用RecyclerViewsmoothScrollToPosition(0)方法。

You can see an example in this answer .您可以在此答案中看到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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