onCheckedChanged自动调用 [英] onCheckedChanged called automatically
问题描述
我在recyclerview中有一个开关,并且从DB检索数据后,数据在recyclerview中显示。打开recyclerview时,我读取了DB,并且如果DB中的字段为 Y,则启用该开关,否则将禁用该开关。
现在问题也随之出现,也调用了onCheckedchanged侦听器,我希望仅当用户手动设置开关时才调用onCheckedChanged。
I have a switch in a recyclerview and data is displayed in the recyclerview after retrieving data from DB. When the recyclerview is opened I read DB and if a field in DB is "Y" I enable the switch or else I disable the switch. Now the problem is along with it the onCheckedchanged listener is also called, I want the onCheckedChanged to be called only when user sets the switch manually.
打开时执行以下recyclerview:
On opening the recyclerview below is executed:
holder.enabledisable.setChecked(messengerRecord.get_is_valid().equalsIgnoreCase("Y"));
ViewHolder类:
ViewHolder class:
public class viewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
public SwitchCompat enabledisable;
public viewHolder(View v) {
enabledisable = (SwitchCompat) v.findViewById(R.id.enabledisable);
enabledisable.setOnCheckedChangeListener(this);
...................................
...................................
OncheckedChanged方法,当刚打开recyclerView时将调用它:
OncheckedChanged method which is called when the recyclerView is just opened:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v("ranjith","called oncheckedchanged");
MessengerRecord rec;
rec = dbHelper.getRecord(descview.getText().toString());
switch (buttonView.getId()) {
case R.id.enabledisable:
if (isChecked) {
rec.set_is_valid("Y");
dbHelper.updateRecord(rec);
}
}
在布局文件中:
<android.support.v7.widget.SwitchCompat
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:id="@+id/enabledisable"
android:layout_alignRight="@+id/textview_to"
android:layout_alignEnd="@+id/textview_to"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
推荐答案
奇怪的是我们所有人都有这个问题,但不是官方的Google回答了这个简单的问题。
It's weird all of us have this problem but not official Google answer to this simple problem.
最简单的检查方法是:
buttonView.isPressed()
如果为true,则表示用户单击了视图。
If true, means the user clicked the view.
不需要全局变量。
希望这会有所帮助。
这篇关于onCheckedChanged自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!