ViewPager与Fragment结合使用,可滑动,可点击
2017-06-09 18:39
423 查看
布局文件
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baiw.testdeom.View.activity.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/Main_Viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/Main_RadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="漫画"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="社区"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_03"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="会员"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_04"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="我的"
android:textSize="25sp" />
</RadioGroup>
</LinearLayout>
[html] view
plain copy
private void initView() {//找控件
rg = (RadioGroup) findViewById(R.id.Main_RadioGroup);
rg.setOnCheckedChangeListener(this);
rb_1 = (RadioButton) findViewById(R.id.rb_01);
rb_2 = (RadioButton) findViewById(R.id.rb_02);
rb_3 = (RadioButton) findViewById(R.id.rb_03);
rb_4 = (RadioButton) findViewById(R.id.rb_04);
viewPager = (ViewPager) findViewById(R.id.Main_pager);
//初始化适配器
adapter = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//viewpager的监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//pager滑动的时候,根据角标radiobutton被选中
switch (position) {
case 0:
rb_1.setChecked(true);
break;
case 1:
rb_2.setChecked(true);
break;
case 2:
rb_3.setChecked(true);
break;
case 3:
rb_4.setChecked(true);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
初始化Fragment并添加到集合中
[html] view
plain copy
private void initEventes() {
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
}
把集合传到适配器中
[html] view
plain copy
private void initGetDate() {
adapter.getDate(fragments);
adapter.notifyDataSetChanged();
}
[html] view
plain copy
//点击rg可以跳转
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
int current = 0; //初始值为0
switch (checkedId) {
case R.id.rb_01:
current = 0;//第一个pager界面是0
break;
case R.id.rb_02:
current = 1;
break;
case R.id.rb_03:
current = 2;
break;
case R.id.rb_04:
current = 3;
break;
}
if (viewPager.getCurrentItem() != current) {
//vp.setCurrentItem(current);
viewPager.setCurrentItem(current, false);
}
}
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baiw.testdeom.View.activity.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/Main_Viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/Main_RadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="漫画"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_02"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="社区"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_03"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="会员"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_04"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="我的"
android:textSize="25sp" />
</RadioGroup>
</LinearLayout>
[html] view
plain copy
private void initView() {//找控件
rg = (RadioGroup) findViewById(R.id.Main_RadioGroup);
rg.setOnCheckedChangeListener(this);
rb_1 = (RadioButton) findViewById(R.id.rb_01);
rb_2 = (RadioButton) findViewById(R.id.rb_02);
rb_3 = (RadioButton) findViewById(R.id.rb_03);
rb_4 = (RadioButton) findViewById(R.id.rb_04);
viewPager = (ViewPager) findViewById(R.id.Main_pager);
//初始化适配器
adapter = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//viewpager的监听
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {//pager滑动的时候,根据角标radiobutton被选中
switch (position) {
case 0:
rb_1.setChecked(true);
break;
case 1:
rb_2.setChecked(true);
break;
case 2:
rb_3.setChecked(true);
break;
case 3:
rb_4.setChecked(true);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
初始化Fragment并添加到集合中
[html] view
plain copy
private void initEventes() {
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
fragments.add(fragment4);
}
把集合传到适配器中
[html] view
plain copy
private void initGetDate() {
adapter.getDate(fragments);
adapter.notifyDataSetChanged();
}
[html] view
plain copy
//点击rg可以跳转
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
int current = 0; //初始值为0
switch (checkedId) {
case R.id.rb_01:
current = 0;//第一个pager界面是0
break;
case R.id.rb_02:
current = 1;
break;
case R.id.rb_03:
current = 2;
break;
case R.id.rb_04:
current = 3;
break;
}
if (viewPager.getCurrentItem() != current) {
//vp.setCurrentItem(current);
viewPager.setCurrentItem(current, false);
}
}
相关文章推荐
- ViewPager与Fragment结合使用,可滑动,可点击
- Android ViewPager、Fragment、组合控件结合使用(滑动切换,点击导航切换)
- 横向滑动菜单,HorizontalScrollView、fragment、Viewpager结合使用
- Android_横向滑动菜单, HorizontalScrollView,Fragment,Viewpager结合使用 +pullToRefreshListView
- 横向滑动菜单, HorizontalScrollView,fragment,Viewpager结合使用
- 自由控制TabLayout和ViewPager结合使用时是否可以点击和滑动
- 横向滑动菜单, HorizontalScrollView,fragment,Viewpager结合使用
- Android_横向滑动菜单, HorizontalScrollView,Fragment,Viewpager结合使用
- 横向滑动菜单,HorizontalScrollView,fragment,Viewpager结合使用
- ViewPager和Fragment结合使用,可以做出顶部导航界面滑动效果
- 横向滑动菜单, HorizontalScrollView,fragment,Viewpager结合使用
- Android 使用ViewPager和RadioGroup配合Fragment实现标签页,可点击按钮切换、滑动切换。
- 使用ViewPager和Fragment同时实现点击底部Tab切换和手势滑动切换Fragment
- Android_横向滑动菜单, HorizontalScrollView,Fragment,Viewpager结合使用
- 横向滑动菜单, HorizontalScrollView,fragment,Viewpager结合使用
- android控件篇:ViewPager+Fragment+GridView的使用(与AndroidQuery框架结合)
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
- ViewPager+Fragment的结合使用,实现QQ界面的理解
- ViewPager+Fragment的结合使用,实现QQ界面的理解
- 使用FragmentTabHost+Fragment+viewpager 实现滑动分页