自定义控件listView_button

点击按钮添加Edittext
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
<ListView 
    android:id="@+id/listview_layoutview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>    
<ImageButton 
    android:id="@+id/imagebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/go"/>
</LinearLayout>

 

自定义控件类
public class ListView_Button extends LinearLayout {
 
private Context context;
private ListView listView;
private ImageButton button;
 
private mAdapter adapter;
public ListView_Button(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context=context;
initView();
}
 
public ListView_Button(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
initView();
}
//获取所有Edittext里面的信息
public ArrayList<String> getDataArrayList(){
//调用适配器的getData方法
return adapter.getData();
}
 
private void initView(){
this.inflate(context, R.layout.layout_listview_button, this);
listView=(ListView)findViewById(R.id.listview_layoutview);
adapter=new mAdapter(context);
listView.setAdapter(adapter);
 
button=(ImageButton)findViewById(R.id.imagebutton);
 
button.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adapter.addItem();
setListViewHeightBasedOnChildren(listView);
}
});
}
class mAdapter extends BaseAdapter{
 
private ArrayList<EditText> arrayList;
private Context context;
 
public mAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
arrayList=new ArrayList<EditText>();
}
 
public ArrayList<String> getData(){
ArrayList<String> array=new ArrayList<String>();
 
for(EditText editText:arrayList){
String string=editText.getText().toString().trim();
if(!"".equals(string)){
array.add(string);
}
}
 
return array;
}
 
public void addItem(){
EditText editText=new EditText(context);
arrayList.add(editText);
this.notifyDataSetChanged();
 
}
 
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
 
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arrayList.get(arg0);
}
 
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
 
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return arrayList.get(arg0);
}
 
}
 
/**
* 动态设置ListView的高度
* @param listView
*/
public static void setListViewHeightBasedOnChildren(ListView listView) { 
    if(listView == null) return;
 
    mAdapter listAdapter = (mAdapter) listView.getAdapter(); 
    if (listAdapter == null) { 
        // pre-condition 
        return; 
    } 
 
    int totalHeight = 0; 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
        View listItem = listAdapter.getView(i, null, listView); 
        listItem.measure(0, 0); 
        totalHeight += listItem.getMeasuredHeight(); 
    } 
 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
}
}

 

posted on 2016-07-06 16:44  Just_Boy  阅读(384)  评论(0)    收藏  举报