RecyclerView里面的每个项目都有Android Switch按钮

时间:2015-08-07 19:14:41

标签: android togglebutton

我正在使用 RecyclerView并在每个项目中使用Switch组件

phaseName

我正在使用 setOnCheckedChangeListener 事件来监听状态变化

PO_VERSION_NUMBER

使用 buttonView.isPressed()是为了识别用户是否调用Switch上的 setChecked ,或者是否通过代码以编程方式调用它。

现在,当我们点击Switch时,它工作正常。

问题 - 当我们滑动Switch时,如何使其工作?在Android 4.0+开关组件提供了很好的现成切换按钮,但我需要知道用户何时滑动开关,以便我可以执行按下时执行的相同代码。也就是说,

let item = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text!

谢谢。

1 个答案:

答案 0 :(得分:0)

我知道这个问题太旧了。但是我尝试使用android.support.v7.widget.SwitchCompat它也适用于滑动。

这是我的示例代码和一些调试日志。

<android.support.v7.widget.SwitchCompat
android:id="@+id/saklar_switch"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

调试代码

Log.d("SAKLAR","Pos: "+position+" buttonView.isPressed()"+buttonView.isPressed());
Log.d("SAKLAR","Pos: "+position+" buttonView = isCheccked? "+isChecked);

调试结果

Pos: 2 buttonView.isPressed()true
Pos: 2 buttonView = isCheccked? true

isPressed()调试也会在我滑动切换按钮时返回true。