问题描述
我有一个带有两个部分regular
和Favourites
的recyclerView,并且为了填充我的RecyclerView
我有List<Object>
我的object
类:
i have a recyclerView with two sections regular
and Favourites
and for populating my RecyclerView
i have List<Object>
my object
class :
public class Object {
String id,channelName;
boolean isFavorite;
}
现在我想将具有isfavorite == true
的值放在Favourite
部分中的recyclerView
顶部,但是我不知道从哪里开始或确切地做什么?我必须用布尔值对List<Object>
进行排序吗?如果有人可以给我一些提示或指导,那对我会很有帮助,
now i want to put the values which have isfavorite == true
to top of my recyclerView
inside my Favourite
section but i don't know where to start or exactly what to do ? do i have to sort the List<Object>
with booleans ? if anybody can give me a little hint or guidance then it'll be so helpful for me,
请提供此图片以便更好地理解,这就是我想要得到的
please this image for better understanding ,this is what i'm trying to get
我通过RecyclerView中添加了该部分; rel ="nofollow noreferrer"> SimpleSectionedRecyclerViewAdapter
i added the Section in my RecyclerView
by SimpleSectionedRecyclerViewAdapter
推荐答案
class DataModal
{
public String title;
public boolean isFavourite;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isFavourite() {
return isFavourite;
}
public void setIsFavourite(boolean isFavourite) {
this.isFavourite = isFavourite;
}
}
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {
private final Context mContext;
private List<DataModal> mData;
public void add(DataModal s,int position) {
position = position == -1 ? getItemCount() : position;
mData.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData.remove(position);
notifyItemRemoved(position);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public SimpleViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.simple_text);
}
}
public SimpleAdapter(Context context, ArrayList<DataModal> data) {
mContext = context;
if (data != null)
mData = data;
else mData = new ArrayList<DataModal>();
}
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.simple_item, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
DataModal data = mData.get(position);
holder.title.setText(data.getTitle());
holder.title.setOnClickListener(new Listener(position,mData.get(position)));
}
class Listener implements View.OnClickListener
{
DataModal Data;
int position;
Listener(int position,DataModal Data)
{
this.Data = Data;
this.position = position;
}
@Override
public void onClick(View view) {
if(Data.isFavourite())
{
//mark the view as unfavorite
}
else{
//mark the view as favorite
}
Toast.makeText(mContext,"Position ="+position,Toast.LENGTH_SHORT).show();
}
}
@Override
public int getItemCount() {
return mData.size();
}
}
这篇关于实施“添加到收藏夹" RecyclerView中的机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!