R Besar
R Besar

Reputation: 644

Hide the view in recyclerview's item

i facing a problem in recyclerview's item.

My Adapter's code :

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Profile item = mListChatting.get(position);

        Log.d("TAG", "CEK : " + viewable);

        if(viewable==true){
    holder.mFormBookingan.setVisibility(View.GONE);
            holder.mDetailBookingan.setVisibility(View.VISIBLE);
        }else{
            //assume that one way is show first as default
             holder.mViewOneWay.setVisibility(View.VISIBLE);
             holder.mViewRoundTrip.setVisibility(View.GONE);
             holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
             holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
             holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewable = true;
                    Log.d("TAG", "CEK 2 : " + viewable);
                }
            });
}

Like my code above, I want to hide mFormBookingan after mSendBooking has pressed. mFormBookingan never show anymore until user calls it again.

I have tried with a lot of ways but still can't find like what i need. After i press mSendBooking the form hide, but when i send new item to recyclerview, the from mFormBookingan that has been hide, appears again.

My Question, how to hide mFormBookingan forever? Until user call it again.

Thank in advance, i will appreciate anyone who help me for this one.

Upvotes: 1

Views: 7955

Answers (5)

Shalauddin Ahamad Shuza
Shalauddin Ahamad Shuza

Reputation: 3657

On refresh android will destroy the view and create new view with new adapter data. So you have to track the current state (visibility) of mFormBookingan. You can use a simple visibility list. When mFormBookingan state (visibility) change update it in visibility list so that whenever the list is refreshed, you can use it to check and set the last state (visibility) of your mFormBookingan. Here is an example

private ArrayList<Boolean> isVisible;

public MyAdapter(ArrayList<Boolean> isVisible){
    // initial state list of mFormBookingan for each row of list
    this.isVisible = isVisible;
}

public void onBindViewHolder(final MyViewHolder holder, final int position) {
    if (isVisible.get(position)) {
        holder.mFormBookingan.setVisibility(View.VISIBLE);
    }else {
        holder.mFormBookingan.setVisibility(View.GONE);
    }
    holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.mFormBookingan.getVisibility() == View.GONE){
                holder.mFormBookingan.setVisibility(View.VISIBLE);
                isVisible.set(position, true);
            }else {
                holder.mFormBookingan.setVisibility(View.GONE);
                isVisible.set(position, false);
            }
        }
    });
}

when you click mSendBooking the mFormBookingan visibility will change and it will remain same after sending new item to recyclerview.

Upvotes: 0

Alok Omkar
Alok Omkar

Reputation: 628

Try this :

Create a boolean in your model class "Profile" to keep track of visibility of button : say boolean isBookingVisible;

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Profile item = mListChatting.get(position);

        if(!item.isBookingVisible){
    holder.mFormBookingan.setVisibility(View.GONE);
            holder.mDetailBookingan.setVisibility(View.VISIBLE);
        }else{
            //assume that one way is show first as default
             holder.mViewOneWay.setVisibility(View.VISIBLE);
             holder.mViewRoundTrip.setVisibility(View.GONE);
             holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
             holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
             holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    item.isBookingVisible = false;
                    //Use notiyItemChanged(position); or notifyDataSetChanged(); here as per your selection criterion
                    Log.d("TAG", "CEK 2 : " + viewable);
                }
            });
}

Upvotes: 2

Fndroid
Fndroid

Reputation: 465

onBindViewHolder will be called when you notify your data set.

So you need to save the viewable in mListChatting. When you click the button, change the viewable in the mListChatting.

And then, change the code in onBindViewHolder

holder.mFormBookingan.setVisibility(item.getViewable() ? View.VISIBLE : View.GONE);

Upvotes: 0

Cenxui
Cenxui

Reputation: 1403

I not sure what the clear situation you want. But if you want to set View invisible you can try this code then check it.

You need to add ismFormBookingVisible in viewHolder class as a boolean attribute.

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Profile item = mListChatting.get(position);

        Log.d("TAG", "CEK : " + viewable);

        if(holder.ismFormBookingVisible==true){
    holder.mFormBookingan.setVisibility(View.GONE);
            holder.mDetailBookingan.setVisibility(View.VISIBLE);
        }else{
            //assume that one way is show first as default
             holder.mViewOneWay.setVisibility(View.VISIBLE);
             holder.mViewRoundTrip.setVisibility(View.GONE);
             holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
             holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
             holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.ismFormBookingVisible = false;
                    Log.d("TAG", "CEK 2 : " + viewable);
                }

Upvotes: 2

Santoshastagi
Santoshastagi

Reputation: 186

You may have to call notifyDataSetChanged() after changing the value of viewable.

Upvotes: 0

Related Questions