侧拉+viewpager+fragment

xiaoxiao2025-08-02  30

.activity_one.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer" tools:context=".OneActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" > </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <RadioButton android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="首页" android:button="@null" android:gravity="center"/> <RadioButton android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="西瓜视频" android:button="@null" android:gravity="center"/> <RadioButton android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="找人" android:button="@null" android:gravity="center"/> <RadioButton android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="小视频" android:button="@null" android:gravity="center"/> <RadioButton android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="我的" android:button="@null" android:gravity="center"/> </RadioGroup> </LinearLayout> <fragment class="com.example.lll.yuekaomoni.fragment.CeLaFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start"></fragment> </android.support.v4.widget.DrawerLayout>

.oneActivity

import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import java.util.ArrayList; public class OneActivity extends BaseActivity { private ViewPager vp; private RadioGroup group; private Button btn1,btn2,btn3,btn4,btn5; private ArrayList<Fragment> fragments; private DrawerLayout drawer; private ActionBarDrawerToggle mToggle; @Override protected void initData() { fragments = new ArrayList<>(); fragments.add(new AFragment()); fragments.add(new BFragment()); fragments.add(new CFragment()); fragments.add(new DFragment()); fragments.add(new EFragment()); vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.btn1: vp.setCurrentItem(0); break; case R.id.btn2: vp.setCurrentItem(1); break; case R.id.btn3: vp.setCurrentItem(2); break; case R.id.btn4: vp.setCurrentItem(3); break; case R.id.btn5: vp.setCurrentItem(4); break; } } }); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: group.check(R.id.btn1); btn1.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); btn2.setBackgroundColor(getResources().getColor(android.R.color.white)); btn3.setBackgroundColor(getResources().getColor(android.R.color.white)); btn4.setBackgroundColor(getResources().getColor(android.R.color.white)); btn5.setBackgroundColor(getResources().getColor(android.R.color.white)); break; case 1: group.check(R.id.btn2); btn2.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); btn1.setBackgroundColor(getResources().getColor(android.R.color.white)); btn3.setBackgroundColor(getResources().getColor(android.R.color.white)); btn4.setBackgroundColor(getResources().getColor(android.R.color.white)); btn5.setBackgroundColor(getResources().getColor(android.R.color.white)); break; case 2: group.check(R.id.btn3); btn3.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); btn1.setBackgroundColor(getResources().getColor(android.R.color.white)); btn2.setBackgroundColor(getResources().getColor(android.R.color.white)); btn4.setBackgroundColor(getResources().getColor(android.R.color.white)); btn5.setBackgroundColor(getResources().getColor(android.R.color.white)); break; case 3: group.check(R.id.btn4); btn4.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); btn1.setBackgroundColor(getResources().getColor(android.R.color.white)); btn2.setBackgroundColor(getResources().getColor(android.R.color.white)); btn3.setBackgroundColor(getResources().getColor(android.R.color.white)); btn5.setBackgroundColor(getResources().getColor(android.R.color.white)); break; case 4: group.check(R.id.btn5); btn5.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); btn1.setBackgroundColor(getResources().getColor(android.R.color.white)); btn2.setBackgroundColor(getResources().getColor(android.R.color.white)); btn3.setBackgroundColor(getResources().getColor(android.R.color.white)); btn4.setBackgroundColor(getResources().getColor(android.R.color.white)); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } @Override protected void initView() { vp = findViewById(R.id.vp); group = findViewById(R.id.group); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn3 = findViewById(R.id.btn3); btn4 = findViewById(R.id.btn4); btn5 = findViewById(R.id.btn5); drawer = findViewById(R.id.drawer); drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); initActionBar(); } private void initActionBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); mToggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close); mToggle.syncState(); drawer.addDrawerListener(mToggle); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(mToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); } @Override protected int initContentView() { return R.layout.activity_one; } }
转载请注明原文地址: https://www.6miu.com/read-5034158.html

最新回复(0)