第一次写博客,本意是为了积累自己之所学,同时也是希望能够将自己在项目中踩到的一些坑分享出来,帮助同样采坑深受其难的朋友避免这些问题。做安卓时间不长,文章中有什么写的不对的地方,还希望不吝赐教
最近在项目中接到这样一个需求,点击上方息券,在其下方弹出其有效期时间,有多少张息券下方就显示多少有效日期,并且每一张有效期不一样。
刚接到需求时,想用radiobutton做,后来在网上各种搜索,发现网上对于radiobutton的使用基本都是在布局文件中写死,而我这种是需要从后台获取数据,根本达不到我要的效果,后来想到使用GridView布局实现多行多列的效果。
由于项目中Acitivity里面有存在ScrollView,导致GridView显示不全,所以这里将此解决方法一并列出,这里是布局文件,使用自定义GridView:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:textSize="20sp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/hello_world"
/>
<LinearLayout
android:id="@+id/linear1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@id/tv1"
android:padding="10dip">
<com.example.gridradiogroupdemo.MyGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="5dip"
android:horizontalSpacing="5dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
自定义GridView:
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 自定义GridView关键方法
* 解决问题:防止和ScollView发生滑动冲突导致GridView内容显示不全的问题
*/
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
这样就能解决GridView与ScrollView结合使用导致GridView内容显示不全的问题了。
现在开始我们的正题,下面是Activity里面的操作:
public class MainActivity extends Activity {private GridView mGrid=null;
private SimpleAdapter mWishesAdapter;
private int mLastItme=-1;
List<String> list = new ArrayList<String>();
String text = "有效期至:2016-11-8";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 8; i++) {
list.add(text); //将文本内容添加进列表中
Log.e("TAG", list.get(i));
}
String[] mWishes = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
mWishes[i] = list.get(i);
}
mWishesAdapter = getRadioButtonAdapter(R.drawable.jia_icon_on, mWishes);
mGrid = (GridView)findViewById(R.id.gridview);
mGrid.setAdapter(mWishesAdapter);
mGrid.requestFocus();
mGrid.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
if(mLastItme != position){
//更新图标、实现单选
if(mLastItme>=0){
ChangeRadioImg(mWishesAdapter, mLastItme,false);
}
mLastItme = position;
ChangeRadioImg(mWishesAdapter,position,true);
}
//获取TextView文本内容
CharSequence radioText = ((TextView) mGrid.getChildAt(position).findViewById(R.id.item_radiotext)).getText();
parent.getAdapter().getItem(position);
parent.getAdapter().getItemId(position);
Toast.makeText(MainActivity.this, "点击了"+radioText+"第"+position+"条", 0).show();
}
});
}
/*
* 创建包含多个radiobutton的Adapter。
* RadioButton的图片有redioImg指定,RadioButton的文字由radioNameArray指定
* RadioButton的图片和文字的相对位置在item_radiobutton.xml布局文件中指定。
*/
private SimpleAdapter getRadioButtonAdapter(int jiaIconOff, String[] mWishes2) {
// TODO Auto-generated method stub
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < mWishes2.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemRadioImg", jiaIconOff);
map.put("itemRadioText", mWishes2[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.item_radiobutton, new String[] { "itemRadioImg", "itemRadioText" },
new int[] { R.id.item_radioimg, R.id.item_radiotext });
return simperAdapter;
}
/*
* 根据选中的状态来更新图标。也就是实现我们自定义RadioGroup的单选功能
*/
private void ChangeRadioImg(SimpleAdapter adapter,int selectedItem, boolean on) {
HashMap<String, Object> map = (HashMap<String, Object>)adapter.getItem(selectedItem);
if(on) {
map.put("itemRadioImg", R.drawable.jia_icon_off);
} else{
map.put("itemRadioImg", R.drawable.jia_icon_on);
}
adapter.notifyDataSetChanged();
}
}
Adapter里面的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_radiobutton"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/item_radioimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/item_radiotext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/item_radioimg"
android:layout_marginLeft="5dip"
android:textSize="15sp"
/>
</RelativeLayout>
下面是效果图
代码里注释很清楚了,不再作过多解释,有什么疑问的地方欢迎大家留言!