Android - 带有自定义 BaseAdapter 的 Gridview,在位置点击查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20129337/
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
Android - Gridview with Custom BaseAdapter, get clicked View at position
提问by Christos Baziotis
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 来显示字母表中的字母。我使用自定义 BaseAdapter 用字符串数组填充 gridview。
What i want to do is to be able to get the Button
View at a position in the GridView.
我想要做的是能够Button
在 GridView 中的某个位置获取视图。
For example i want to be able from my gridView.setOnItemClickListener();
to get-set the BackgroundColor of the Button that was clicked.
例如,我希望能够从我gridView.setOnItemClickListener();
的设置中设置单击的按钮的背景颜色。
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
活动_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
<?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;
}
}
Here is how it looks
这是它的外观
回答by Christos Baziotis
I figured it out on my own. I just changed my cell.xml to :
我自己想出来的。我刚刚将 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>
And then i could get any button by doing this:
然后我可以通过这样做获得任何按钮:
Button button = (Button) gridView.getChildAt(position);
回答by dilix
1) If you need only to change bg color or smth else - you'd better use selectors.
1)如果您只需要更改背景颜色或其他 - 您最好使用选择器。
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.
2)在你的例子中你不必膨胀xml文件然后找到按钮,你可以动态创建一个按钮然后直接返回这个按钮。
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.
3)如果您必须扩展复杂的视图 - 在您的 onItemClickListener 中,您可以通过 adapter\gridview.getItem 获取您的视图,然后,当您收到您的视图时,可以执行 findViewById 并找到所有必要的视图。
回答by Rick Falck
In onItemClick()
在 onItemClick()
gridAdapter.itemSelected = position;
in CustomGridAdapter:
在 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);
}