如何从另一个片段中打开一个片段?

时间:2020-06-15 20:55:35

标签: android android-fragments

我无法从一个片段到另一个片段。我一直在使用FragmentTransaction.replace()和FragmentManager对象,但是由于某些原因,该应用程序无法转到下一个片段。我正在尝试通过列表视图中的单击事件从HomeFragment转到ToDoListFragment。

HomeFragment.java:

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    DatabaseHelper myDb;
    ArrayList<ToDoList> arrayList;
    ArrayList<String> list;
    ArrayAdapter adapter;
    ListView listView;
    MyAdapter myAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        myDb = new DatabaseHelper(getActivity());
        list = new ArrayList<String>();
        ToDoList toDoList;

        final Cursor list_result = myDb.getAllListData();


        listView = root.findViewById(R.id.toDoList_list);
        viewData();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                String text = listView.getItemAtPosition(i).toString();
                ToDoListFragment fragment = new ToDoListFragment();
                while (list_result.moveToNext())
                {
                    String title = list_result.getString(1);

                    if (title.equals(text))
                    {
                        FragmentManager fm = getFragmentManager();
                        FragmentTransaction transaction = fm.beginTransaction();
                        transaction.replace(R.id.home_container, fragment);
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }
                }
            }
        });

        return root;
    }

fragment_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/toDoList_list"/>

</FrameLayout>

ToDoListFragment.java:

public class ToDoListFragment extends Fragment {
    ListView listView;

    public ToDoListFragment()
    {

    }

    private ToDoListViewModel mViewModel;
    DatabaseHelper myDb;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.to_do_list_fragment, container, false);
        listView = view.findViewById(R.id.task_list);

        myDb = new DatabaseHelper(getActivity());

        return view;
    }

to_do_list_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/task_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.ToDoListFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/task_list"/>

</FrameLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private DrawerLayout drawer;
    DatabaseHelper myDb;
    Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        NavigationView navigationView = findViewById(R.id.nav_view);
        drawer = findViewById(R.id.drawer_layout);
        menu = navigationView.getMenu();

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_add_new_list, R.id.nav_delete_list)
                .setDrawerLayout(drawer)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/frame_container"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

此外,我是否想在transaction.replace()中使用初始片段或我想去的片段中FrameLayout的ID?

1 个答案:

答案 0 :(得分:0)

原因很简单,您在片段中有一个框架布局,这是不正确的,因为片段必须包含在活动中

XML活动

RegularFileProperty

在片段中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">


    <FrameLayout
        android:id="@+id/content_frame"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>