【问题标题】:Implement a ListView unclickable with two clickable buttons on every Item实现一个 ListView 不可点击,每个项目上有两个可点击的按钮
【发布时间】:2013-08-31 14:31:46
【问题描述】:

我正在尝试用两个按钮制作一个 ListView,当我点击任何按钮时我需要知道列表的位置,但我需要让 Item 变得不可点击!

有没有人可以帮助我?

这是我的 item.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/Clear">

    <ImageView
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/Clear"
        android:layout_centerHorizontal="true"
        android:id="@+id/imgProducte"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nomPreuQuantitat"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_below="@id/imgProducte"
        android:layout_marginBottom="3dp"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:gravity="center"
        android:text="test1"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nomPreuQuantitat"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="3dp"
        android:textSize="16sp"
        android:layout_centerHorizontal="true"
        android:id="@+id/descripcio"
        android:gravity="center"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_below="@id/descripcio">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/btn_menys"
            android:background="@drawable/b_minus"
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginBottom="10dp"
            android:id="@+id/quantitat"
            android:background="@drawable/b_buit"
            android:gravity="center"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="10dp"
            android:onClick=""
            android:id="@+id/btn_mes"
            android:background="@drawable/b_plus"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"/>
    </LinearLayout>
</RelativeLayout>

我的 java 类是:

lstProductes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),""+view.isSelected(),Toast.LENGTH_SHORT).show();
            try {
                final Button botoAfegir = (Button) findViewById(R.id.btn_mes);
                final Button botoRestar = (Button) findViewById(R.id.btn_menys);
                final String concepte = arraiProductes.get(position).getName();

                botoAfegir.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Afegir " + concepte, Toast.LENGTH_SHORT).show();
                    }
                });
                botoRestar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Restar " + concepte, Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception exc) {
                Toast.makeText(getApplicationContext(), exc.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });

【问题讨论】:

  • 您能否详细说明,但我需要该项目变得不可点击!
  • 在自定义的getView 中定义按钮点击监听器
  • 如果您的项目不应该是可点击的,为什么还要有一个 onItemClickListener ?
  • 我实现onItemClickListener是为了知道项目的位置,但我认为你是对的,我要定义ClickListener,有什么想法可以知道选中的行吗?
  • 非常感谢!!在getView上定义按钮后,问题解决了!!

标签: java android xml listview button


【解决方案1】:

制作你在Adapter的getView中提到的Layout(在Adapter的getView中convertView),当点击layout中的按钮时将setClickable设置为false

并在Adapter的getView中提及点击监听

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多