2012-05-08 70 views
0

我想在我的應用中實現文件瀏覽器。我看到了文件瀏覽器的代碼。但在所有這些示例中,dir /文件都列爲lisrview。我想在我的應用程序中提供gridview顯示。文件夾/文件將在網格中列出。點擊文件夾後,它會打開下一級網格。android文件瀏覽器在網格視圖中列出項目

我沒有看到任何支持這一點的實現。 如果您有任何引用,請分享

感謝時間和支持

+0

你嘗試過什麼?這實際上非常簡單/基本要做 – dymmeh

+0

沒有實現,但是自己動手應該是非常簡單的。您可以將GridView視爲列表視圖,並使用每個ListView項目中的列。您可能需要一個自定義適配器,以便您可以正確填充每個listview項目,但如果您願意嘗試一下,那應該是非常簡單的。 – Gophermofur

+0

是的,我試過了,它很容易。謝謝。 – png

回答

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; 
} 
相關問題