动态添加布局

    一些需求中,像listview,但是在listview中又有分类,这样来如果用listview来展示布局就比较乱,这样只能在scrollview中动态添加布局,和适配器中的getview()原来一样.循环list实现动态添加布局.先来看看效果图:

动态添加布局_第1张图片

从图可以看出1下面有一些应用名字数据,2下面也有一些数据,这样用listView来展示也是可以的,不过这里用的动态加载布局.

先看数据怎么处理:首先创建一个类来存储数据(用来测试):代码如下:

import android.graphics.drawable.Drawable;

public class AppEntity {

	private Drawable icon;//图片
	private String name;//名字
	private String packName;//包名
	private int size;//大小
	private int type;//标题

	public Drawable getIcon() {
		return icon;
	}

	public void setIcon(Drawable icon) {
		this.icon = icon;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPackName() {
		return packName;
	}

	public void setPackName(String packName) {
		this.packName = packName;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}

然后我们来创建item的布局,和适配器item一样需要布局.由于我们有两类不一样的布局所以分开比较简单,但是也可以不分开,通过判断隐藏也是可以的,

下面来看布局代码如下:




    

        
    

这个就是显示1,2的布局




    

        

        

            

            
        

        
    

这个就是显示比较多的内容那个item
现在我们就开始动态加载布局了代码如下:比较简单
public class SoftwareActivity extends Activity implements SelectListener {
	private LayoutInflater inflater;
	private LinearLayout layout;
	private ArrayList mList;
	private int index = -1;//判断1,2item的变量
	private SoftwareBarView barView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_software);
		inflater = getLayoutInflater();
		mList = getData();
		initView();
	}

	private void initView() {
		layout = (LinearLayout) findViewById(R.id.container_layout);
		barView = (SoftwareBarView) findViewById(R.id.software_bar_view);
		barView.setSelectListener(this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				ScreenUtil.dip2px(this, 0.5f));
		int color = getResources().getColor(R.color.gray);
		//动态加载布局
		for (int i = 0; i < mList.size(); i++) {
			AppEntity ent = mList.get(i);
			//控件1,2item
			if (ent.getType() != index) {
				index = ent.getType();
				View view = inflater.inflate(
						R.layout.activity_app_item_interval, null);
				TextView message = (TextView) view
						.findViewById(R.id.app_item_interval_text);
				message.setText(ent.getType() + "");
				layout.addView(view);
				View line = new View(getApplicationContext());
				line.setLayoutParams(params);
				line.setBackgroundColor(color);
				layout.addView(line);
			}
			//内容item
			View view = inflater.inflate(R.layout.activity_app_item, null);
			ImageView img = (ImageView) view
					.findViewById(R.id.activity_app_item_icon);
			TextView name = (TextView) view
					.findViewById(R.id.activity_app_item_name);
			TextView size = (TextView) view
					.findViewById(R.id.activity_app_item_content);
			TextView menu = (TextView) view
					.findViewById(R.id.activity_app_item_menu);
			//设置点击事件通过传i来判断点击的是那一项,
			menu.setOnClickListener(new MyOnItemClickLisener(i));
			name.setText(ent.getName());
			size.setText(ent.getSize() + "");

			layout.addView(view);
			//下面的分割线
			View line = new View(getApplicationContext());
			line.setLayoutParams(params);
			line.setBackgroundColor(color);
			layout.addView(line);
		}

	}

	public void soOnClick(View v) {
		switch (v.getId()) {
		case R.id.software_back_image:// 返回
			finish();
			break;
		case R.id.accelerate_speed_up:// 卸载

			break;
		}
	}
	//点击事件
	class MyOnItemClickLisener implements OnClickListener {
		private int position;

		public MyOnItemClickLisener(int i) {
			position = i;
		}

		@Override
		public void onClick(View v) {
			mList.get(position);
		}

	}
	//测试数据
	private ArrayList getData() {
		ArrayList list = new ArrayList();
		AppEntity ent1 = new AppEntity();
		ent1.setName("微信");
		ent1.setSize(50);
		ent1.setType(1);
		AppEntity ent2 = new AppEntity();
		ent2.setName("QQ");
		ent2.setSize(24);
		ent2.setType(1);
		AppEntity ent3 = new AppEntity();
		ent3.setName("微博");
		ent3.setSize(90);
		ent3.setType(2);
		AppEntity ent4 = new AppEntity();
		ent4.setName("手机管家");
		ent4.setSize(20);
		ent4.setType(2);
		AppEntity ent5 = new AppEntity();
		ent5.setName("有道字典");
		ent5.setSize(80);
		ent5.setType(2);

		list.add(ent1);
		list.add(ent2);
		list.add(ent3);
		list.add(ent4);
		list.add(ent5);
		return list;
	}

	@Override
	public void getOnclick(int i) {
		// 选择的数据
	}
}

代码比较简单,注释很清楚,可以做个参考去优化,希望对大家有用





你可能感兴趣的:(Android,工作总结)