Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: set fixed item on RecyclerView

I have a RecyclerView, when I click in the first view it adds another view like in the image, what I want is to set the "add" view which ID is "1" to be fixed in the last position of the recycler instead in the first. enter image description here

My adapter:

public class AddEventsAdapter extends RecyclerView.Adapter<AddEventsAdapter.ViewHolder> {

   private List<String> items = new ArrayList<>();

    public void addItem(String name) {
        items.add(name);
        notifyItemInserted(items.size() - 1);
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, items.size());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.add_event_item, parent, false);

        return new ViewHolder(view);
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setEventNameName(i + "");
        if(position == 0) 
        {
            holder.theLayout.setBackgroundColor(Color.parseColor("#7F9099"));
            holder.eventName.setText("Add");
        }

    }
    static int i;
    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView eventName;
        public RelativeLayout theLayout;




        public ViewHolder(final View itemView) {
            super(itemView);
            eventName = (TextView)itemView.findViewById(R.id.eventName);
            theLayout = (RelativeLayout)itemView.findViewById(R.id.backgroundevent);

            theLayout.setId(++i);

            theLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getAdapterPosition()>0){
                        removeItem(getAdapterPosition());
                    }else {
                        addItem("");
                    }
                }
            });

        }

        public void setEventNameName(String TheEventName){
            eventName.setText(TheEventName);
        }
    }
}

In the activity:

final AddEventsAdapter AddContainer = new AddEventsAdapter();
    AddEventsRecycler.setLayoutManager(new LinearLayoutManager(this));
    AddEventsRecycler.setAdapter(AddContainer);
    AddEventsRecycler.setItemViewCacheSize(666);

    RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
    itemAnimator.setAddDuration(1000);
    itemAnimator.setRemoveDuration(1000);
    AddEventsRecycler.setItemAnimator(itemAnimator);

    AddContainer.addItem("");
like image 891
John Sardinha Avatar asked Apr 16 '16 22:04

John Sardinha


People also ask

How do I set fixed size to RecyclerView?

You can now use android:layout_height="wrap_content" on a RecyclerView, which, among other things, allows a CollapsingToolbarLayout to know it should not collapse when the RecyclerView is empty. This only works when you use setHasFixedSize(false) on the RecylcerView.

What is setHasStableIds?

setHasStableIds(true) :- set setHasStableIds(true); in RecyclerView. Adapter . true means this adapter would publish a unique value as a key for item in data set. Adapter can use the key to indicate they are the same one or not after notifying data changed.

What is RecyclerView in Android with example?

RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.


2 Answers

I solved it by creating a custom adapter that allows a footer and a header, here is the project in GitHub: https://github.com/u3breeze/android-RecyclerView-WithHeaderAndFooter

like image 116
John Sardinha Avatar answered Oct 06 '22 07:10

John Sardinha


Did you try something like:

 public void addItem(String name) {
        if (items.size() != 0) {
            removeItem(items.size() - 1);
            items.add(name);
            items.add("Add");
        } else
            items.add("Add");

        notifyItemInserted(items.size() - 1);
    }

then id of Add item is items.size() -1

edit: Change if in your Holder class with following code:

     if(position == items.size() - 1) 
   {      
         holder.theLayout.setBackgroundColor(Color.parseColor("#7F9099"));
                    holder.eventName.setText("Add");
                }
like image 44
MilanNz Avatar answered Oct 06 '22 07:10

MilanNz