2

Actually, I have a menu from which I can add in my MainActivity buttons in a recyclerView an in that menu I'm showing all added buttons so one can delete chosen button.

But I have 4 first items of that recyclerView that should be default so no one should touch them, to manage it I've made that if on swipe position equals from 0 to 3 I will do nothing but I would hide them from that recyclerView and just show the added items.

enter image description here

Like as you can see on that drawing 1 is my MainActivity with 4 Default items and above them, I'm showing the added one, while the 2nd is my SettingsActivity where I'm adding the extra items but for now it's showing even the default that I would hide.

NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • Possible duplicate of [How RecyclerView concept works on android?](https://stackoverflow.com/questions/36042926/how-recyclerview-concept-works-on-android) – Shubham AgaRwal Oct 05 '18 at 13:34
  • just use some flag isVisible in your dataset and on the basis of that toggle visibility of each item of recycler view in bind view holder function – Shubham AgaRwal Oct 05 '18 at 13:35
  • 1
    Add and remove data from the list which you are passing to the adapter and set notifyDataSetChanged after that to reflect the result. – Kunu Oct 05 '18 at 13:37
  • @Kunu i can't delete the data because i have to keep the items position – NiceToMytyuk Oct 05 '18 at 13:38
  • @Killer that would be a cool solution i'll try it immediatly – NiceToMytyuk Oct 05 '18 at 13:39
  • in your adapter `onBindViewHolder` set a counter which will increment on every call and a condition which will check the counter if it is <= 4 in this condition set the visibility to `gone` or you can just delete the first four elements from the list which you are adding in the adapter – sourabh kaushik Oct 05 '18 at 13:40
  • @sourabhkaushik so just something like this in onBindViewHolder int i = 0; i++; if(i <= 4){ holder.box.setVisibility(View.GONE); } ? – NiceToMytyuk Oct 05 '18 at 13:44
  • 1
    @Killer that's working but there remain a white space as the items are just set to INVISIBLE ant not GONE – NiceToMytyuk Oct 05 '18 at 13:52
  • https://imgur.com/lav0UKd that's what is going on with VISIBILITY GONE, how can i even delete that white space? – NiceToMytyuk Oct 05 '18 at 13:58
  • what is the datatype Array u send to adapter and what is the data ArrayList content – Hassan Badawi Oct 05 '18 at 14:13
  • ArrayList is of tree Strings and two Int – NiceToMytyuk Oct 05 '18 at 14:18
  • @JohnKarry ArrayList hashmap can you type the datatype like this for more explain to me sir EX > ArrayList> – Hassan Badawi Oct 05 '18 at 14:21

3 Answers3

4

One way to solve this problem is to manipulate the backing array so the RecyclerView just doesn't know about the missing views. A second way is to assign a different view type with zero height to those positions that are missing. One type for visible positions and another for invisible ones. The following is an example adapter that implements this concept:

RecyclerViewAdapter.java

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> mItems;

    RecyclerViewAdapter(List<String> items) {
        mItems = items;
    }

    @Override
    public @NonNull
    RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == INVISIBLE_ITEM_TYPE) {
            // The type is invisible, so just create a zero-height Space widget to hold the position.
            view = new Space(parent.getContext());
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder vh = (ItemViewHolder) holder;
        String itemText = mItems.get(position);

        if (vh.getItemViewType() == VISIBLE_ITEM_TYPE) {
            // Only populate if a visible type.
            vh.mItemTextView.setText(itemText);
            int bgColor = (position % 2 == 0)
                ? android.R.color.holo_blue_light
                : android.R.color.holo_green_light;
            holder.itemView.setBackgroundColor(
                holder.itemView.getContext().getResources().getColor(bgColor));
        }
    }

    @Override
    public int getItemCount() {
        return (mItems == null) ? 0 : mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        // First 4 position don't show. The visibility of a position can be separately
        // determined if only, say, the first and third position should be invisible.
        return (position < 4) ? INVISIBLE_ITEM_TYPE : VISIBLE_ITEM_TYPE;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView mItemTextView;

        ItemViewHolder(View item) {
            super(item);
            mItemTextView = item.findViewById(android.R.id.text1);
        }
    }

    private final static int VISIBLE_ITEM_TYPE = 1;
    private final static int INVISIBLE_ITEM_TYPE = 2;
}

I would post a video, but it will just show the RecyclerView starting at item #4.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
0

As far as my level of understanding of your problem statement, you can use below code inside onBindViewHolder of adapter class to hide first 4 items.

Where rootView will be parent view of your recycler view item in xml.

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

    if (position <= 3) {
        holder.rootView.setVisiblity(GONE);
    }
    else {
        holder.rootView.setVisiblity(VISIBLE);
    }
}
0
before removing it save those records in another ArrayList say BackupArrayList then Remove it as below
    if (recyclerList.size() > 4) {
        for (int i = 0; i < 4; i++) {
            BackUpArrayList.add(recyclerList.get(i));
            recyclerList.remove(i);
        }
        recyclerAdapter.notifyDataSetChanged();
    }
Arwy Shelke
  • 333
  • 1
  • 2
  • 12