How to set first 4 items from recyclerView to visibility gone?

半世苍凉 提交于 2020-01-05 02:15:32

问题


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.

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.


回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/52666801/how-to-set-first-4-items-from-recyclerview-to-visibility-gone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!