先是主 布局文件 main.xml
<?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" > <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list_view" ></ListView> </LinearLayout>
然后是3个不同控件的布局文件,先是 TextView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="8dip" > <TextView android:id="@+id/monitor_list_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > </TextView> <TextView android:id="@+id/monitor_list_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" > </TextView> </RelativeLayout>
然后是 Checkbox
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="8dip" > <TextView android:id="@+id/monitor_list_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > </TextView> <CheckBox android:id="@+id/monitor_list_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" > </CheckBox> </RelativeLayout>
然后是 Turn On / Turn Off == ToggleButton 控件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="8dip" > <TextView android:id="@+id/monitor_list_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > </TextView> <ToggleButton android:id="@+id/monitor_list_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" /> </RelativeLayout>
然后是 ListItem.java 里面继承了 BaseAdapter 并在里面做了 position判断调用不同 布局 的
package t1.com; import java.util.HashMap; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; import android.widget.ToggleButton; public class ListItem extends BaseAdapter { private List<HashMap<String,String>> list; private Context context; //构造函数 public ListItem (Context context,List<HashMap<String,String>> list){ this.context = context; this.list = list; } public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater mInflater = LayoutInflater.from(context); View view = null; switch (position) { case 0: view = mInflater.inflate(R.layout.list_textview, null); TextView title = (TextView)view.findViewById(R.id.monitor_list_left); title.setText("编号"); TextView value = (TextView)view.findViewById(R.id.monitor_list_right); value.setText(list.get(position).get("data")); break; case 1: view = mInflater.inflate(R.layout.list_turn, null); TextView title2 = (TextView)view.findViewById(R.id.monitor_list_left); title2.setText("是否开启"); ToggleButton value2 = (ToggleButton)view.findViewById(R.id.monitor_list_right); if(list.get(position).get("data").equals("1")){ value2.setChecked(true); }else{ value2.setChecked(false); } break; case 2: view = mInflater.inflate(R.layout.list_checkbox, null); TextView title3 = (TextView)view.findViewById(R.id.monitor_list_left); title3.setText("是否结婚"); CheckBox value3 = (CheckBox)view.findViewById(R.id.monitor_list_right); if(list.get(position).get("data").equals("1")){ value3.setChecked(true); }else{ value3.setChecked(false); } break; default: break; } return view; } }
最后是 主Activity文件 T1Activity.java
package t1.com; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class T1Activity extends Activity { /** Called when the activity is first created. */ private ListView listVIew; ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listVIew = (ListView)findViewById(R.id.list_view); String data[] = {"33","1","结婚设置","0"}; for(int i = 0;i<data.length;i++){ HashMap<String, String> map = new HashMap<String, String>(); map.put("data", data[i]); listItem.add(map); } ListItem listItemAdapter = new ListItem(T1Activity.this, listItem); listVIew.setAdapter(listItemAdapter); } }
分割 布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="8dip" android:paddingRight="8dip" android:paddingTop="4dip" android:paddingBottom="4dip" android:background="#E3E3E3" > <TextView android:id="@+id/monitor_list_left" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="10dp" > </TextView> </RelativeLayout>
** 如果空间是EditText 时不能获取焦点的问题
先设置litView 属性 android:scrollbars="none"
然后给 listVIew 的 父 层级 添加一个 LinearLayout , 再在外面添加一个 ScrollView , 遇到 listview 无法显示完全的办法是 给他的 layout_height 写死
** 得到 ListView 中 各个控件中的值
/* for (int i = 0; i < listVIew.getChildCount(); i++) { LinearLayout ll = (LinearLayout)listVIew.getChildAt(i);// 获得子级 CheckBox chkone = (CheckBox) ll.findViewById(R.id.chkone);// 从子级中获得控件 System.out.println("chkone.isChecked():----------->"+chkone.isChecked()); } */ RelativeLayout ll = (RelativeLayout)listVIew.getChildAt(0);// 获得子级 CheckBox chkone = (CheckBox) ll.findViewById(R.id.monitor_list_right);// 从子级中获得控件 if(chkone.isChecked()){ System.out.println("点击了"); }else{ System.out.println("没点击"); }
注意 RelativeLayout ll = (RelativeLayout)listVIew.getChildAt(0); 需要根据 子 布局的 不同 布局方式 定义 RelativeLayout 或 LinearLayout
** 关于处理 刷新 listView 置顶问题 使用 notifyDatasetchanged() 在之前先 将 listItemAdapter 里面的构造函数中的变量 重新赋值
if(!doFir){ listItemAdapter = new LoginIndexList(LoginIndex.this, listItem,1+Integer.parseInt(localDeviceArray[99])); listVIew.setAdapter(listItemAdapter); }else{ listItemAdapter.list = listItem; listItemAdapter.cut = 1+Integer.parseInt(localDeviceArray[99]); listItemAdapter.notifyDataSetChanged(); } doFir = true;