英文:
How to pin one itemview in recyclerview that Always show that view on top of recyclerview
问题
我有一个RecyclerView。数据来自Firebase。我想将一个项目固定在所有项目的顶部显示。我该如何做到这一点?
我不明白如何做到这一点。
我的适配器代码是:
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ViewHolder> {
Context context;
ArrayList<DataReturn> list;
public ReportAdapter(Context context, ArrayList<DataReturn> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ReportAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.report_user, parent, false);
return new ReportAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ReportAdapter.ViewHolder holder, int position) {
DataReturn dataReturn = list.get(position);
holder.reportId.setText(dataReturn.getFromNumber());
holder.reportName.setText(dataReturn.getName());
holder.reportRank.setText(dataReturn.getRank());
holder.reportLastVacation.setText(dataReturn.getLastVacation());
holder.reportNextVacation.setText(dataReturn.getComingEnd());
String image = dataReturn.getImage();
if (image.equals("")){
holder.circleImageView.setImageResource(R.drawable.error);
}else {
Glide.with(context).load(image).into(holder.circleImageView);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView circleImageView;
TextView reportName, reportLastVacation, reportNextVacation, reportRank,reportId;
public ViewHolder(@NonNull View itemView) {
super(itemView);
reportId = itemView.findViewById(R.id.item_number);
reportName = itemView.findViewById(R.id.report_name);
reportLastVacation = itemView.findViewById(R.id.report_last_vacation);
circleImageView = itemView.findViewById(R.id.report_profile_image);
reportRank = itemView.findViewById(R.id.report_rank);
reportNextVacation = itemView.findViewById(R.id.next_vacation);
}
}
}
我不明白如何做到这一点。我有一个位置号码,它将显示在所有项目的顶部。
英文:
I have a recyclerview. There come data from firebase. I want to pin a item which will be show in top off all item. How can I do this.
I don’t understand how can I do this,
My Adapter Code is
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ViewHolder> {
Context context;
ArrayList<DataReturn> list;
public ReportAdapter(Context context, ArrayList<DataReturn> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ReportAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.report_user, parent, false);
return new ReportAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ReportAdapter.ViewHolder holder, int position) {
DataReturn dataReturn = list.get(position);
holder.reportId.setText(dataReturn.getFromNumber());
holder.reportName.setText(dataReturn.getName());
holder.reportRank.setText(dataReturn.getRank());
holder.reportLastVacation.setText(dataReturn.getLastVacation());
holder.reportNextVacation.setText(dataReturn.getComingEnd());
String image = dataReturn.getImage();
if (image.equals("")){
holder.circleImageView.setImageResource(R.drawable.error);
}else {
Glide.with(context).load(image).into(holder.circleImageView);
}
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView circleImageView;
TextView reportName, reportLastVacation, reportNextVacation, reportRank,reportId;
public ViewHolder(@NonNull View itemView) {
super(itemView);
reportId = itemView.findViewById(R.id.item_number);
reportName = itemView.findViewById(R.id.report_name);
reportLastVacation = itemView.findViewById(R.id.report_last_vacation);
circleImageView = itemView.findViewById(R.id.report_profile_image);
reportRank = itemView.findViewById(R.id.report_rank);
reportNextVacation = itemView.findViewById(R.id.next_vacation);
}
}
}
I don't get any idea how can I do this.
I have the position number which will be show top of the all item.
答案1
得分: 0
在onBindViewHolder
方法中,您可以为项目添加一个onClickListener
,在项目上单击时,将其从列表中移除并放置在第0个位置,使其显示在顶部,并调用notifyDatasetChanged()
。
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 处理项目单击事件
// 将单击的项目移动到列表顶部
DataReturn clickedItem = list.get(holder.getAdapterPosition());
list.remove(holder.getAdapterPosition());
list.add(0, clickedItem);
// 通知适配器数据已更改
notifyDataSetChanged();
}
});
您可以在项目视图文件中添加一个小图标作为ImageView
,该图标在其他情况下将不可见,只有当用户单击项目时才会变为可见。
英文:
In the onBindViewHolder you can add a onClickListener to the item to and onClick the item would be removed and placed add the 0th index so it appears on the top and notifyDatasetChanged().
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Handle the item click here
// Move the clicked item to the top of the list
DataReturn clickedItem = list.get(holder.getAdapterPosition());
list.remove(holder.getAdapterPosition());
list.add(0, clickedItem);
// Notify the adapter that the data has changed
notifyDataSetChanged();
}
});
And you can add a small pin icon as an Imageview in the item view file which would be invisible otherwise and only becomes visible when the user clicks on an item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论