Android中一个单行与多行并存的listview,可以用来参考:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:windowSoftInputMode="adjustUnspecified|stateHidden" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:layout_width="fill_parent" android:layout_height="380sp" android:id="@+id/CarAddlistView" android:paddingBottom="2sp" ></ListView> </LinearLayout> </ScrollView>
java代码:
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.DatePicker; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class multiListview extends Activity{ private ListView listview; private List<Map<String, Object>> DataList; MyAdapter adapter; private Runnable viewOrders; private int mYear; private int mMonth; private int mDay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //禁止休眠 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.listview); listview = (ListView)findViewById(R.id.listView); listview.setAdapter(adapter); listview.setOnItemClickListener(new ListView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) { setSelectedValues(position); } }); } //选择listview事件,处理相关逻辑 private void setSelectedValues(int position) { Intent _Intent; Bundle bundle; AreaOption area; switch(position) { case 0:// Toast.makeText(multiListview.this, "请正确选择您的日期1!", Toast.LENGTH_SHORT).show(); showDialog(DATE1_DIALOG_ID); break; case 1:// Toast.makeText(multiListview.this, "请正确选择您的日期2!", Toast.LENGTH_SHORT).show(); showDialog(DATE2_DIALOG_ID); break; } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE1_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); case DATE2_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener1, mYear, mMonth, mDay); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; Map<String, Object> map = new HashMap<String, Object>(); map.put("left1", "日期1:"); map.put("center1", getDate()); map.put("star1", "*"); map.put("arrow1", ">"); DataList.set(0, map); adapter.notifyDataSetChanged(); } }; private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; Map<String, Object> map = new HashMap<String, Object>(); map.put("left1", "日期2:"); map.put("center1", getDate()); map.put("star1", "*"); map.put("arrow1", ">"); DataList.set(1, map); adapter.notifyDataSetChanged(); } }; private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); //0 Map<String, Object> map = new HashMap<String, Object>(); map.put("left1", "日期1:"); map.put("center1", getDate()); map.put("star1", "*"); map.put("arrow1", ">"); list.add(map); //1 map = new HashMap<String, Object>(); map.put("left1", "日期2:"); map.put("center1", getDate()); map.put("star1", "*"); map.put("arrow1", ">"); list.add(map); //2 map = new HashMap<String, Object>(); map.put("left1", "地点1:"); map.put("center1", "请选择"); map.put("star1", "*"); map.put("arrow1", ">"); list.add(map); //3 map = new HashMap<String, Object>(); map.put("left1", "地点2:"); map.put("center1", "请选择"); map.put("star1", "*"); map.put("arrow1", ">"); list.add(map); //4 map = new HashMap<String, Object>(); map.put("left1", "体重:"); map.put("center1", "请选择"); map.put("star1", ""); map.put("arrow1", ">"); list.add(map); //5 map = new HashMap<String, Object>(); map.put("left1", "工作:"); map.put("center1", "请选择"); map.put("star1", "*"); map.put("left2", "移动电话1:"); map.put("center2", "请选择"); map.put("star2", "*"); map.put("arrow2", ">"); map.put("left3", "移动电话2:"); map.put("center3", "请选择"); map.put("star3", ""); list.add(map); //6 map = new HashMap<String, Object>(); map.put("left1", "其他:"); map.put("center1", "请选择"); map.put("arrow1", ">"); list.add(map); return list; } //获得日期 private String getDate() { StringBuilder Date = new StringBuilder() .append(mYear).append("-") .append(mMonth + 1).append("-") .append(mDay).append(" "); return Date.toString(); } public final class ViewHolder{ public TextView left1; public TextView left2; public TextView left3; public TextView center1; public TextView center2; public TextView center3; public TextView star1; public TextView star2; public TextView star3; public TextView arrow1; public TextView arrow2; public TextView arrow3; public LinearLayout line1; public LinearLayout line2; public LinearLayout line3; } public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; public MyAdapter(Context context){ this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return DataList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.sourceaddlistview, null); holder.left1 = (TextView)convertView.findViewById(R.id.left1); holder.left2 = (TextView)convertView.findViewById(R.id.left2); holder.left3 = (TextView)convertView.findViewById(R.id.left3); holder.center1 = (TextView)convertView.findViewById(R.id.center1); holder.center2 = (TextView)convertView.findViewById(R.id.center2); holder.center3 = (TextView)convertView.findViewById(R.id.center3); holder.star1 = (TextView)convertView.findViewById(R.id.star1); holder.star2 = (TextView)convertView.findViewById(R.id.star2); holder.star3 = (TextView)convertView.findViewById(R.id.star3); holder.arrow1 = (TextView)convertView.findViewById(R.id.arrow1); holder.arrow2 = (TextView)convertView.findViewById(R.id.arrow2); holder.arrow3 = (TextView)convertView.findViewById(R.id.arrow3); holder.line1=(LinearLayout)convertView.findViewById(R.id.line1); holder.line2=(LinearLayout)convertView.findViewById(R.id.line2); holder.line3=(LinearLayout)convertView.findViewById(R.id.line3); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.line2.setVisibility(convertView.GONE); holder.line3.setVisibility(convertView.GONE); holder.left1.setText((String)DataList.get(position).get("left1")); holder.center1.setText((String)DataList.get(position).get("center1")); holder.star1.setText((String)DataList.get(position).get("star1")); holder.arrow1.setText((String)DataList.get(position).get("arrow1")); if(position==5) { holder.left2.setText((String)DataList.get(position).get("left2")); holder.center2.setText((String)DataList.get(position).get("center2")); holder.star2.setText((String)DataList.get(position).get("star2")); holder.arrow2.setText((String)DataList.get(position).get("arrow2")); holder.left3.setText((String)DataList.get(position).get("left3")); holder.center3.setText((String)DataList.get(position).get("center3")); holder.star3.setText((String)DataList.get(position).get("star3")); holder.arrow3.setText((String)DataList.get(position).get("arrow3")); holder.line2.setVisibility(convertView.VISIBLE); holder.line3.setVisibility(convertView.VISIBLE); } return convertView; } }