界面是一个listview,初始字体均为红色,点击一行选中时为字体颜色为绿色,再次点击取消选中变为红色,可以同时选择多行记录
页面布局文件:
<?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:background="#d5d3d6"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@drawable/choose_panel_background"
android:orientation="vertical" >
<ListView
android:id="@+id/panellistview"
android:layout_width="1064px"
android:layout_height="380px"
android:layout_gravity="center"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:divider="#FFCC00">
</ListView>
</LinearLayout>
</LinearLayout>
listitem布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1064px"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TableRow
android:layout_width="1064px"
android:layout_height="62px">
<View style="@style/list_item_cell_seperator_layout" />
<ImageView
android:id="@+id/selection"
android:layout_width="100px"
android:layout_height="58px"
android:gravity="center"
/>
<View style="@style/list_item_cell_seperator_layout" />
<TextView
android:id="@+id/panel"
style="@style/Panel_ListView_TextView_100px"
/>
<View style="@style/list_item_cell_seperator_layout" />
<TextView
android:id="@+id/subpanel"
style="@style/Panel_ListView_TextView_100px"
/>
<View style="@style/list_item_cell_seperator_layout" />
<TextView
android:id="@+id/oneway"
style="@style/Panel_ListView_TextView_150px"
/>
<View style="@style/list_item_cell_seperator_layout" />
<TextView
android:id="@+id/functionalrole"
style="@style/Panel_ListView_TextView_300px"
/>
<View style="@style/list_item_cell_seperator_layout" />
<TextView
android:id="@+id/roledescription"
style="@style/Panel_ListView_TextView_300px"
/>
<View style="@style/list_item_cell_seperator_layout" />
</TableRow>
<View
android:layout_height="2px"
android:background="#008f7d" />
</TableLayout>
activity文件:
public class ChoosePanelActivity extends Activity {
private ListView list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_panel);
//关联layout里的listview
list = (ListView) findViewById(R.id.panellistview);
//生成动态数组,加入数据
ArrayList<HashMap<String, Object>> arraylist = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("panel", "45路");
map.put("subpanel", "45");
map.put("oneway", "45环形");
map.put("functionalrole", "线路主调");
map.put("roledescription", "线路主调-调度");
arraylist.add(map);
}
final SelectedAdapter adapter = new SelectedAdapter(this, arraylist);
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//设置为多选
list.setOnItemClickListener (
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview, View v, int position,
long id) {
adapter.modifyStates(position);//根据点击的位置改变该行的选择状态
adapter.getView(position, v, list);
adapter.notifyDataSetChanged();
}
});
}
自己写的adapter:
public class SelectedAdapter extends BaseAdapter{
private ArrayList<HashMap<String, Object>> data;
private LayoutInflater layoutInflater;
private List<Boolean> listselected;//用布尔型的list记录每一行的选中状态
public SelectedAdapter(Context context,ArrayList<HashMap<String, Object>> data) {
this.data = data;
this.layoutInflater = LayoutInflater.from(context);
this.setListselected(new ArrayList<Boolean>(getCount()));
for(int i=0;i<getCount();i++)
getListselected().add(false);//初始为false,长度和listview一样
}
public List<Boolean> getListselected() {
return listselected;
}
public void setListselected(List<Boolean> listselected) {
this.listselected = listselected;
}
@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;
}
public void modifyStates(int position){
if(getListselected().get(position)==false){
getListselected().set(position, true);//如果相应position的记录是未被选中则设置为选中(true)
notifyDataSetChanged();
}else{
getListselected().set(position, false);//否则相应position的记录是被选中则设置为未选中(false)
notifyDataSetChanged();}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListItem item = null;
if(convertView==null){
item = new ListItem();
// 获取组件布局
convertView = layoutInflater.inflate(R.layout.panellistview_list, null);
item.selection = (ImageView) convertView.findViewById(R.id.selection);
item.panel = (TextView) convertView.findViewById(R.id.panel);
item.subpanel= (TextView) convertView.findViewById(R.id.subpanel);
item.oneway= (TextView) convertView.findViewById(R.id.oneway);
item.functionalrole= (TextView) convertView.findViewById(R.id.functionalrole);
item.roledescription= (TextView) convertView.findViewById(R.id.roledescription);
// 使用tag来存储数据
convertView.setTag(item);
}
else {
item = (ListItem) convertView.getTag();
}
// 绑定数据
item.panel.setText((String)data.get(position).get("panel"));
item.subpanel.setText((String)data.get(position).get("subpanel"));
item.oneway.setText((String)data.get(position).get("oneway"));
item.functionalrole.setText((String)data.get(position).get("functionalrole"));
item.roledescription.setText((String)data.get(position).get("roledescription"));
if(getListselected().get(position)==false){//如果未被选中,设置为红色
item.selection.setImageResource(R.drawable.bus_icon_red);
item.panel.setTextColor(Color.RED);
item.oneway.setTextColor(Color.RED);
item.subpanel.setTextColor(Color.RED);
item.functionalrole.setTextColor(Color.RED);
item.roledescription.setTextColor(Color.RED);
}else{//如果被选中,设置为绿色
item.selection.setImageResource(R.drawable.bus_icon_green);
item.panel.setTextColor(Color.GREEN);
item.oneway.setTextColor(Color.GREEN);
item.subpanel.setTextColor(Color.GREEN);
item.functionalrole.setTextColor(Color.GREEN);
item.roledescription.setTextColor(Color.GREEN);}
return convertView;
}
}
【转载】Listview可以选择多行,点击选中行item字体颜色变化
转载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【Android】listview选中行字体变大
目标:listview中item使用textview,当item选中时,字体为25px;当item未选中时,字体21px之前想了很久,以为同listview选中行字体颜色
listview android exception menu null -
listview实现点击选中
nchors.topMargin: 4 anchors.fil
js函数 i++ 值传 -
listviw item选中颜色
切记:每个item 的背景颜色不要添加
android sed xml