准备两个布局文件,一个给ListView列表准备,一个用于ListView里面的内容怎么样布局。
<?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" >
<include
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.08"
layout="@layout/order_position_search_information" />
<include
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.08"
layout="@layout/order_subjects_bar" />
<include
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.01"
layout="@layout/order_horizontal_bar" />
/**导入ListView的文件*/
<include
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="0.83"
layout="@layout/order_body" /> /*这个文件在下面的代码*/
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> /**这个就是order_body的文件*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
/**listView里面的列表怎么样布局,取决下面这个代码*/
<?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="wrap_content"
android:layout_marginBottom="5dp"
android:background="@drawable/search_bar_radius"
android:orientation="horizontal" >
<ImageView
android:id="@+id/header"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="ds"
android:textColor="#4a4a4a"
android:textSize="18dp" />
<ImageView
android:id="@+id/desc3"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:paddingLeft="10dp"
android:text="ds"
android:textColor="#4a4a4a"
android:textSize="18dp" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/desc2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="ds"
android:textColor="#4a4a4a"
android:textSize="18dp" >
</TextView>
</LinearLayout>
</LinearLayout>
/**下面就是实现代码*/
package com.example.tutor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class OrderFragment extends Fragment {
private ListView list;
private String[] names = new String[] { "课程: 数学,物理", "课程: 数学,物理,化学",
"课程: 数学,语文,英语", "课程: 物理,数学", "课程: 数学,语文,英语", "课程: 数学,英语,物理",
"课程: 政治,历史,地理", "课程: 数学,语文", "课程: 数学,英语,物理", "课程: 物理,化学,生物",
"课程: 数学,物理,", "课程: 政治,历史,地理" };
private String[] descs = new String[] { "年级: 初三", "学校: 广州大学", "学校: 中山大学",
"年级: 初二", "年级: 小学五年级", "学校: 华南师范大学", "年级: 高一", "年级: 小学四年级",
"学校: 华南理工大学", "年级: 高二", "学校: 华南师范大学", "年级: 高一" };
private String[] descs2 = new String[] { "地址: 广州市天河区林和西路17号", "对象: 初中,高中",
"对象: 小学,初中", "地址: 广州市天河区儒林大街45号", "地址: 广州市增城区风光路393号", "对象: 初中,高中",
"地址: 广州市天河区岗顶509号", "地址: 广州市海珠区903号", "对象: 小学,初中",
"地址: 广州市番禺区石碑603", "对象: 初中,高中", "地址: 广州市增城区中新镇395号" };
private int[] descs3 = new int[] { R.drawable.accept, R.drawable.awarded,
R.drawable.awarded, R.drawable.accept, R.drawable.accept,
R.drawable.awarded, R.drawable.accept, R.drawable.accept,
R.drawable.awarded, R.drawable.accept, R.drawable.awarded,
R.drawable.accept };
private int[] imageIds = new int[] { R.drawable.icon_05,
R.drawable.icon_05, R.drawable.icon_05, R.drawable.icon_05,
R.drawable.icon_05, R.drawable.icon_05, R.drawable.icon_05,
R.drawable.icon_05, R.drawable.icon_05, R.drawable.icon_05,
R.drawable.icon_05, R.drawable.icon_05 };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_order, null);
list = (ListView) view.findViewById(R.id.mylist);
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItem.put("desc", descs[i]);
listItem.put("desc2", descs2[i]);
listItem.put("desc3", descs3[i]);
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(),
listItems, R.layout.order_body_item, new String[] {
"personName", "header", "desc", "desc2", "desc3" },
new int[] { R.id.name, R.id.header, R.id.desc, R.id.desc2,
R.id.desc3 });
list.setAdapter(simpleAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position,
long id) {
/**这里就是实现界面跳转以及数据传递的地方,具体代码参考我以前的数据传递*/
Toast.makeText(getActivity(), names[position]+"被点击了",
Toast.LENGTH_LONG).show();
}
});
// list.setOnItemSelectedListener(new OnItemSelectedListener() {
//
// @Override
// public void onItemSelected(AdapterView<?> parent, View view,
// int position, long id) {
// // TODO Auto-generated method stub
// Toast.makeText(getActivity(), ""+position+id, Toast.LENGTH_SHORT).show();
//
// }
//
// @Override
// public void onNothingSelected(AdapterView<?> parent) {
// // TODO Auto-generated method stub
//
// }
//
// });
return view;
}
}