【问题标题】:Android Listview button doesn't workAndroid Listview 按钮不起作用
【发布时间】:2015-10-24 07:12:07
【问题描述】:

我使用基本适配器创建了一个自定义列表视图,但由于某种原因无法单击单个列表视图内的按钮。

这是主要布局的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Light List"
    android:id="@+id/lightName"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/loginbutton"
    android:id="@+id/button"
    android:onClick="buttonClicked"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/lightName"
    android:layout_alignStart="@+id/lightName" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lightList"
    android:layout_below="@+id/lightName"
    android:layout_above="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

</RelativeLayout>

这是列表中视图的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lightList"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/lightName"
     />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/statusText"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/buttonoff"
    android:focusable="true"
    android:enabled="true"
    android:clickable="true" />

</LinearLayout>

最后是适配器代码:

public ArrayList<myLight> dataSet = new ArrayList<myLight>();

        public void makeLightList(ArrayList<myLight> lights) {
            Iterator<myLight> iter = lights.iterator();
            while(iter.hasNext()) {
                dataSet.add(iter.next());
            }
        }

        @Override
        public int getCount() {
            return dataSet.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v;
            LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.lightlistlayout, null);

            TextView name = (TextView) findViewById(R.id.lightName);
            name.setText(dataSet.get(position).name);

            TextView status = (TextView) findViewById(R.id.statusText);
            if(dataSet.get(position).reachable) {
                if(dataSet.get(position).isOn) {
                    status.setText("Light is On");
                } else {
                    status.setText("Light is Off");
                }
            } else {
                status.setText("Light is currently not reachable");
            }

            dataHolder dh = new dataHolder();
            dh.name = dataSet.get(position).name;

            //Handle Off button click from list view
            Button offButton = (Button) findViewById(R.id.buttonoff);
            offButton.setTag(dh);
            offButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dataHolder dhv = new dataHolder();
                    dhv = (dataHolder)v.getTag();
                    Toast.makeText(getBaseContext(), dhv.name + " Turned Off", Toast.LENGTH_LONG).show();
                }
            });


            return v;
        }
    };

我不确定我的 onclicklistener 是否正确,因为我还不能点击按钮。这是显示按钮的图片。我在 xml 文件中弄乱了不同的焦点和可点击设置,但没有运气。我添加了一个搜索栏,我可以来回滑动它,但是按钮不允许我点击它。

【问题讨论】:

  • 删除android:focusable="true"
  • 我刚刚尝试过,并尝试将其设置为 false。虽然 cdlimg 在发布之前进行搜索,但大多数人在选择与按钮分开的行时遇到问题。这就是为什么我将它设置为 true 以使按钮成为焦点,但这不起作用。我在想我在某个地方遗漏了一些非常简单的东西,但不知道是什么。可能在父视图中,但正如我在问题中提到的,当我在视图中有一个搜索栏时,我能够选择它,所以我真的很茫然。

标签: android listview button baseadapter


【解决方案1】:
v = inflater.inflate(R.layout.lightlistlayout, null);

这里你已经为你的适配器膨胀了行文件,下面是你的 id 初始化,并且你没有将你的视图添加到你的 id 中,所以试试这个。

TextView name = (TextView) v.findViewById(R.id.lightName);
TextView status = (TextView) v.findViewById(R.id.statusText);
Button offButton = (Button) v.findViewById(R.id.buttonoff);

【讨论】:

  • 我相信我在适配器代码的 getview 中这样做了。
  • 嗯....如果这个答案对你有帮助,请投票,这样更多的人会明白这确实有效。祝你好运。
  • 我已经在原始代码中这样做了,但它似乎不起作用。和其他想法?
  • 嘿,我以为你在代码中添加了两次视图持有者,这意味着在按钮点击事件中你添加了相同的视图持有者代码,这将是男孩所需要的。
【解决方案2】:

尝试改变

android:focusable="true" to android:focusable="false" in Button's xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多