AndroidFragment+Viewpager实现左右滑动和点击实现DrawerLayout

xiaoxiao2025-08-11  28

xml布局代码:

<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:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"></android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:orientation="horizontal"> <RadioButton //android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:checked="true" android:drawableTop="@drawable/mess" android:gravity="center" android:text="首页" /> <RadioButton //android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/see" android:gravity="center" android:text="影院" /> <RadioButton android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/start" android:button="@null" android:gravity="center" android:text="电影" /> <RadioButton //android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/xin" android:button="@null" android:gravity="center" android:text="会员" /> <RadioButton // android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:drawableTop="@drawable/peo" android:button="@null" android:gravity="center" android:text="我的" /> </RadioGroup> </LinearLayout> <fragment class="com.ww.wangwei.yuekao_lianxi.FragmentNav" android:layout_gravity="start" android:layout_width="360dp" android:layout_height="match_parent"></fragment> </android.support.v4.widget.DrawerLayout>

java代码:

public class MainActivity extends AppCompatActivity { private ViewPager pager; private RadioGroup radioGroup; private ArrayList<Fragment> list; private DrawerLayout drawerLayout; private ActionBar actionBar; private ActionBarDrawerToggle toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 pager = (ViewPager) findViewById(R.id.viewPager); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); initAction(); //默认选中第一个页面 radioGroup.check(radioGroup.getChildAt(0).getId()); //创建集合 list = new ArrayList<>(); list.add(new Frag01()); list.add(new Frag02()); list.add(new Frag03()); list.add(new Frag04()); list.add(new Frag05()); //适配器 pager.setAdapter(new PagerAdapter(getSupportFragmentManager(), list)); pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { radioGroup.check(radioGroup.getChildAt(i).getId()); } @Override public void onPageScrollStateChanged(int i) { } }); //联动 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { pager.setCurrentItem(checkedId - 1); /* switch (checkedId) { case R.id.button1: pager.setCurrentItem(0); break; case R.id.button2: pager.setCurrentItem(1); break; case R.id.button3: pager.setCurrentItem(2); break; case R.id.button4: pager.setCurrentItem(3); break; case R.id.button5: pager.setCurrentItem(4); break;*/ } } }); } private void initAction() { actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close); toggle.syncState(); drawerLayout.addDrawerListener(toggle); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }

}

新建一个Class,侧拉页面:

public class FragmentNav extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment,container,false); return view; } }
转载请注明原文地址: https://www.6miu.com/read-5034663.html

最新回复(0)