0

下面是我的代码,它检查所选文件是目录还是文件。如果它是一个目录,它会更改为它。但是如何在按下后退按钮时返回父目录?

我在 Stackflow 中看到了一个代码,该代码在 backpressroot.parent时运行,但现在我没有找到该代码。

    gridView =(GridView)findViewById(R.id.grid);
    gridView.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int 
    position,long id) {


    if(currentFiles[position].isDirectory())
    {
        root = new File("/data/data/com.myexample
 /files/"+FileName(currentFilePath[position])+"/");

        Log.e("Root first",root+ " ");

        currentFiles = root.listFiles();

        inflateListView(currentFiles);
    }


     private void inflateListView(File[] files){


List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();


for(int i=0;i<files.length;i++)

{       
    Map<String, Object> listItem = new HashMap<String, Object>();

    if(files[i].isDirectory())
    {
        listItem.put("icon", R.drawable.folder);
    }
    else
    {
        listItem.put("icon", R.drawable.file);
    }

    listItem.put("fileName", files[i].getName());
    listItems.add(listItem);
  }

simpleAdapter=new SimpleAdapter(this,listItems,R.layout.line,new String[]
{"icon","fileName"},new int[]{R.id.icon,R.id.file_name});
gridView.setAdapter(simpleAdapter);
4

3 回答 3

0

您需要覆盖在 backpressed 上调用的方法:

    /** {@inheritDoc}. */
@Override
public void onBackPressed() {
    if (this.currentDirectory.getParent() != null) {
        // --> browse one folder up...
    } else {
        // --> you are already at the root folder
    }
}
于 2013-05-03T09:48:02.790 回答
0

很容易检测是否按下了返回按钮。你有几个选择:

1) 覆盖View.onKeyDown()

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
    }
    return super.onKeyDown(keyCode, event);
}

2) 覆盖Activity.onBackPressed()

@Override
public void onBackPressed() {
}

3) 覆盖Activity.onKeyDown()(如果没有视图处理按键,则调用):

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Log.d(this.getClass().getName(), "back button pressed");
    }
    return super.onKeyDown(keyCode, event);
}
于 2013-05-03T09:52:45.113 回答
0

试试这个:将您的路径保存在一个字符串变量中dirname,然后将其初始化到您的根目录。

初始化
String dirname=<your-root-directory>;

稍微改变你的onItemClickas

protected void onListItemClick(ListView l, View v, int position, long id)
{
    if(currentFiles[position].isDirectory())
    {
        dirname=dirname+FileName(currentFilePath[position])+"/";  //changes here
        root = new File(dirname);

        Log.e("Root first",root+ " ");

        currentFiles = root.listFiles();
        inflateListView(currentFiles);
    }
}

要在按下返回按钮时返回父目录,请编写以下代码:

@Override
public void onBackPressed() 
{
    if(dirname.equals(<your-root-directory>))
    {
         super.onBackPressed();
    }
    else
    {
        dirname=dirname.substring(0, dirname.lastIndexOf("/"));
        root = new File(dirname);
        Log.e("Root first",root+ " ");
        currentFiles = root.listFiles();
        inflateListView(currentFiles);
    }
}

有任何疑问,在这里分享。

于 2013-05-03T10:21:27.113 回答