GridView不响应onClickListener

GridView不响应onClickListener

本文介绍了GridView不响应onClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用googleDevelopers示例,我使用按钮创建gridView,Everythink正常运行,但是当我创建TOAST向我展示单击位置时,没有任何消息.你们可以看一下代码吗,也许我做错了

going with googleDevelopers example i create gridView with buttons, Everythink working, but when i create TOAST to show me the clicked position im no get any message. Can u guys look at code, maybe i do something wrong

这是一个xml文件:

 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/pick_currency"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:clickable="true"
        />

我在活动中配置gridView,当然我在onCreate中调用此方法:

Here i configuring the gridView in activity, of course i call this method in onCreate:

 private void configureGridViewCurrencySelect() {
        gridViewCurrency.setAdapter(new ButtonAdapter(this));
        gridViewCurrency.setNumColumns(4);

        gridViewCurrency.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), "klick " + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

这是我的ButtonAdapter:

And here is my ButtonAdapter:

 public class ButtonAdapter extends BaseAdapter {
    private Context mContext;

    public ButtonAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        Button buttonView;
        if (convertView == null) {
            buttonView = new Button(mContext);
            buttonView.setLayoutParams(new GridView.LayoutParams(220, 160));
            buttonView.setPadding(8, 20, 8, 8);
        } else {
            buttonView = (Button) convertView;
        }

        buttonView.setText(mThumbIds[position]);
        return buttonView;
    }

    private String[] mThumbIds = {"EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",
            "EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",};
}

推荐答案

您正在将clickable(Button)项目用作GridView适配器中的行项目,这就是您未调用onItemClick的原因.

You are using clickable(Button) item as row item in GridView Adapter, that is the reason your are not getting onItemClick called.

将您的按钮设置为clickable false,然后尝试使用GridView onItemClick

set your button clickable false and then try for the GridView onItemClick

这是ListView和GridView的常见问题,如果您将任何可单击的控件用作行项目,则将不会执行onItemClick.

This is common issue for ListView and GridView, if you have used any clickable control as a row item then your onItemClick will not be performed.

OR

在您的根布局中应用以下属性,其中在布局xml中声明了GridView/ListView

Apply below attribute in your root layout in which GridView/ListView declared in layout xml

android:descendantFocusability="blocksDescendants"

还将以下内容应用于行xml中的可点击控件(如果有的话)

Also apply below to your clickable control in row xml if any

android:focusable="false"
android:focusableInTouchMode="false"

OR

在您的情况下,您将以编程方式将按钮添加到GridView适配器中,因此,按钮覆盖了GridView单元的整个区域,因此,上述两种解决方案都无法解决您的问题,因此您需要更新代码,如下所示:如下所示.

In your case you are adding button programatically into GridView adapter so, button is covering the whole area of a cell of GridView and because of this you will not have your problem resolved by above two solutions, you need to update your code as shown below.

为GridView行创建新的布局xml

Create new layout xml for GridView row

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

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

</LinearLayout>

这是你的活动

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();
    private GridView gridViewCurrency;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridViewCurrency = (GridView) findViewById(R.id.gridview);

        configureGridViewCurrencySelect();
    }

    private void configureGridViewCurrencySelect() {
        gridViewCurrency.setAdapter(new ButtonAdapter(this));

        gridViewCurrency.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), "klick " + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ButtonAdapter extends BaseAdapter {
        private Context mContext;

        public ButtonAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {

            Holder holder = null;


            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.row_button, null);
                holder.button = (Button) convertView.findViewById(R.id.button);

                convertView.setTag(holder);

            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.button.setText(mThumbIds[position]);
            return convertView;
        }

        public class Holder {
            private Button button;

        }

        private String[] mThumbIds = {"EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",
                "EUR/USD", "EUR/USD", "EUR/USD", "EUR/USD",};
    }
}

这是声明了GridView的布局xml

This is your layout xml having GridView declared

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/activity_currency_select"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants"
    >

    <TextView
        android:id="@+id/pick_currency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Pick Currency"
        android:textColor="@android:color/black"
        android:textSize="40dp"/>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/pick_currency"
        android:layout_marginTop="50dp"
        android:background="@color/colorAccent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"/>
</RelativeLayout>

这篇关于GridView不响应onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:47