0

我想在我的应用程序中实现文件浏览器。我看到了文件浏览器的代码。但在所有这些示例中,dir /files 都被列为 lisrview。我想在我的应用程序中显示网格视图。文件夹/文件将在网格中列出。单击文件夹时,它将打开下一级网格。

我没有看到任何支持这一点的实现。如果您有任何参考,请分享

感谢您的时间和支持

4

1 回答 1

2

在此处添加我的代码以供任何人将来参考

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;
}
于 2012-05-09T12:57:10.177 回答