又好几天没有写博客了,上周末出去玩了一趟,昨天下班回来又和基友打了一晚上LOL,一晃几天就过去了,现在心里又有了快毕业时回想虚度学校生活的罪恶感,今天再也不能玩了,开撸吧。
不知道大家有没有遇到过在ListView中使用CheckBox之类的控件,相信这类需求还是很常见的,这里面也有一个很常见的问题,如果我们不对CheckBox这样的控件做特殊处理的话,ListView的OnItemClick事件就不会触发,其实原因很简单,是因为CheckBox的焦点优先级比ListView要高,我们点击Item的时候,焦点会被CheckBox抢占,然后会触发CheckBox的点击事件,类似的控件还有Button,RadioButton,其实解决起来也不是很难,只需要在xml中给这些控件设置两个属性就可以了(java代码中设置无效):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="5dp">
<TextView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
/>
<CheckBox
android:id="@+id/choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</LinearLayout>
以下是Activty中代码:
package com.meskal.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btnAll,btnNone;
private ListView lv;
private DataAdapter mAdapter;
private List<Data> list;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
initData();
}
private void initView() {
btnAll = (Button) findViewById(R.id.all);
btnNone = (Button) findViewById(R.id.none);
lv = (ListView) findViewById(R.id.list);
}
private void setListener() {
btnAll.setOnClickListener(this);
btnNone.setOnClickListener(this);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Data data = list.get(position);
data.setSelected(!data.isSelected());
mAdapter.setData(list);
}
});
}
private void initData() {
mAdapter = new DataAdapter(this);
lv.setAdapter(mAdapter);
list = new ArrayList<>();
for (int i = 0; i <30; i++) {
list.add(new Data("这是第" + i+ "条内容", false));
}
mAdapter.setData(list);
}
@Override public void onClick(View v) {
switch (v.getId()){
case R.id.all:
for (Data data : list){
data.setSelected(true);
}
mAdapter.setData(list);
break;
case R.id.none:
for (Data data : list){
data.setSelected(false);
}
mAdapter.setData(list);
break;
}
}
}
主页布局很简单,这里面用一个ScrollView嵌套ListView,一般会出现可能只显示一行数据,其他的要滑动才能看到,这里可以自定义计算一下ListView的高度然后重新设置param:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
这方法依次计算每个Item高度然后累加起来然后再设置ListView的高度,相对来说比较繁琐,当然还有更简单的方法,只需要在布局文件中设置ScrollView的一个属性就可以了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/line"
android:dividerHeight="1px"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/all"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="全选"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/btn_bg"
/>
<Button
android:id="@+id/none"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="全不选"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/btn_bg"
/>
</LinearLayout>
</LinearLayout>
在pojo里面,我们加一个属性标注该对象是否被选中,当然也可以用一个map来记录每个Item是否被选中,可以以位置为key,是否选中为value,但是如果我们还要在别的地方获取某条数据的状态还是下面这种更好用:
package com.meskal.test;
public class Data {
private String content;
private boolean isSelected;
public Data(String s, boolean b) {
this.content = s;
this.isSelected = b;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
adapter中内容很常见了,相信大家都写过N遍了,最近RecycleView很流行,用了一次,感觉比ListView灵活多了,表格,瀑布流效果美爆了,以后要多用用那个了,ListView相对简单多了:
package com.meskal.test;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by 25711 on 2016/8/9.
* Function:
* Version:
*/
public class DataAdapter extends BaseAdapter{
private Context mContext;
private LayoutInflater inflater;
private List<Data> datas;
public DataAdapter(Context context){
this.mContext = context;
inflater = LayoutInflater.from(context);
datas = new ArrayList<>();
}
public void setData(List<Data> list){
if (list == null){
return;
}
this.datas = list;
notifyDataSetChanged();
}
@Override public int getCount() {
return datas.size();
}
@Override public Object getItem(int position) {
return datas.get(position);
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.tv = (TextView) convertView.findViewById(R.id.content);
holder.cb = (CheckBox) convertView.findViewById(R.id.choose);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(datas.get(position).getContent());
holder.cb.setChecked(datas.get(position).isSelected());
return convertView;
}
private static class ViewHolder{
TextView tv;
CheckBox cb;
}
}
整体来说只是很简单的两个知识点,一个是设置焦点优先级高的控件不可获的焦点,防止其抢占ListView的焦点,还一个就是ScrollView嵌套ListView等这样的滑动控件时让其撑满整个空间使用android:fillViewport="true",以及通过代码设置Listview的高度方法