<LinearLayout xmlns:android="schemas.android.com/apk/res/and…"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textColor="#FCFCFC"
android:textSize="11pt"
android:gravity="center_vertical"
android:layout_marginLeft="10dip"
/>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="381dip"
android:cacheColorHint ="#00000000"
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="53dip"
android:orientation="horizontal"
<Button
android:id="@+id/selectall"
android:layout_width="80dip"
android:layout_height="50dip"
android:layout_marginLeft="20dip"
android:text="全选"
android:gravity="center"
/>
<Button
android:id="@+id/inverseselect"
android:layout_width="80dip"
android:layout_height="50dip"
android:layout_marginLeft="118dip"
android:text="反选"
android:gravity="center"
/>
<Button
android:id="@+id/cancel"
android:layout_width="80dip"
android:layout_height="50dip"
android:layout_marginLeft="213dip"
android:text="取消已选"
android:gravity="center"
/>
ListView每个item的布局,listviewitem.xml:
这里需要注意的是,由于checkbox的点击事件优先级比listview的高,所以要添加android:focusable="false"属性,使得checkbox初始的时候没有获取焦点。
另外这里是点击ListView的item控制checkbox的状态改变,也就是让item接收clik事件,所以需要加上android:focusableInTouchMode="false"这一属性。
<RelativeLayout xmlns:android="schemas.android.com/apk/res/and…"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:layout_marginTop="20dip"
<TextView
android:id="@+id/item_tv"
android:layout_width="267dip"
android:layout_height="40dip"
android:textSize="10pt"
android:gravity="center_vertical"
android:layout_marginLeft="10dip"
/>
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_toRightOf="@id/item_tv"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
/>
ViewHolder类
package simtice.test.listview.viewholder;
import android.widget.CheckBox;
import android.widget.TextView;
public class ViewHolder {
public TextView tv = null;
public CheckBox cb = null;
}
为listview自定义适配器,该类为主Activity类MainActivity.java的内部类
public static class MyAdapter extends BaseAdapter {
public static HashMap<Integer, Boolean> isSelected;
private Context context = null;
private LayoutInflater inflater = null;
private List<HashMap<String, Object>> list = null;
private String keyString[] = null;
private String itemString = null; // 记录每个item中textview的值
private int idValue[] = null;// id值
public MyAdapter(Context context, List<HashMap<String, Object>> list,
int resource, String[] from, int[] to) {
this.context = context;
this.list = list;
keyString = new String[from.length];
idValue = new int[to.length];
System.arraycopy(from, 0, keyString, 0, from.length);
System.arraycopy(to, 0, idValue, 0, to.length);
inflater = LayoutInflater.from(context);
init();
}
// 初始化 设置所有checkbox都为未选择
public void init() {
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < list.size(); i++) {
isSelected.put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
ViewHolder holder = null;
if (holder == null) {
holder = new ViewHolder();
if (view == null) {
view = inflater.inflate(R.layout.listviewitem, null);
}
holder.tv = (TextView) view.findViewById(R.id.item_tv);
holder.cb = (CheckBox) view.findViewById(R.id.item_cb);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
HashMap<String, Object> map = list.get(position);
if (map != null) {
itemString = (String) map.get(keyString[0]);
holder.tv.setText(itemString);
}
holder.cb.setChecked(isSelected.get(position));
return view;
}
}
最后,最重要的就是MainActivity.java中一些事件响应的处理
public class MainActivity extends Activity {
TextView tv = null;
ListView lv = null;
Button btn_selectAll = null;
Button btn_inverseSelect = null;
Button btn_calcel = null;
String name[] = { "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", "G13", "G14" };
ArrayList listStr = null;
private List<HashMap<String, Object>> list = null;
private MyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
lv = (ListView) this.findViewById(R.id.lv);
btn_selectAll = (Button) this.findViewById(R.id.selectall);
btn_inverseSelect = (Button) this.findViewById(R.id.inverseselect);
btn_calcel = (Button) this.findViewById(R.id.cancel);
showCheckBoxListView();
//全选
btn_selectAll.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
listStr = new ArrayList();
for(int i=0;i<list.size();i++){
MyAdapter.isSelected.put(i,true);
listStr.add(name);
}
adapter.notifyDataSetChanged();//注意这一句必须加上,否则checkbox无法正常更新状态
tv.setText("已选中"+listStr.size()+"项");
}
});
Android开发除了flutter还有什么是必须掌握的吗?
相信大多数从事Android开发的朋友们越来越发现,找工作越来越难了,面试的要求越来越高了
除了基础扎实的java知识,数据结构算法,设计模式还要求会底层源码,NDK技术,性能调优,还有会些小程序和跨平台,比如说flutter,以思维脑图的方式展示在下图;
点击文档前往获取面试资料与视频教程;【阿里P7级别Android架构师技术脑图+全套视频】