cari

Rumah  >  Soal Jawab  >  teks badan

java - Aplikasi ranap selepas menggunakan kaedah tambah objek FragmentTransaction dalam Android

Saya ingin menetapkan dan menggantikan serpihan dalam aktiviti. Dalam kaedah setDefaultFragment(), jika anda menggunakan pernyataan transaction.add(R.id.layFrame, MapsFragment.newInstance("map","map"));
, aplikasi akan ranap. Komen kenyataan ini dan ia akan berjalan seperti biasa.

Tangkapan skrin aplikasi

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  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"
    android:orientation="vertical"
    tools:context="com.example.tourguide.activity.MainActivity">

    <FrameLayout  android:id="@+id/layFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"  />

    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</LinearLayout>

Aktiviti

public class MainActivity extends AppCompatActivity
        implements BottomNavigationBar.OnTabSelectedListener{

    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //底部导航栏对象
        BottomNavigationBar bottomNavigationBar
                = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        //设置样式、颜色
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar
                .setActiveColor(R.color.colorPrimary)
                .setInActiveColor("#FFFFFF")
                .setBarBackgroundColor("#ECECEC");
        //添加导航项
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_location_on_white_24dp, "Location"))
                .addItem(new BottomNavigationItem(R.drawable.ic_tracks_on_white_24dp, "Tracks"))
                .setFirstSelectedPosition(0)
                .initialise();
        //获取fragment列表,设定默认fragment
        fragments = getFragments();
        setDefaultFragment();
        //监听
        bottomNavigationBar.setTabSelectedListener(this);

    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MapsFragment.newInstance("Map","Map"));
        fragments.add(TracksFragment.newInstance(3));
        return fragments;
    }

    private void setDefaultFragment(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.layFrame, MapsFragment.newInstance("map","map"));
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    void init() {

    }

    @Override
    public void onTabSelected(int position) {
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }
}
仅有的幸福仅有的幸福2747 hari yang lalu961

membalas semua(1)saya akan balas

  • 给我你的怀抱

    给我你的怀抱2017-06-12 09:21:13

    Selesai, ternyata terdapat antara muka OnFragmentInteraction dalam serpihan, tetapi ia tidak dilaksanakan dalam aktiviti

    balas
    0
  • Batalbalas