Listview项目内的Android按钮单击无法正常工作

时间:2015-05-29 05:49:31

标签: android listview scroll click

我的应用程序主页面包含一个列表视图,每个项目都是一个高度为屏幕高度80%的容器,它包含许多按钮和图像视图。当我滚动列表视图并单击图标时,它第一次不接受点击。如何获得点击响应等ios?

这是我的适配器的getView()中的代码

 pollChoice1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.getAnswered()) {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            } else {
                message.setAnswered(true);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption1Count(message.getOption1Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }

                message.setAnswer("" + 0);

                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });


                PollReply pollReply = new PollReply(message);
                pollReply.execute();
            }

        }
    });

    pollChoice2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.getAnswered()) {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            } else {
                message.setAnswered(true);
                message.setAnswer("" + 1);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption2Count(message.getOption2Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();
            }

        }
    });

    pollChoice3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.getAnswered()) {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            } else {
                message.setAnswered(true);
                message.setAnswer("" + 2);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption3Count(message.getOption3Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();
            }

        }
    });

    pollChoice4.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.getAnswered()) {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            } else {
                message.setAnswered(true);
                message.setAnswer("" + 3);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption4Count(message.getOption4Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();

            }

        }
    });

    pollChoice5.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (message.getAnswered()) {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            } else {
                message.setAnswered(true);
                message.setAnswer("" + 4);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption5Count(message.getOption5Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();
            }

        }
    });

    starWhite1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!message.getAnswered()) {
                message.setAnswered(true);
                message.setAnswer("" + 1);
                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption1Count(message.getOption1Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();

            } else {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            }

        }
    });

    starWhite2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!message.getAnswered()) {
                message.setAnswered(true);
                message.setAnswer("" + 2);

                if ((message.getScope().equals("private") && message.getResponseCount() > 0) || message.getScope().equals("domain")) {
                    message.setOption2Count(message.getOption2Count() + 1);
                    message.setResponseCount(message.getResponseCount() + 1);
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        UpdateMessageTask updateMessageTask = new UpdateMessageTask(message);
                        updateMessageTask.execute();
                    }
                });

                PollReply pollReply = new PollReply(message);
                pollReply.execute();

            } else {
                if (message.getScope().equals("private") && (message.getResponseCount() < 1) && message.getVisibility().equals("public")) {
                    showAlert("Not enough people have responded to display answers");
                } else {
                    showAlert("You have already answered this poll");
                }
            }

        }
    });

这是我使用的ListView:

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@android:color/transparent"
    android:scrollbarSize="3dp"
    android:scrollbarStyle="outsideOverlay"
    android:scrollingCache="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

1 个答案:

答案 0 :(得分:0)

尝试添加

 android:focusable="false"

到ListViewItems的xml

编辑:示例ListViewItem xml wit按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tree_item_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/transparent_withline"
android:gravity="left|center_vertical"
android:orientation="horizontal"
android:focusable="false">

<ImageView
        android:id="@+id/tree_item_toggle"
        android:layout_width="24dip"
        android:layout_height="24dip"
        android:focusableInTouchMode="false"
        android:src="@drawable/closed"/>

<ImageView
    android:id="@+id/tree_item_status"
    android:layout_width="24dip"
    android:layout_height="24dip"
    android:layout_gravity="center_vertical"
    android:src="@drawable/icon12"/>
<LinearLayout
        android:id="@+id/tree_item_icocontainer"
        android:layout_width="24dip"
        android:layout_height="48dip"
        android:orientation="vertical" >

    <ImageView
            android:id="@+id/tree_item_docs"
            android:layout_width="24dip"
            android:layout_height="24dip"
            android:layout_gravity="center_vertical"
            android:background="@drawable/has_docs" />
    <ImageView
            android:id="@+id/tree_item_coord"
            android:layout_width="24dip"
            android:layout_height="24dip"
            android:layout_gravity="center_vertical"
            android:background="@drawable/has_location" />
</LinearLayout>
<LinearLayout
        android:id="@+id/tree_item_textcontainer"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_marginLeft="6dip"
        android:layout_height="wrap_content">
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" android:layout_gravity="right"/>
</LinearLayout>