Android 如何添加 gridview setOnItemClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20191914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:26:31  来源:igfitidea点击:

How to add gridview setOnItemClickListener

androidandroid-gridview

提问by Batuhan Co?kun

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.

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

My gridview children which has a button on it(gridview_members.xml);

我的 gridview 子项上有一个按钮(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\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

My ImageAdapter class;

我的 ImageAdapter 类;

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;
}

}

Gridview implementation on activity_main.xml ;

Gridview 在 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>

And finally my MainActivity.java file;

最后是我的 MainActivity.java 文件;

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();
        }
    });
}
}

采纳答案by Hyman K Fouani

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

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

from

<?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\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

to

<?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\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

or you can add ClickListener inside adapter

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

@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;
}

回答by Mohammed Saleem

GridView is clickable no need to put button inside GridView.

GridView 是可点击的,无需在 GridView 中放置按钮。

To add listener to the grid add the following code:

要将侦听器添加到网格,请添加以下代码:

// Implement On Item click listener
gridView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0: 
                break;
            case 1:
                break;

        }
    }
}); 

And so on you can add all numbers in the cases.

依此类推,您可以在案例中添加所有数字。

回答by E J Chathuranga

If you want to get text that clicked item you can use this

如果你想获得点击项目的文本,你可以使用这个

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String item = ((TextView)view.findViewById(R.id.ID_OF_TEXTVIEW)).getText().toString();
                Toast.makeText(showMissions.this, "" + item, Toast.LENGTH_SHORT).show();

            }
        });

回答by DaVin.Cinmay

You can add click listener this way to your grid view items in Kotlin

您可以通过这种方式将点击侦听器添加到 Kotlin 中的网格视图项中

grdlyt_merchant_color_code.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            loyaltyColor = parent.getItemAtPosition(position) as LoyaltyColor?
            colorCode= LoyaltyUtils.getLoyaltyColorById(loyaltyColor!!.colorId).colorCode
            val color = ContextCompat.getColor(requireContext(), colorCode!!)
            shape.setColor(color)
            loyalty_card_img.background = shape
            uncheckAllColors()
            loyalty_card_img.visibility = View.GONE
            loyaltyColor!!.isColorSelected = true

            mAdapter.notifyDataSetChanged()
        }

回答by Dilip Malviya

I am suggesting you to add click listener in getview method rather than adding in activity class. try this code

我建议您在 getview 方法中添加点击侦听器,而不是在活动类中添加。试试这个代码

@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 OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
            //Do your task here
        }
    });
    return gridView;
}

回答by Apple Appala

In your CustomAdapter into

在你的 CustomAdapter 中

 onBindViewHolder(...
{
  yourViewHolder.gridview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        //Do Something
       }
    });

}

}