How To Set First 4 Items From Recyclerview To Visibility Gone?
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
Solution 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
classRecyclerViewAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> mItems;
RecyclerViewAdapter(List<String> items) {
mItems = items;
}
@Overridepublic@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 = newSpace(parent.getContext());
view.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
} else {
view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
returnnewItemViewHolder(view);
}
@OverridepublicvoidonBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ItemViewHoldervh= (ItemViewHolder) holder;
StringitemText= mItems.get(position);
if (vh.getItemViewType() == VISIBLE_ITEM_TYPE) {
// Only populate if a visible type.
vh.mItemTextView.setText(itemText);
intbgColor= (position % 2 == 0)
? android.R.color.holo_blue_light
: android.R.color.holo_green_light;
holder.itemView.setBackgroundColor(
holder.itemView.getContext().getResources().getColor(bgColor));
}
}
@OverridepublicintgetItemCount() {
return (mItems == null) ? 0 : mItems.size();
}
@OverridepublicintgetItemViewType(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;
}
staticclassItemViewHolderextendsRecyclerView.ViewHolder {
private TextView mItemTextView;
ItemViewHolder(View item) {
super(item);
mItemTextView = item.findViewById(android.R.id.text1);
}
}
privatefinalstaticintVISIBLE_ITEM_TYPE=1;
privatefinalstaticintINVISIBLE_ITEM_TYPE=2;
}
I would post a video, but it will just show the RecyclerView
starting at item #4.
Solution 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
.
@OverridepublicvoidonBindViewHolder(MyViewHolder holder, finalint position) {
if (position <= 3) {
holder.rootView.setVisiblity(GONE);
}
else {
holder.rootView.setVisiblity(VISIBLE);
}
}
Solution 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();
}
Post a Comment for "How To Set First 4 Items From Recyclerview To Visibility Gone?"