将侦听器添加到包含CheckedTextView和按钮的自定义列表视图

时间:2017-08-15 13:41:14

标签: java android android-studio listener

这是我的行布局(rowlayout.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/checkedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"

        android:gravity="center_vertical"
        android:paddingRight="16dp"
        android:text="CheckedTextView" />

    <Button
        android:id="@+id/telque"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@id/checkedTextView"
        android:text="telque" />


</RelativeLayout>

这是我的主要布局(activity_work_condition.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.doctorbeingwell.doctorbeingwell.createfilepack.WorkConditions">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:onClick="passconditions"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        android:id="@+id/linearLayout">

        <ListView
            android:id="@+id/checkabalelist"
            android:layout_width="match_parent"
            android:layout_height="491dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_weight="0.2"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

这是我的班级(workconditions.java):

public class WorkConditions extends Activity {
  ArrayList<String> selectedItems = new ArrayList<>();
    ListView checkablelist;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_condition);


        checkablelist = findViewById(R.id.checkabalelist);
        checkablelist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        String[] items = {"étudiant",
                "chomeur",
                "retraité",
                "travail à domicile",
                "travail au bureau"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.checkedTextView, items);
        checkablelist.setAdapter(adapter);
        CheckedTextView checkedTextView= findViewById(R.id.checkedTextView);
        checkablelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                String selectedItem = ((CheckedTextView) view).getText().toString();
                if (selectedItems.contains(selectedItem)) {
                    selectedItems.remove(selectedItem);
                } else selectedItems.add(selectedItem);
            }
        });

这是结果,

result

但这些项目无法点击,我无法使用此代码检索数据

String selectedItem = ((CheckedTextView) view).getText().toString();

1 个答案:

答案 0 :(得分:0)

在rowlayout.xml中,将 android:descendantFocusability =&#34; blocksDescendants&#34; 添加到根节点(在您的情况下为RelativeLayout)。

rowlayout.xml中的

CheckedTextViewview 按钮都是可聚焦的,这会阻止ListView项获得焦点。

有关详情,请参阅android:descendantFocusability