Android - Gridview with Custom BaseAdapter,点击查看位置

时间:2013-11-21 18:41:12

标签: android gridview baseadapter

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

我想要做的是能够在GridView中的某个位置获取Button视图。

例如,我希望能够从我的gridView.setOnItemClickListener();获取已点击的Button的BackgroundColor。

到目前为止,我能够在一个位置获得字符串数组的文本,但我不知道如何获得点击的按钮。

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>

MainActivity

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

    }
}

CustomGridAdapter

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

以下是它的外观

enter image description here

3 个答案:

答案 0 :(得分:7)

我自己想通了。我刚刚将cell.xml更改为:

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

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

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

答案 1 :(得分:2)

1)如果你只需要更改bg颜色或其他颜色 - 你最好使用选择器。

2)在你的例子中,你不必膨胀xml文件,然后找到按钮,你可以动态创建一个按钮,然后直接返回这个按钮。

3)如果你不得不夸大复杂的视图 - 你在onItemClickListener中你可以通过adapter \ gridview.getItem获取你的视图然后,当你收到你的视图时,可以执行findViewById并找到所有必要的视图。

答案 2 :(得分:1)

在onItemClick()

gridAdapter.itemSelected = position;
CustomGridAdapter中的

public int itemSelected = -1;

getView(..)

    TextView tv = (TextView) convertView.findViewById(R.id.feedback);
    if (itemSelected == position) {            
        tv.setBackgroundColor(int selectedcolor);
    } else {
        tv.setBackgroundColor(int unselectcolor);
    }