如何按回按钮上的父目录?

时间:2013-05-03 09:41:53

标签: android

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

我在Stackflow中看到一些代码在背压时变为root.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);

3 个答案:

答案 0 :(得分:0)

您需要覆盖在反向调用时调用的方法:

    /** {@inheritDoc}. */
@Override
public void onBackPressed() {
    if (this.currentDirectory.getParent() != null) {
        // --> browse one folder up...
    } else {
        // --> you are already at the root folder
    }
}

答案 1 :(得分: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);
}

答案 2 :(得分:0)

试试这个: 将路径保存在String变量中,说dirname并将其初始化为根目录。

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

稍稍将onItemClick更改为

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);
    }
}

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