I'm fairly new to Android development so I'm sorry if I haven't given all the information you need. Basically I'm building a basic file browser but I wanted to try something different out by having a list view of all the files in a fragment.
Here's what I have so far:
In my main activity I have a listView for all the directories which has an onclick event that calls stackAFragment which does this:
public class Main extends Activity implements OnItemClickListener {
private File RootDir;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Initialize the current directory when the main activity is created**/
RootDir = new File("/sdcard/Docs/");
Files_Main.setCurDir(RootDir);
ListView l = (ListView) findViewById(R.id.dir_list);
ArrayAdapter<String> directories = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
Files_Main.getFolderList(RootDir));
l.setAdapter(directories);
l.setOnItemClickListener(this);
stackAFragment(RootDir.toString());
}
/**
* Add a Fragment to our stack
*/
private void stackAFragment(String dir) {
File myFile = new File(dir);
Fragment f = new FilesFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.files_frag, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
My FilesFragment doesn't do anything partiularly cleaver, it just gets the current directory that I have saved in an arraylist and gets all the files in that directory and uses that list in the array adapter, like so:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
{
Context c = getActivity().getApplicationContext();
File curDir = Files_Main.getCurDir();
Toast.makeText(getActivity(), (curDir.toString()), Toast.LENGTH_SHORT).show();
/** setup layouts **/
LinearLayout main = new LinearLayout(c);
ListView list = new ListView(c);
/**Get a list of files from the current directory we are viewing**/
String[] FilesList = Files_Main.getFileList(curDir);
/**put list into a array adapter**/
ArrayAdapter<String> files = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
FilesList);
/**Set list view to contain items from array adapter**/
list.setAdapter(files);
list.setOnItemClickListener(this);
开发者_运维百科 /**Attach the list view to the LinearLayout view**/
main.addView(list);
/**Return LinearLayout View with list view as the fragment to display**/
return main;
}
But for some reason when I run this and go into another directory which should re-call the stackAFragment with a different directory, it doesn't replace the fragment which I'm very confused why this doesn't work. I also did this using the linearlayout and using buttons instead of a listview, which worked and replaced the fragment as you go into a new directory.
Can anyone help with this? Thanks as I'm confused why this is not working.
public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
{
File targetDir;
ListView l = (ListView) findViewById(R.id.dir_list);
if(parent.getItemAtPosition(pos).toString() == "...")
{
/**
* This is going up a directory by deleting the last entry of the directory arraylist.
* **/
targetDir = Files.getPreviousDir();
Files.setPreviousDir();
}
else
{
/**
* Sets the item clicked on as the target directory we want to browse and adds it
* to the directory arraylist.
* **/
targetDir = new File (Files.getCurDir() + "/" +
parent.getItemAtPosition(pos).toString());
Files.setCurDir(targetDir);
}
ArrayAdapter<String> directories = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
Files.getFolderList(targetDir));
l.setAdapter(directories);
l.setOnItemClickListener(this);
stackAFragment(targetDir.toString());
}
I am assuming that the ListView with the ID "dir_list" is not in a fragment, but is just in a ViewGroup with the ID "files_frag" like so...
<LinearLayout
android:id="@+id/files_frag"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/dir_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent/>
</LinearLayout>
If this is the case, I think I know your problem. ft.replace() only replaces fragments in the given container, not views. That is by design and why your new solution works since there is actually a fragment to replace.
To make it less "messy", I would create a FoldersFragment and move the ListView into it including the initialization that you are currently doing in Main.OnCreate(). This would be VERY similar to FilesFragment then add that fragment to the xml like so...
<LinearLayout
android:id="@+id/files_frag"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.mypackage.FoldersFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
For bonus points, reuse FilesFragments to handle both and you could possibly drill down recursively through multiple levels of a file structure :)
Hope that helps.
精彩评论