Android - Gridview with Custom BaseAdapter, get clicked View at position

主宰稳场 提交于 2019-11-29 07:54:57

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

1) If you need only to change bg color or smth else - you'd better use selectors.

2) In you example you don't have to inflate the xml file and then find button, you can create a button dynamically and then return this button directly.

3) If you have to inflate complicated view - in you onItemClickListener you can get your view by adapter\gridview.getItem and then, when you receive your view it's possible to perform findViewById and find all necessary views.

In onItemClick()

gridAdapter.itemSelected = position;

in 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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!