ファイルブラウズ+ファイル選択
9947 ワード
FileBrowser.java
FileAdapter.java
file_row.xml(layout)
public class FileBrowser {
private Context context;
private List<String> items=null;
private List<String> paths=null;
private String rootPath="/";
private TextView mPath;
private View view;
private ListView list;
private FileAdapter m_FileAdapter;
private String selectedFilePath = "";
public String getSelectedFilePath() {
return selectedFilePath;
}
public void setSelectedFilePath(String selectedFilePath) {
this.selectedFilePath = selectedFilePath;
}
public View getFileListView(){
return view;
}
public FileBrowser(Context context){
this.context = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.filelist, null);
mPath = (TextView)view.findViewById(R.id.mPath);
mPath.setTextColor(context.getResources().getColor(R.color.text_color));
list = (ListView)view.findViewById(R.id.filelist);
getFileDir(rootPath);
}
private void getFileDir(String filePath){
mPath.setText(filePath);
items=new ArrayList<String>();
paths=new ArrayList<String>();
File f=new File(filePath);
File[] files=f.listFiles();
if(!filePath.equals(rootPath))
{
items.add("goroot");
paths.add(rootPath);
items.add("goparent");
paths.add(f.getParent());
}
for(int i=0;i<files.length;i++)
{
File file=files[i];
items.add(file.getName());
paths.add(file.getPath());
}
m_FileAdapter = new FileAdapter(context,items,paths);
list.setAdapter(m_FileAdapter);
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
File file=new File(paths.get(position));
if(file.canRead()){
if (file.isDirectory()){
getFileDir(paths.get(position));
}else{
ViewHolder vHollder = (ViewHolder) view.getTag();
for(ViewHolder v : m_FileAdapter.getVHs()){
v.cBox.setChecked(false);
v.cBox.setVisibility(View.GONE);
}
for(int i : m_FileAdapter.getIsSelected().keySet()){
m_FileAdapter.getIsSelected().put(i, false);
}
for(int i : m_FileAdapter.getIsVisibility().keySet()){
m_FileAdapter.getIsVisibility().put(i, View.GONE);
}
vHollder.cBox.setVisibility(View.VISIBLE);
m_FileAdapter.getIsVisibility().put(position, View.VISIBLE);
vHollder.cBox.setChecked(true);
m_FileAdapter.getIsSelected().put(position, vHollder.cBox.isChecked());
if(mPath.getText().toString().equals(rootPath)){
setSelectedFilePath(mPath.getText().toString() + vHollder.text.getText().toString());
}else{
setSelectedFilePath(mPath.getText().toString() + File.separator + vHollder.text.getText().toString());
}
Toast.makeText(context, getSelectedFilePath(), Toast.LENGTH_SHORT).show();
}
}else{
LinearLayout lay = new LinearLayout(context);
lay.setOrientation(LinearLayout.HORIZONTAL);
ImageView image = new ImageView(context);
TextView text = new TextView(context);
text.setTextColor(context.getResources().getColor(R.color.text_color));
text.setTextSize(16);
text.setText(" , !");
Toast toast = Toast.makeText(context, text.getText().toString(), Toast.LENGTH_SHORT);
image.setImageResource(android.R.drawable.ic_lock_idle_alarm);
lay.addView(image);
lay.addView(text);
toast.setView(lay);
toast.show();
}
}
});
}
}
FileAdapter.java
public class FileAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
private Bitmap mIcon4;
private List<String> items;
private List<String> paths;
private Map<Integer, Boolean> isSelected;
private Map<Integer, Integer> isVisibility;
public Map<Integer, Integer> getIsVisibility() {
return isVisibility;
}
private List<ViewHolder> VHs = new ArrayList<ViewHolder>();
private Context context;
public Map<Integer, Boolean> getIsSelected() {
return isSelected;
}
public void setIsSelected(Map<Integer, Boolean> isSelected) {
this.isSelected = isSelected;
}
public FileAdapter(Context context,List<String> it,List<String> pa){
this.context = context;
mInflater = LayoutInflater.from(context);
if( it != null && it.size() > 0){
isSelected = new HashMap<Integer, Boolean>();
isVisibility = new HashMap<Integer, Integer>();
for (int i = 0; i < it.size(); i++) {
isSelected.put(i, false);
isVisibility.put(i, View.GONE);
}
}
items = it;
paths = pa;
mIcon1 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.backroot);
mIcon2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.backparent);
mIcon3 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.folder);
mIcon4 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.doc);
}
@Override
public int getCount(){
return items.size();
}
@Override
public Object getItem(int position){
return items.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){
convertView = mInflater.inflate(R.layout.file_row, null);
holder = new ViewHolder();
VHs.add(holder);
holder.text = (TextView) convertView.findViewById(R.id.filetext);
holder.text.setTextColor(context.getResources().getColor(R.color.text_color));
holder.icon = (ImageView) convertView.findViewById(R.id.fileicon);
holder.cBox = (CheckBox) convertView.findViewById(R.id.file_check);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
File f=new File(paths.get(position).toString());
if(items.get(position).toString().equals("goroot")){
holder.text.setText(" ");
holder.icon.setImageBitmap(mIcon1);
holder.cBox.setChecked(isSelected.get(position));
holder.cBox.setVisibility(isVisibility.get(position));
}else if(items.get(position).toString().equals("goparent")){
holder.text.setText(" ");
holder.icon.setImageBitmap(mIcon2);
holder.cBox.setChecked(isSelected.get(position));
holder.cBox.setVisibility(isVisibility.get(position));
}else{
holder.text.setText(f.getName());
if(f.isDirectory()){
holder.icon.setImageBitmap(mIcon3);
holder.cBox.setChecked(isSelected.get(position));
holder.cBox.setVisibility(isVisibility.get(position));
}else{
holder.icon.setImageBitmap(mIcon4);
holder.cBox.setChecked(isSelected.get(position));
holder.cBox.setVisibility(isVisibility.get(position));
}
}
return convertView;
}
public List<ViewHolder> getVHs() {
return VHs;
}
public final class ViewHolder
{
public TextView text;
public ImageView icon;
public CheckBox cBox;
}
}
file_row.xml(layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/file_items"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView android:id="@+id/fileicon"
android:layout_height="wrap_content"
android:textSize="26dip"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_below="@+id/file_items"/>
<TextView android:id="@+id/filetext"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/fileicon"
android:paddingLeft="20dip"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/file_check"
style="?android:attr/starStyle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_below="@+id/file_items"/>
</RelativeLayout>