Using the GridView to achieve single-choice effect, the gridview implements single-Choice
1. Radio Effect
Because the single-choice button radiobutton provided by Android can only be displayed in a single row or single column, and the style is not beautiful, you can use the GridView for transformation to achieve the single-choice effect, to achieve this effect, the focus is on the GridView adapter.
The first is the XML of the items in the GridView:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<RelativeLayout
Android: id = "@ + id/options"
Android: layout_width = "match_parent"
Android: layout_height = "143dp">
<ImageView
Android: id = "@ + id/imageView"
Android: layout_width = "65dp"
Android: layout_height = "65dp"/>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: text = "Address Book"/>
</RelativeLayout>
</LinearLayout>
It is very simple, just used to display the selected imageView and text textView
Next is the adapter:
Public class GridViewRadioAdapter extends BaseAdapter {
Private Context mContext;
Private int lastPosition =-1; // records the position of the last selected image. It is not selected by default.
Private String [] str = null; // array of problematic content text
Public GridViewRadioAdapter (Context mContext ){
This. mContext = mContext;
}
Public void setStr (String [] str) {// call this method in activity to pass in the problematic Array
This. str = str;
}
Public void setSelection (int position) {// call this method in the onItemClickListener of the activity GridView to set the selected position
LastPosition = position;
}
@ Override
Public int getCount (){
Return str. length;
}
@ Override
Public Object getItem (int position ){
Return position;
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ViewHolder viewHolder = null;
If (convertView = null ){
ViewHolder = new ViewHolder ();
ConvertView = LayoutInflater. from (mContext). inflate (R. layout. adapter_gridview_radio_item, null );
ViewHolder. textView = (TextView) convertView. findViewById (R. id. textView );
ViewHolder. imageView = (ImageView) convertView. findViewById (R. id. imageView );
ViewHolder. relativeLayout = (RelativeLayout) convertView. findViewById (R. id. options );
ConvertView. setTag (viewHolder );
} Else {
ViewHolder = (ViewHolder) convertView. getTag ();
}
ViewHolder. textView. setText (str [position]);
If (lastPosition = position) {// determines whether the selected item is selected, and sets different styles for the selected item and non-selected item.
Switch (position) {// set the style in the selected status
Case 0:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_a_checked );
Break;
Case 1:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_ B _checked );
Break;
Case 2:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_c_checked );
Break;
Case 3:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_d_checked );
Break;
}
ViewHolder. relativeLayout. setBackgroundResource (R. drawable. shape_rect_orange );
} Else {// set the style in an unselected status
Switch (position ){
Case 0:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_a );
Break;
Case 1:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_ B );
Break;
Case 2:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_c );
Break;
Case 3:
ViewHolder. imageView. setBackgroundResource (R. drawable. option_d );
Break;
}
ViewHolder. relativeLayout. setBackgroundResource (R. drawable. shape_rect_gray );
}
Return convertView;
}
Class ViewHolder {
Private TextView textView;
Private ImageView imageView;
Private RelativeLayout relativeLayout;
}
}
Finally, set the adapter in activity:
GridViewRadioAdapter adapter = new GridViewRadioAdapter (mContext );
Adapter. setStr (options); // options for passing in the question
MGridView. setAdapter (adapter );
MGridView. setOnItemClickListener (new AdapterView. OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3 ){
Adapter. setSelection (position); // The value is updated.
Adapter. notifyDataSetChanged (); // notify the adapter to re-render each time you click
}
});