如何从片段类调用ListFragment片段

时间:2014-03-23 17:42:19

标签: android android-fragments android-listfragment

  

这个FragmentA调用另一个名为FilesManage的ListFragment类   但这给了我错误f1是不兼容的类型   transaction.replace(R.id.layout,f1);在FragmentA中

public class FragmentA extends Fragment implements OnClickListener {

public FragmentA() {

}

//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        return inflater.inflate(R.layout.activity_fileexplorer, container, false);
}




@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Button bt=(Button)getActivity().findViewById(R.id.skipButton);
    bt.setOnClickListener(this);
  }

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(getActivity(),"Turned on" ,Toast.LENGTH_LONG).show();
    FilesManage f1=new FilesManage();
    android.support.v4.app.FragmentManager manager =getFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    transaction.replace(R.id.layout, f1);
    transaction.commit();
    }
}

// ListFragment。

public class FilesManage extends ListFragment {

public FilesManage() {
    // Required empty public constructor
}

File currentDir;
FileArrayAdapter adapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    fill();

}

public void fill() {
    // TODO Auto-generated method stub
    currentDir = new File("/sdcard/");
    fill1(currentDir);

}

private void fill1(File f) {
    // TODO Auto-generated method stub

    {
        File[] dirs = f.listFiles();
        getActivity().setTitle("Current Dir: " + f.getName());
        List<Item> dir = new ArrayList<Item>();
        List<Item> fls = new ArrayList<Item>();
        try {
            for (File ff : dirs) {
                Date lastModDate = new Date(ff.lastModified());
                DateFormat formater = DateFormat.getDateTimeInstance();
                String date_modify = formater.format(lastModDate);
                if (ff.isDirectory()) {

                    File[] fbuf = ff.listFiles();
                    int buf = 0;
                    if (fbuf != null) {
                        buf = fbuf.length; // returns length of file in
                                            // bytes
                    } else
                        buf = 0;
                    String num_item = String.valueOf(buf);
                    if (buf == 0)
                        num_item = num_item + " item";
                    else
                        num_item = num_item + " items";

                    // String formated = lastModDate.toString();
                    dir.add(new Item(ff.getName(), num_item, date_modify,
                            ff.getAbsolutePath(), "directory_icon"));
                } else {

                    fls.add(new Item(ff.getName(), ff.length() + " Byte",
                            date_modify, ff.getAbsolutePath(), "file_icon"));
                }
            }
        } catch (Exception e) {

        }
        Collections.sort(dir);
        Collections.sort(fls);
        dir.addAll(fls);
        if (!f.getName().equalsIgnoreCase("sdcard"))
            dir.add(0, new Item("..", "Parent Directory", "",
                    f.getParent(), "directory_up"));
        adapter = new FileArrayAdapter(getActivity(), R.layout.file_view,
                dir);
        this.setListAdapter(adapter);
    }

}

// @Override
/*
 * public View onCreateView(LayoutInflater inflater, ViewGroup container,
 * Bundle savedInstanceState) { // Inflate the layout for this fragment
 * return inflater.inflate(R.layout.fragment_files_manage, container,
 * false); }
 */
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Item o = adapter.getItem(position);
    if (o.getImage().equalsIgnoreCase("directory_icon")
            || o.getImage().equalsIgnoreCase("directory_up")) {
        currentDir = new File(o.getPath());
        fill1(currentDir);
    } else {
        // onFileClick(o);
    }
}

}

1 个答案:

答案 0 :(得分:3)

你说过:

  

这给了我一个错误,即f1在transaction.replace中是不兼容的类型(R.id.layout,f1);在FragmentA中

上次我遇到这个问题,只是由于我的导入,请检查它们,如果你在FilesManage类中使用import android.app.ListFragment;它会导致不兼容,因为你的FragmentManager是{{1}的一部分}。在这种情况下,只需使用android.support.v4.app.*