简体   繁体   中英

How to pass a boolean from fragment to adapter

I have this fragment in which I have a switch. What I want is that, when the switch's state is set to false, the items in the recycler view I have change color. The thing is, I am not sure how to pass the boolean from my fragment to my adapter. I was trying to do so through an interface but it doesn't seem to be working, the color is changed all the time, no matter the state of the switch.

This is the part in the fragment:

private Boolean switchState = false;

@Override
public boolean getClicked() {
    if(switchState) {
        return false;
    }
    return true;
}

The part in the adapter (after passing the interface to the class and everything):

interface ClickHere {
    val clicked: Boolean
}
//inside the viewholder that has the bind function
 if(clickHere.clicked){
    faceImage.alpha = 0.5F                     
    faceName.setTextColor(resources.getColor(R.color.disabled))
    } else {
      faceImage.alpha = 1F
      faceName.setTextColor(resources.getColor(R.color.content_1))
}

So far, the recycler seems to be functioning as if the clickHere.clicked is always true, no matter the switch. How can I make it listen to the boolean from my fragment?

Alternative with variable as suggested in comment but still not working: Inside ViewHolder class and binding function.

var adapterVariable = myAdapter(listener,displayMetrics)
if(adapterVariable.colored{    
    faceImage.alpha = 0.5F                     
    faceName.setTextColor(resources.getColor(R.color.disabled))
    } else {
      faceImage.alpha = 1F
      faceName.setTextColor(resources.getColor(R.color.content_1))
}

In fragment:

myAdapter adapter = new myAdapter(this, getContext().getResources().getDisplayMetrics());
binding.facialSwitch.setOnClickListener(view -> {
    if(switchState){
        turnSwitchOff();
        adapter.setColored(switchState);
    } else {
        turnSwitchOn();
        adapter.setColored(false);
    }
});

Inside of your adapter, you can instantiate a private boolean variable with a matching getter and setter function. This variable is then used inside your viewhholder creation. Make sure, that you're calling notifyDataSetChanged in your setter method. Otherwise, your items will not get redrawn and therefore will not update their color.

You can do that with:

var colored = false
    set(value) {
        field = value
        this.notifyDataSetChanged()
    }

After adding these, add a listener to your switch, which calls the adapters new set function. Make sure you save your adapter as a variable inside your fragment.

This is achieved with:

var adapter: MyAdapter // create in onCreateView, save in your fragment
var switch: Switch // get from your UI
switch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener{ _, isChecked ->
        adapter.colored = isChecked})

By that, there is no interface needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM