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

huangapple go评论91阅读模式
英文:

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

问题

我正在尝试实现类似Instagram的应用用户可以点赞和分享但当他点击一个按钮类似图片上的心形按钮

///获取位置的接口
@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();

   //更新数据表
   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);
}

// 这是我的适配器中点击的接口,我想让点赞按钮变为红色,虽然实现了,但是消失得很快
@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;
}
英文:

I'm trying to implement app like Instagram where user can like and share but when he press on a button (like image heart) .
> ///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&lt;String,Object&gt; map=new HashMap&lt;&gt;();
map.put(id,new Upload(name,url,number,id));
//  mDatabaseRef.child(id).child(&quot;number_likes&quot;).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 **

 @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;
}

答案1

得分: 0

在项目点击时,使用调用 RecyclerViewsmoothScrollToPosition(0) 方法。

您可以在这个答案中看到一个示例。

英文:

On item click, use call RecyclerView's smoothScrollToPosition(0) method.

You can see an example in this answer.

huangapple
  • 本文由 发表于 2020年4月7日 08:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定