开发者

add button in getView() GridView

开发者 https://www.devze.com 2023-04-11 05:07 出处:网络
I have tried to use the HelloGridView sample code. I wish to add button to each grid. From the research, it seems i have to create buttons instead of imageviews in getView adapter method.

I have tried to use the HelloGridView sample code.

I wish to add button to each grid. From the research, it seems i have to create buttons instead of imageviews in getView adapter method.

However, I have开发者_JAVA技巧 no idea how to create button in the getView() method.

Can anyone kindly show me how to create a button in the method?

thanks!


I wanted to do the same thing, so what I did was use an xml file and a layoutinflater with some code around it.

XML Containing GridView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="10dp" >
<GridView  
    android:id="@+id/gridview2"
    android:layout_width="fill_parent" 
    android:layout_height="250dp"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />
</LinearLayout>

That being my base GridView, I used an xml file for the GridView "cell" called grid_item.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button 
    android:id="@+id/GridItem_Button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

Then in your adapter.getView method

public View getView( final int position, View convertView, ViewGroup parent ) {
View mView = convertView;
if( convertView == null ) {

// I use a cursor for the content which is setup elsewhere
cursor.moveToPosition( position );
// inflate the layout to edit it
LayoutInflater li = (LayoutInflater) Context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
mView = li.inflate( R.layout.grid_item, null );
// not we can get the button defined in grid_item.xml
Button b = (Button) mView.findViewById( R.id.GridItem_Button );
// set the button text based on the cursor/your content
b.setText( cursor.getString(0);
// now we can also do an OnClickListener
b.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick( View v ) {
    // do something on button click
    }
});
}
return mView;
}

This works great for me At first I was confused on which onClickListener I should use and for buttons it works to add the onClickListener to the button, but otherwise it seems better to put the onItemClickListener to the GridView itself. If anything isn't clear please let me know.

Nick

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号