如何加载我的片段并保存其状态

时间:2017-04-11 22:56:47

标签: java android xml android-fragments uinavigationbar

我的应用程序中有Aurel Hubert的BottomNavigation栏:https://github.com/aurelhubert/ahbottomnavigation

我有三个标签,问题是第二个片段太大了,它会以2秒的延迟打开,这会导致底栏的动画突然出现。所以我认为在开头一次加载片段并保存其状态会很有用。

我该怎么做? (我是Android Studio新手)

这是我的代码:我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.aaron.waller.mrpolitik.MainActivity"
    android:id="@+id/content_id">

    <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
        android:id="@+id/myBottomNavigation_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

和我的MainActivity.java:

public class MainActivity extends AppCompatActivity implements AHBottomNavigation.OnTabSelectedListener {

    private AHBottomNavigation bottomNavigation;
    private FragenFragment fragenFragment;
    private StatistikenFragment statistikenFragment;
    private ParteienFragment parteienFragment;




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

        parteienFragment = new ParteienFragment();
        statistikenFragment = new StatistikenFragment();
        fragenFragment = new FragenFragment();


        bottomNavigation = (AHBottomNavigation) findViewById(R.id.myBottomNavigation_ID);
        bottomNavigation.setOnTabSelectedListener(this);
        bottomNavigation.setDefaultBackgroundColor(Color.BLUE);
        this.createNavItems();

    }


    //Create items, add them to bar, set propertied and set current item
    private void createNavItems() {
        //CREATE ITEMS
        AHBottomNavigationItem ohnemundItem = new AHBottomNavigationItem("Parteien", R.drawable.parteienicon);
        AHBottomNavigationItem grinseItem = new AHBottomNavigationItem("Statistiken", R.drawable.statsicon);
        AHBottomNavigationItem lachItem = new AHBottomNavigationItem("Fragen", R.drawable.fragenicon);

        //ADD THEM to bar
        bottomNavigation.addItem(ohnemundItem);
        bottomNavigation.addItem(grinseItem);
        bottomNavigation.addItem(lachItem);


        //set properties
        bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));

        //set current item
        bottomNavigation.setCurrentItem(0);

    }

    @Override
    public void onTabSelected(int position, boolean wasSelected) {
        if (position == 0) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_id, parteienFragment).commit();
        } else if (position == 1) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_id, statistikenFragment).commit();
        } else if (position == 2) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_id, fragenFragment).commit();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

保留片段的第一步是在oncreate中的片段上设置它:

Parameter name hints

一旦完成,还有一些步骤。这里解释得很好:

more info