listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
if(isMultiple){
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
TextView textView = (TextView) view.findViewById(R.id.text);
if(checkBox.isChecked()){
checkBox.setChecked(false);
adapter.checkedMap.put(position, false);
textView.setTextColor(Color.WHITE);
adapter.colorMap.put(position, Color.WHITE);
int index = checked.indexOf(textView.getText()+“”);
checked.remove(index);
}else{
checkBox.setChecked(true);
adapter.checkedMap.put(position, true);
textView.setTextColor(Color.RED);
adapter.colorMap.put(position, Color.RED);
checked.add(textView.getText()+“”);
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu = menu.addSubMenu(“操作”);
subMenu.add(0, 1, 0, “多选”);
subMenu.add(0, 2, 0, “删除”);
subMenu.add(0, 3, 0, “全选”);
subMenu.add(0, 4, 0, “取消全选”);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1){
isMultiple = true;
checked = new ArrayList();
int index = adapter.getCount();
for(int i=0;i<index;i++){
adapter.visibleMap.put(i, CheckBox.VISIBLE);
}
adapter.notifyDataSetChanged();
}else if(item.getItemId()==2){
for(String text : checked){
int index = items.indexOf(text);
items.remove(index);
}
isMultiple = false;
adapter = new MyAdapter(items,MainActivity.this);
listView.setAdapter(adapter);
}else if(item.getItemId()==3){
isMultiple = true;
checked = new ArrayList();
int index = adapter.getCount();
for(int i=0;i<index;i++){
adapter.checkedMap.put(i, true);
adapter.colorMap.put(i, Color.RED);
adapter.visibleMap.put(i, CheckBox.VISIBLE);
View view1 = adapter.getView(i, null, null);
TextView textView = (TextView) view1.findViewById(R.id.text);
textView.setTextColor(Color.RED);
/*CheckBox checkBox = (CheckBox) view1.findViewById(R.id.checkbox);
checkBox.setVisibility(CheckBox.VISIBLE);*/
adapter.notifyDataSetChanged();
checked.add(textView.getText()+“”);
}
}else if(item.getItemId()==4){
isMultiple = false;
checked = null;
int index = adapter.getCount();
for(int i=0;i<index;i++){
adapter.checkedMap.put(i, false);
adapter.colorMap.put(i, Color.WHITE);
adapter.visibleMap.put(i, CheckBox.INVISIBLE);
/*View view1 = adapter.getView(i, null, null);
TextView textView = (TextView) view1.findViewById(R.id.text);
textView.setTextColor(Color.WHITE);
CheckBox checkBox = (CheckBox) view1.findViewById(R.id.checkbox);
checkBox.setVisibility(CheckBox.INVISIBLE);*/
adapter.notifyDataSetChanged();
}
}
return super.onOptionsItemSelected(item);
}
}
继承自BaseAdapter的MyAdapter
package yy.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList items;
private Bitmap icon;
private Context context;
public Map<Integer,Boolean> checkedMap; //保存checkbox是否被选中的状态
public Map<Integer,Integer> colorMap; //保存textview中文字的状态
public Map<Integer,Integer> visibleMap; //保存checkbox是否显示的状态
public MyAdapter(ArrayList items, Context context) {
super();
this.items = items;
this.context = context;
inflater = LayoutInflater.from(context);
icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
checkedMap = new HashMap<Integer, Boolean>();
colorMap = new HashMap<Integer, Integer>();
visibleMap = new HashMap<Integer, Integer>();
for(int i=0;i<items.size();i++){
checkedMap.put(i, false);
colorMap.put(i, Color.WHITE);
visibleMap.put(i, CheckBox.INVISIBLE);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
view = inflater.inflate(R.layout.file_row, null);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.text);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.setVisibility(visibleMap.get(position));
checkBox.setChecked(checkedMap.get(position));
image.setImageBitmap(icon);
text.setText(items.get(position));
text.setTextColor(colorMap.get(position));
return view;
}
}
主界面的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
<ListView
android:id=“@+id/listview”
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
[外链图片转存中…(img-wjoccTeX-1715897942342)]
[外链图片转存中…(img-qbtYztNR-1715897942343)]
[外链图片转存中…(img-P60b9Ahy-1715897942344)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!