Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the holder element outside view Holder in adapter android?

I have a ViewHolder Class.

class VHHeader extends RecyclerView.ViewHolder{
        TextView txtTitle;
        RecyclerView recyclerView;

        public VHHeader(View itemView) {
            super(itemView);
            this.txtTitle = (TextView)itemView.findViewById(R.id.txtHeader);
            this.recyclerView =(RecyclerView)itemView.findViewById(R.id.recyclerView);


        }
    }

Now I want to access this View Holders recycler view in some different method in same adapter class .

Public void useRecyclerView(){
}

How to use the element of View Holder in this method.

like image 635
stack Learner Avatar asked Aug 25 '16 11:08

stack Learner


2 Answers

I think you want to use your view holder elements in some of the methods with in same adapter. so for that you need to pass the view holder and position in the function in which you can use your view holder elements.

Example.

@Override
            public void onBindViewHolder(final AdapterName.ViewHolder holder, final int position) {

                holder.elementname.setText(arrayList.get(position).getYourMethodName());


                holder.elementname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                yourMethodName(holder,position);

                }
            });
        }



    //and in that method    you can use your view holder elements.

    private void yourMethodName(final AdapterName.ViewHolder holder,final int position)
    {
         holder.elementname.setText(arrayList.get(position).getYourMethodName());
    }       


//This way you can use your elements 
like image 149
Ketan Ramani Avatar answered Oct 08 '22 20:10

Ketan Ramani


As i understand, you want to make some changes on item click of recyclerview. You don't need to store your viewholder. You can set clicklistener on adapter's onBindViewHolder method like:

@Override
public void onBindViewHolder(final VHHeader holder, final int position) {
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do your operations here like
            holder.txtTitle.setText("new title");
        }
    });
}

Good luck.

like image 40
Batuhan Coşkun Avatar answered Oct 08 '22 19:10

Batuhan Coşkun