从listview从一个片段导航到另一个片段

am46iovg  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(214)

我有一个问题,我不知道如何从一个片段导航到另一个片段,实际上第一个片段有一个listview,通过单击列表中的一个值,我想打开另一个片段,我粘贴了各种代码,请帮助我
主要活动

public class MainActivity extends AppCompatActivity

{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView btnNav=findViewById(R.id.navigationBottom);
    btnNav.setOnNavigationItemSelectedListener(navListner);
    //Salve lo stato del fragment
    if(savedInstanceState==null){
        //Setting Home Fragment as main fragment
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new PlaceFragment()).commit();
    }
}

//Listner Nav Bar
private BottomNavigationView.OnNavigationItemSelectedListener navListner = new
        BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                Fragment selectedFragment=null;

                switch (item.getItemId()){
                    case R.id.place:
                        selectedFragment=new PlaceFragment();
                        break;
                    case R.id.commercial:
                        selectedFragment=new CommercialFragment();
                        break;
                    case R.id.emergency:
                        selectedFragment=new EmergencyFragment();
                        break;
                }

                //Begin Transaction
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragment_container,selectedFragment).commit();
                return true;
            }
        };

}
主布局

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigationBottom"/>

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/navigationBottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/menu"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:foregroundGravity="center"/>

片段活性

public class PlaceFragment extends Fragment {

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_place, container, false);
}

}
片段布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlaceFragment">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listplace"
    android:entries="@array/place_visit"/>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题