0

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

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 please this image for better understanding of what i want

i added the Section in my RecyclerView by SimpleSectionedRecyclerViewAdapter

remy boys
  • 2,928
  • 5
  • 36
  • 65
  • you can check [this example in GitHub](https://github.com/thedeveloperworldisyours/FullRecyclerView/tree/master/app/src/main/java/com/thedeveloperworldisyours/fullrecycleview/addfavorites) with regular and favourites sections. – Cabezas Sep 06 '17 at 16:52

2 Answers2

3
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();
    }
}
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
2
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {

    private final Context mContext;
    private List<String> mData;

    public void add(String 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, String[] data) {
        mContext = context;
        if (data != null)
            mData = new ArrayList<String>(Arrays.asList(data));
        else mData = new ArrayList<String>();
    }

    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) {
        holder.title.setText(mData.get(position));
        holder.title.setOnClickListener(new Listener(position,mData.get(position),false,favview));
    }

    class Listener implements View.OnClickListener
    {
    ImageView favoriteView;
    int position;
    Listener(int position,String Data,boolean isFavourite,ImageView favoriteView)
    {
    this.favoriteView = favoriteView;
    this.position = position;
    }
     @Override
            public void onClick(View view) {

            if(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();
    }
}
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31