简体   繁体   中英

new list view in fragmentB from list view of fragment A on click on list item of A

MainActivity.java This is my MainActivity.java code.

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // 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_courses, R.id.nav_practice_ques, R.id.nav_contact_us)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

Courses.java (fragment A) Here in the Courses.java I am trying to call another fragment which is courseDetails.java through a list view. And I want to show a new list view in courseDetails.java After that, I am calling a new activity from this (courseDetails.java).

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_courses, container, false);
        ListView coursesListView = view.findViewById(R.id.coursesListView);
        ArrayList<String> courses = new ArrayList<>();
        courses.add("Science");
        courses.add("Mathematics");
        courses.add("English");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_checked, courses);
        coursesListView.setAdapter(arrayAdapter);

        coursesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                courseDetails courseDetails = new courseDetails();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.courseLayout, courseDetails);
                transaction.commit();
            }
        });


        return view;
    }

courseDetails.java (fragment B)

 @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course_details, container, false);
        ListView courseDetailView = view.findViewById(R.id.courseDetailView);
        ArrayList<String> courseDetail = new ArrayList<>();
        courseDetail.add("Physics");
        courseDetail.add("Chemistry");
        courseDetail.add("Biology");

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_checked, courseDetail);
        courseDetailView.setAdapter(arrayAdapter);

        courseDetailView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent (view.getContext(), coursePlayer.class);
                startActivity(intent);
            }
        });

        return view;
    }

When I tap on Science then I should see the new list view (ie Physics, Chemistry, Biology) only. But I get a new list overwritten on the previous list. 在此处输入图片说明

How can I resolve this? Thanks in advance

Solution:
Changed the id from:

transaction.replace(R.id.courseLayout, courseDetails);

to:

transaction.replace(R.id.nav_host_fragment, courseDetails);

Explanation:
Here, the 'nav_host_fragment' is the id of the host fragment which is in the main content. So, by using this id in replace method I am replacing the current fragment with the new fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM