项目需要,使用ListView把存储卡中的某个文件夹下的文件列出来,并且可以对这些文件进行删除处理。
1、布局文件line.xml
用来布局ListView每一项的样式
一个图片文件、一个文本显示、一个选择框,其中CheckBox要做失焦处理,因为我们想要的是点击后进入ListView的监听函数
失焦处理:
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android :id= "@+id/icon"
android :layout_width= "40dp"
android :layout_height= "40dp"
android :paddingLeft= "10dp" />
android :id= "@+id/file_name"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_toRightOf= "@+id/icon"
android :textSize= "30dp"
android :textColor= "@android:color/white" />
android :id= "@+id/checkBox"
android :text= ""
android :layout_width= "78dp"
android :layout_height= "wrap_content"
android :layout_alignBottom= "@+id/file_name"
android :layout_alignParentRight= "true"
android :layout_alignParentEnd= "true"
android :focusable= "false"
android :clickable= "false"
android :focusableInTouchMode= "false" />
2、布局文件main.xml
用ListView显示文件,按钮操作文件,共4个按钮:确认删除,全部选择,取消选择,取消删除。
android :id= "@+id/listview"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_above= "@+id/line"
android :layout_alignParentTop= "true" >
android :id= "@+id/line"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :orientation= "horizontal" >
android :id= "@+id/affirm"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= " 确认删除 "
android :textSize= "50sp"
android :layout_weight= "1" />
android :id= "@+id/selectAll"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= " 全部选择 "
android :textSize= "50sp"
android :layout_weight= "1" />
android :id= "@+id/cancelselect"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= " 取消选择 "
android :textSize= "50sp"
android :layout_weight= "1" />
android :id= "@+id/cancel"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= " 取消删除 "
android :textSize= "50sp"
android :layout_weight= "1" />
3.、Main.java文件
// 定义存放在 drawable 文件夹下的 file.png 文件
import static com.nialt.www.listview.R.drawable.file ;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {
// 定义组件
ListView listView ;
Button affirm ;
Button selectAll ;
Button cancelselect ;
Button cancel ;
List,Object>> listitems ;
SimpleAdapter simpleAdapter ;
CheckBox checkBox1 ;
@Override
public void onResume ()
{
super .onResume();
// 绑定组件并且定义监听函数
listView =(ListView)findViewById(R.id.listview );
listView .setOnItemLongClickListener(this );
listView .setOnItemClickListener(this );
affirm =(Button)findViewById(R.id.affirm );
selectAll =(Button)findViewById(R.id.selectAll );
cancelselect =(Button)findViewById(R.id.cancelselect );
cancel =(Button)findViewById(R.id.cancel );
affirm .setOnClickListener(this );
selectAll .setOnClickListener(this );
cancelselect .setOnClickListener(this );
cancel .setOnClickListener(this );
File sdcardDir = Environment.getExternalStorageDirectory ();
// 得到一个路径,内容是 sdcard 的文件夹路径和名字
String path=sdcardDir.getPath();
File path1 = new File(path+"/RBMFILE" );
File[] files=path1.listFiles();
listitems =new ArrayList,Object>>();
for (int i=0 ; ilength; i++)
{
Map,Object> listitem=new HashMap,Object>();
listitem.put("icon" , file );
listitem.put("file_name" , files[i].getName());
listitem.put("checkBox" , false );
listitems .add(listitem);
}
// 创建一个 SimpleAdapter
simpleAdapter =new SimpleAdapter(getApplicationContext(),
listitems ,
R.layout.line ,
new String[]{"icon" , "file_name" , "checkBox" },
new int []{R.id.icon , R.id.file_name , R.id.checkBox });
listView .setAdapter(simpleAdapter );
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
}
@Override
public boolean onItemLongClick (AdapterView> adapterView, View view, int i, long l) {
Log.v (" 长按输出 " , " 长按输出 " );
return false;
}
@Override
public void onClick (View view) {
Button button=(Button)view;
if (button==affirm )
{
for (int i=0 ; i<listitems .size(); i++)
{
// 寻找选中的项
if (listitems .get(i).get("checkBox" ).toString().equals("true" ))
{
String s=listitems .get(i).get("file_name" ).toString();
Log.v (" 文件名称 " , s);
}
}
}
else if (button==selectAll )
{
for (int i=0 ; i<listitems .size(); i++)
{
listitems .get(i).put("checkBox" ,true );
simpleAdapter .notifyDataSetChanged();
}
}
else if (button==cancelselect )
{
for (int i=0 ; i<listitems .size(); i++)
{
if (listitems .get(i).get("checkBox" ).toString().equals("true" ))
{
listitems .get(i).put("checkBox" ,false );
simpleAdapter .notifyDataSetChanged();
}
}
}
else if (button==cancel )
{
Log.v (" 取消删除 " , "cancel" );
}
}
@Override
public void onItemClick (AdapterView> adapterView, View view, int i, long l) {
String s=listitems .get(i).get("checkBox" ).toString();
if (s.equals("false" ))
{
listitems .get(i).put("checkBox" ,true );
}
else
{
listitems .get(i).put("checkBox" ,false );
}
simpleAdapter .notifyDataSetChanged();
}
}
4、实现效果,真机运行的,直接拍图了