具有列表视图的片段在应该替换列表时没有替换列表?

发布于 2024-12-07 22:08:07 字数 4076 浏览 0 评论 0原文

我对 Android 开发还很陌生,所以如果我没有提供您需要的所有信息,我很抱歉。基本上我正在构建一个基本的文件浏览器,但我想通过片段中所有文件的列表视图来尝试不同的东西。

这是我到目前为止所拥有的:

在我的主要活动中,我有一个包含所有目录的 listView,其中有一个调用 stackAFragment 的 onclick 事件,该事件执行以下操作:

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

我的 FilesFragment 不做任何特别的事情,它只是获取我拥有的当前目录保存在数组列表中并获取该目录中的所有文件并在数组适配器中使用该列表,如下所示:

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

但是由于某种原因,当我运行此文件并进入另一个目录时,该目录应该使用不同的目录重新调用 stackAFragment目录,它不会替换片段,我很困惑为什么这不起作用。我还使用线性布局并使用按钮而不是列表视图来完成此操作,当您进入新目录时,它可以工作并替换片段。

谁能帮忙解决这个问题吗?谢谢,因为我很困惑为什么这不起作用。


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-12-14 22:08:07

我假设 ID 为“dir_list”的 ListView 不在片段中,而只是在 ID 为“files_frag”的 ViewGroup 中,如下所示...

<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>

如果是这种情况,我想我知道你的问题。 ft.replace() 仅替换给定容器中的片段,而不替换视图。这是设计使然,也是您的新解决方案有效的原因,因为实际上有一个片段需要替换。

为了使其不那么“混乱”,我将创建一个FoldersFragment并将ListView移入其中,包括您当前在Main.OnCreate()中正在进行的初始化。这与 FilesFragment 非常相似,然后将该片段添加到 xml 中,如下所示...

<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>

为了获得加分,请重用 FilesFragments 来处理这两者,并且您可以递归地向下钻取文件结构的多个级别:)

希望有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文