setOnItemClickListener

setOnItemClickListener

本文介绍了如何添加 gridview setOnItemClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 81 个按钮的 GridView.我想将 clicklistener 添加到此 gridview,但它不可用.我添加了 OnItemClickListener 但它不起作用,我不明白为什么.代码没有错误.唯一不起作用的是 OnItemClickListener.

I have a GridView with 81 buttons on it. I want to add clicklistener to this gridview but it is not available. I have added the OnItemClickListener but it is not working and I cannot understand why. There is no error with the code. The only thing not working is the OnItemClickListener.

我的 gridview 孩子,上面有一个按钮(gridview_members.xml);

My gridview children which has a button on it(gridview_members.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="match_parent"
>

<Button
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:textStyle="bold"
    android:text="Code

City"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

我的 ImageAdapter 类;

My ImageAdapter class;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

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

public int getCount() {
    return 81;
}

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

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

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

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    gridView =new View(mContext);

    gridView = inflater.inflate(R.layout.gridview_members, null);

    Button city = (Button) gridView.findViewById(R.id.city);

    return gridView;
}

}

Activity_main.xml 上的 Gridview 实现;

Gridview implementation on activity_main.xml ;

<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="6" >
</GridView>

最后是我的 MainActivity.java 文件;

And finally my MainActivity.java file;

public class MainActivity extends Activity{

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

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}
}

推荐答案

您正在请求单击项目(而不是项目内的按钮),因此需要更改您的子 XML 布局

You are requesting click on the item(not on the button inside the item) so need to change your child XML layout

来自

<?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="match_parent"
>

<Button
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:textStyle="bold"
    android:text="Code

City"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:clickable="true"
    android:textStyle="bold"
    android:text="Code

City"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

或者您可以在适配器内添加 ClickListener

or you can add ClickListener inside adapter

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

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    gridView =new View(mContext);

    gridView = inflater.inflate(R.layout.gridview_members, null);

    Button city = (Button) gridView.findViewById(R.id.city);

                city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //Toast here

        }});

    return gridView;
}

这篇关于如何添加 gridview setOnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:14