LayoutInflater的作用类似于findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化。
通过LayoutInflater获取布局文件根元素,并将新的UI元素添加进去,再设置Adapter和setContentView(),就可以动态的改变布局;
看一例子:
要实现的效果:屏幕中六行六列图片,只显示下三角的图片,点击任一个图片,变为上三角;
1.Activity;
package zzu.wyb.gridview;
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class GridViewActivity extends Activity {
private GridView gv_sample; // GridView;
private int[] imgids = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3 };
private boolean layoutIndicator = false; // 控制图片显示位置的指示器;
private LayoutInflater inflater; //渲染器;
private LinearLayout rootLayout;//根布局;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* GridView gridv = new GridView(this);
this.setContentView(gridv);
gridv.setNumColumns(6);
gridv.setAdapter( new GridViewAdapter(this, imgids, true));
gridv.setOnItemClickListener(gridViewListener);
*/
setContentView(R.layout.main_gridview);
gv_sample = (GridView) findViewById(R.id.gv_sample);
gv_sample.setNumColumns(6);
setGridViewAdapter();
gv_sample.setOnItemClickListener(gridViewListener);
}
/**
* 设置监听器,设置适配器!
*/
private void setGridViewAdapter() {
GridViewAdapter adapter = new GridViewAdapter(this, imgids, layoutIndicator);// 新监听器
gv_sample.setAdapter(adapter);
}
GridView.OnItemClickListener gridViewListener = new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// 改变指示器;
layoutIndicator = !layoutIndicator;
System.out.println("test,itemSelectedListener.");
// 改变布局;
changeView(imgids, layoutIndicator);
}
};
/**
* 控制图片显示;
*
* @param
*/
private void changeView(int[] imgs, boolean viewIndicator) {
if (true) {
// 获取根布局;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
rootLayout = (LinearLayout) inflater.inflate(
R.layout.main_gridview, null);
// 移除布局文件下的所有子元素;
rootLayout.removeView(gv_sample);
// this.gv_sample.setVisibility(GridView.GONE);
//新一个GridView,上下文环境为当前;
gv_sample = new GridView(this);
gv_sample.setNumColumns(6);//设置显示为六行;
if (rootLayout != null) {
System.out.println("convertView is not null!!!");
gv_sample.setAdapter(new GridViewAdapter(this, imgs, viewIndicator));
rootLayout.addView(gv_sample);//将新的GridView添加到布局文件中;
this.setContentView(rootLayout);//显示更改过的布局;
gv_sample.setOnItemClickListener(gridViewListener);
} else {
System.out.println("convertView is null!!!");
}
}
}
}
package zzu.wyb.gridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* 配置Adapter
*
* @author BurgenWang
*
*/
public class GridViewAdapter extends BaseAdapter {
private Context context;
private int[] imgs;
private boolean viewIndicator;
/**
*
* @param context GridView所要显示的上下文环境;
* @param imgids 数据源,图片id数组;
* @param viewIndicator 指示器,boolean
*/
public GridViewAdapter(Context context, int[] imgids,boolean viewIndicator) {
this.context = context;
this.imgs = imgids;
this.viewIndicator = viewIndicator;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgs.length;
}
@Override
/**
* 通过元素在GridView中的位置获得该元素的id;
*/
public Object getItem(int position) {
return imgs[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return imgs[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = null;
if(convertView == null ){
img = new ImageView(this.context);
convertView = img;
}else{ //convertView保存上次的View;
img = (ImageView) convertView;
}
img.setScaleType(img.getScaleType().CENTER_CROP);//设置图片显示;从图片中心向外缩放;
img.setLayoutParams(new GridView.LayoutParams(70,70));//每个图片所占空间大小
img.setPadding(8, 8, 8, 8); //图片间左上右下间距;
img.setImageResource(this.imgs[position]);//图片资源
int x = position%6;//图片在GridView中所在行
int y = position/6;//列;
if(this.viewIndicator){//若为true,则显示右上半部分;
if(x>y ){
img.setVisibility(ImageView.INVISIBLE);
}
}else{//否则显示左下半部分;
if(x<y ){
img.setVisibility(ImageView.INVISIBLE);
}
}
return img;
}
}
3.layout布局文件;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gv_sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="6"
android:gravity="center">
</GridView>
</LinearLayout>