Android - 带有自定义 BaseAdapter 的 Gridview,点击查看位置 [英] Android - Gridview with Custom BaseAdapter, get clicked View at position

查看:25
本文介绍了Android - 带有自定义 BaseAdapter 的 Gridview,点击查看位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个显示字母表的网格视图.我使用自定义 BaseAdapter 用字符串数组填充 gridview.

I have created a gridview which displays the letters of the alphabet. I populate the gridview with a string array using a custom BaseAdapter.

我想要做的是能够在 GridView 的某个位置获得 Button 视图.

What i want to do is to be able to get the Button View at a position in the GridView.

例如,我希望能够从我的 gridView.setOnItemClickListener(); 设置被点击的按钮的背景颜色.

For example i want to be able from my gridView.setOnItemClickListener(); to get-set the BackgroundColor of the Button that was clicked.

到目前为止,我只能在某个位置从字符串数组中获取文本,但我不知道如何获得点击的按钮.

So far i am able to get the just the text form the string array at a position, but i don't know how i can get the clicked Button.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myGridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:numColumns="7" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/feedback"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

cell.xml

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

    <Button
        android:id="@+id/grid_item"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="05"
        android:textSize="20sp"
        android:clickable="false"
        android:focusable="false"/>
    </Button>

</LinearLayout>

主活动

public class MainActivity extends Activity {

    private TextView text;
    private GridView gridView;
    private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

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

        text = (TextView) findViewById(R.id.feedback);

        gridView = (GridView) this.findViewById(R.id.myGridView);
        CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                text.setText((String) (gridView.getItemAtPosition(position)));
                Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position)));
            }
        });

    }
}

自定义网格适配器

public class CustomGridAdapter extends BaseAdapter {

    private Context context;
    private String[] items;
    LayoutInflater inflater;

    public CustomGridAdapter(Context context, String[] items) {
        this.context = context;
        this.items = items;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cell, null);
        }
        Button button = (Button) convertView.findViewById(R.id.grid_item);
        button.setText(items[position]);

        return convertView;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return items[position];
    }

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

这是它的样子

推荐答案

我自己想出来的.我只是将我的 cell.xml 更改为:

I figured it out on my own. I just changed my cell.xml to :

<Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_item"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="05"
        android:textSize="20sp"
        android:clickable="false"
        android:focusable="false"/>
    </Button>

然后我可以通过这样做获得任何按钮:

And then i could get any button by doing this:

Button button = (Button) gridView.getChildAt(position);

这篇关于Android - 带有自定义 BaseAdapter 的 Gridview,点击查看位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆