英文:
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<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 **
@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
在项目点击时,使用调用 RecyclerView
的 smoothScrollToPosition(0)
方法。
您可以在这个答案中看到一个示例。
英文:
On item click, use call RecyclerView
's smoothScrollToPosition(0)
method.
You can see an example in this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论