点击按钮切换页面viewpager

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="首页"
            android:textSize="25sp"
            android:gravity="center_horizontal"
            android:padding="10dp"/>

        <ImageView
            android:id="@+id/img_change"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@mipmap/ic_launcher"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"/>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/fram_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>
</LinearLayout>
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        img_change = findViewById(R.id.img_change);

        manager = getSupportFragmentManager();
        listsFragment = new XListsFragment();
        xListFragment = new ListsFragment();
        manager.beginTransaction().add(R.id.fram_layout , listsFragment)
                                   .add(R.id.fram_layout , xListFragment)
                                   .hide(xListFragment).commit();


        img_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (page == 0){
                    page = 1;
                    manager.beginTransaction().show(xListFragment).hide(listsFragment).commit();
                }else {
                    page = 0;
                    manager.beginTransaction().show(listsFragment).hide(xListFragment).commit();
                }
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/yz1743585120/article/details/82728433