我想在我的应用中实现文件浏览器。我看到了文件浏览器的代码。但在所有这些示例中,目录/文件都列为lisrview。我想在我的应用程序中显示gridview。文件夹/文件将列在网格中。点击一个文件夹,它将打开下一级网格。
我没有看到任何支持此功能的实现。 如果您有任何参考,请分享
感谢时间和支持
答案 0 :(得分:1)
在此处添加我的代码以供将来参考
public class FileBrowserActivity extends Activity {
GridView gridView;
public static List<FileBrowserItem> gridItemsList;
public static String name;
String root;
String TAG= "FileBrowser";
public static boolean multiSelectFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
root = getIntent().getExtras().getString("rootpath");
Log.i(TAG,"path:"+ root);
getGridItemsList();
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new FileBrowserGridViewAdapter(this));
}
private void getGridItemsList() {
gridItemsList = new ArrayList<FileBrowserItem>();
File file;
file = new File(root);
File list[] = file.listFiles();
if(list.length == 0){
Toast.makeText(getApplicationContext(), "Empty Folde", Toast.LENGTH_SHORT).show();
}
for (int i = 0; i < list.length; i++) {
FileBrowserItem item = new FileBrowserItem();
File temp_file = new File(file.getAbsolutePath(),list[i].getName());
item.setPath( file.getAbsolutePath()+"/"+list[i].getName());
item.setName(list[i].getName());
//Log.i(TAG,i + item.value + item.name);
if ( temp_file.listFiles() != null) {
item.setType(FileBrowserItem.DIRECTORY_TYPE) ;
} else {
item.setType(FileBrowserItem.FILE_TYPE) ;
}
gridItemsList.add(item);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
root = new File(root).getParent();
Log.i(TAG,"back"+root);
getGridItemsList();
finish();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_label);
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
holder.checkBox= (CheckBox) convertView
.findViewById(R.id.checkBox);
holder.image.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("onClick", "position [" + position + "]");
if (FileBrowserActivity.gridItemsList.get(position)
.getType() == FileBrowserItem.DIRECTORY_TYPE) {
Intent intent = new Intent(mContext,
FileBrowserActivity.class);
intent.putExtra("rootpath",
FileBrowserActivity.gridItemsList.get(position)
.getPath());
mContext.startActivity(intent);
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Log.i(TAG, "Position" + position);
Log.i(TAG, FileBrowserActivity.gridItemsList.get(position).getName());
holder.text1.setText(FileBrowserActivity.gridItemsList.get(position)
.getName());
holder.image
.setImageResource(getImageIcon(FileBrowserActivity.gridItemsList
.get(position).getType()));
if(FileBrowserActivity.multiSelectFlag){
holder.checkBox.setVisibility(View.VISIBLE);
}
else{
holder.checkBox.setVisibility(View.GONE);
}
return convertView;
}