fragment+viewpager+侧拉

本文介绍了一个基于Android的应用程序中实现侧滑菜单及底部导航栏的功能,并通过RadioButton触发不同Fragment的显示与隐藏,实现了页面间的快速切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/mydrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame"
        ></FrameLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/rel_navigate"
            android:layout_alignParentBottom="true">
            <RadioButton
                android:id="@+id/but1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="首页"
                android:button="@null"
                android:gravity="center"/>
            <RadioButton
                android:id="@+id/but2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="视频"
                android:button="@null"
                android:gravity="center"/>
            <RadioButton
                android:id="@+id/but3"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="我的"
                android:button="@null"
                android:gravity="center"/>

        </RadioGroup>

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:id="@+id/rel_menu"
        android:layout_gravity="start"
        android:background="#550000ff">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/img_title"
            android:background="@mipmap/ic_launcher"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sssssss"
            android:layout_below="@+id/img_title"/>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>
Activity:
public class MainActivity extends AppCompatActivity {

    private RadioButton b1;
    private RadioButton b2;
    private RadioButton b3;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private DrawerLayout drawelauout;
    private ImageView imgTitle;
    private RelativeLayout relMenu;

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

        drawelauout = (DrawerLayout) findViewById(R.id.mydrawer);
        imgTitle = (ImageView) findViewById(R.id.img_title);
        relMenu = (RelativeLayout) findViewById(R.id.rel_menu);

        b1 = (RadioButton) findViewById(R.id.but1);
        b2 = (RadioButton) findViewById(R.id.but2);
        b3 = (RadioButton) findViewById(R.id.but3);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        getSupportFragmentManager().beginTransaction().add(R.id.frame,fragment1).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.frame,fragment2).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.frame,fragment3).commit();
        hide();
        getSupportFragmentManager().beginTransaction().show(fragment1).commit();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                getSupportFragmentManager().beginTransaction().show(fragment1).commit();
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                getSupportFragmentManager().beginTransaction().show(fragment2).commit();
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                getSupportFragmentManager().beginTransaction().show(fragment3).commit();
            }
        });

        initdata();
    }

    private void initdata() {
        imgTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭,侧滑菜单
                drawelauout.closeDrawer(relMenu);
            }
        });
    }

    private void hide() {
        getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
        getSupportFragmentManager().beginTransaction().hide(fragment2).commit();
        getSupportFragmentManager().beginTransaction().hide(fragment3).commit();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值