【问题标题】:adding listener to custom listview containing CheckedTextView and button将侦听器添加到包含 CheckedTextView 和按钮的自定义列表视图
【发布时间】:2017-08-15 13:41:14
【问题描述】:

这是我的行布局(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);
            }
        });

这就是结果,

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

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

【问题讨论】:

    标签: java android android-studio listener


    【解决方案1】:

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

    CheckedTextViewviewButton 在 rowlayout.xml 中都是可聚焦的,这会阻止 ListView 项目获得焦点。

    更多信息请参考android:descendantFocusability

    【讨论】:

    • 感谢您的回答我添加了一个日志来查看我的结果 Log.d("workconditions", "stringSelectedItems:"+stringSelectedItems);可以选择结果,但在布局中,drawable 未选中
    • @testerNew 你在问另一个问题。无论如何,这让我很困惑,你真正想点击哪个 itemCheckedTextView,或包含 CheckedTextViewButton 的列表视图项?如果要响应 CheckedTextView 上的点击事件,则必须在其上指定点击监听器。但是我没有在你的代码中找到这样的逻辑。
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多