可以跟着创建4个文件,然后复制即可使用。
涉及到4部分:
1.activity的布局文件
2.每个Items的布局文件(使用ImageView等自定义控件)
3.自定义的adaptor
4.使用listview的adaptor
具体步骤如下
1.activity的布局文件
activity_option1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#eeeeee">
<ListView
android:id="@+id/option_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
2.每个listview的item的布局文件
option_items.xml
<?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="horizontal"
>
<ImageView
android:id="@+id/option_picture"
android:layout_width="100px"
android:layout_height="100px"
/>
<TextView
android:id="@+id/option_name"
android:layout_marginLeft="200px"
android:paddingTop="20px"
android:textSize="30px"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3.listview的Adaptor
OptionAdapter.java
public class OptionAdapter extends BaseAdapter {
private List<Map<String, Object>> data;
private LayoutInflater layoutInflater;
private Context context;
public OptionAdapter(Context context, List<Map<String, Object>> data) {
this.context = context;
this.data = data;
this.layoutInflater = LayoutInflater.from(context);
}
/**
* 组件集合,对应list.xml中的控件
*
* @author Administrator
*/
public final class Component {
public ImageView picture;
public TextView name;
}
@Override
public int getCount() {
return data.size();
}
/**
* 获得某一位置的数据
*/
@Override
public Object getItem(int position) {
return data.get(position);
}
/**
* 获得唯一标识
*/
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Component component = null;
if (convertView == null) {
component = new Component();
//获得组件,实例化组件
convertView = layoutInflater.inflate(R.layout.option_items, null); //这里是items的布局文件!
component.picture = (ImageView) convertView.findViewById(R.id.option_picture);
component.name = (TextView) convertView.findViewById(R.id.option_name);
convertView.setTag(component);
} else {
component = (Component) convertView.getTag();
}
//绑定数据
component.picture.setBackgroundResource((Integer) data.get(position).get("option_picture"));
component.name.setText((String) data.get(position).get("option_name"));
return convertView;
}
}
4.使用listview的activity
OptionActivity1.java
public class OptionActivity1 extends Activity {
private ListView listView = null;
List<Map<String, Object>> list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option1);//记得获取该acitivty的布局文件
listView = (ListView) findViewById(R.id.option_list);
list = getData();
listView.setAdapter(new OptionAdapter(this, list));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(list.get(position).get("option_name").equals("车载空调"))
{
Intent intentMain = new Intent();
intentMain.setClass(OptionActivity1.this, AirerActivity2.class);
OptionActivity1.this.startActivity(intentMain);
}
else if(list.get(position).get("option_name").equals("家居控制"))
{
Intent intent = new Intent();
intent.putExtra("mode", 2);//选择模式:2为蓝牙模式
intent.putExtra("account", "");
intent.setClass(OptionActivity1.this, ClientMainActivity.class);
OptionActivity1.this.startActivity(intent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("option_picture", R.drawable.car_airer);
map.put("option_name", "车载空调");
list.add(map);
map = new HashMap<String, Object>();
map.put("option_picture", R.drawable.car_ihome);
map.put("option_name", "家居控制");
list.add(map);
return list;
}
}
注意事项:
- 记得在AndroidManifest中给activity注册,防止报错
- 如何确定点击哪个items?
依据自定义adaptor来确定。
如下内容是list通过get获取map,然后再map中get获取相应内容
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(list.get(position).get("option_name").equals("车载空调"))
{
;
}
else if(list.get(position).get("option_name").equals("家居控制"))
{
;
}
}
});
如下内容用于向list中存入数据
public List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("option_picture", R.drawable.car_airer);
map.put("option_name", "车载空调");
list.add(map);
return list;
}