Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change recyclerview item's layout from outside?

I have a RecyclerView, which has a Button and an EditText. I have a Button outside of the RecyclerView. On click the outside Button, I want the Button in the RecyclerView toggles between hide and show. How to do that?

I have tried to send a boolean parameter mEditFlag to the recyclerAdapter, toogle mEditFlag, and notify the adapter the data has changed. but it doesn't work.

mCurrentOrderRecyclerAdapter = new CurrentOrderRecyclerAdapter(this, mEditFlag);
    rvOrder.setAdapter(mCurrentOrderRecyclerAdapter);

rlEditOrderList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mEditFlag = !mEditFlag;
            mCurrentOrderRecyclerAdapter.notifyDataSetChanged();
            if (mEditFlag) {
                ivEditOrderList.setImageResource(R.drawable.order_edit_true);
                tvEditOrderList.setText(R.string.order_edit_flag_true);
            } else {
                ivEditOrderList.setImageResource(R.drawable.order_edit_false);
                tvEditOrderList.setText(R.string.order_edit_flag_false);
            }
        }
    });

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

    if(editFlag) {
        holder.rlLeftItemRvOrder.setVisibility(View.VISIBLE);
    } else {
        holder.rlLeftItemRvOrder.setVisibility(View.GONE);
    }

    holder.tvNumItemRvOrder.setText(position + "x");
    holder.tvDesItemRvOrder.setText("holder.ivFoodItemRvOrder.setImageResource(R.drawable.drawer_menu)");
    holder.ivFoodItemRvOrder.setImageResource(R.drawable.drawer_menu);

}
like image 817
Kai Wang Avatar asked Jan 27 '16 19:01

Kai Wang


People also ask

How do you access the holder element outside the view holder in adapter Android?

getContext(); //to get the activity context use this line. MyViewHolder dataObjectHolder = new MyViewHolder(view); //in this way you can use the holder object. } private void update_data(MyViewHolder holder) { holder. edittext1.

Is there an Addheaderview equivalent for RecyclerView?

addItemDecoration(headerDecoration); The decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.

How do you load all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.


1 Answers

You have to achieve it with your adapter layer.

The Button outside the RecyclerView has to update the item inside the adapter (for example a boolean).
Then notify the change to update (for example with the notifyItemChanged method) the RecyclerView

Somenthing like:

rlEditOrderList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Update the item inside the adapter
            MyObject obj = mAdapter.getItem(position);
            obj.myBoolean= true;
            mAdapter.notifyItemChanged(position);
        }
    });

with an adapter like:

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

        private ArrayList<MyObject> mDataset;

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            //Get the item in the adapter
            MyObject obj = getItem(position);
            if(obj.myBoolean)
               holder.mButtonView.setVisibile(View.VISIBLE);
            else
               holder.mButtonView.setVisibile(View.GONE);
        }

    }
like image 103
Gabriele Mariotti Avatar answered Oct 02 '22 08:10

Gabriele Mariotti