radiobutton与viewpager联动

Posted glj521

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了radiobutton与viewpager联动相关的知识,希望对你有一定的参考价值。

主布局

<RelativeLayout 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"
tools:context=".MainActivity" >

 

<RelativeLayout
android:id="@+id/rl"
android:background="#F2F2F2"
android:layout_width="match_parent"
android:layout_height="40dip" >

<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="38dip"
android:orientation="horizontal" >
<RadioButton
android:layout_weight="1"
android:id="@+id/zixun"
android:button="@null"
android:gravity="center"
android:checked="true"
android:textColor="@drawable/rb_blue_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="资讯"/>
<RadioButton
android:layout_weight="1"
android:id="@+id/redian"
android:button="@null"
android:textColor="@drawable/rb_blue_bg"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热点"/>
<RadioButton
android:layout_weight="1"
android:id="@+id/boke"
android:button="@null"
android:gravity="center"
android:textColor="@drawable/rb_blue_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="博客"/>
<RadioButton
android:textColor="@drawable/rb_blue_bg"
android:layout_weight="1"
android:id="@+id/tuijian"
android:button="@null"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="推荐"/>
</RadioGroup>
<ImageView
android:id="@+id/img"
android:layout_width="1dip"
android:layout_height="5dip"
android:layout_alignParentBottom="true"
android:background="#0f0"
android:scaleType="matrix"
/>
</RelativeLayout>

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rl" >
</android.support.v4.view.ViewPager>

</RelativeLayout>

创建selector选择器 rb_blue_bg

<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#0f0" />
    <item android:state_checked="false" android:color="#000000"/>
</selector>

activity

 vp = (ViewPager) findViewById(R.id.vp);
            rg = (RadioGroup) findViewById(R.id.rg);
            img = (ImageView) findViewById(R.id.img);
//          
            TabFragmentPagerAdapter mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
            vp.setAdapter(mAdapter);
            DisplayMetrics dm = new DisplayMetrics();
            //将当前窗口的一些信息放在DisplayMetrics类中,
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            //radiobutton的宽度
            indicatorWidth = dm.widthPixels / 4;
            LayoutParams params = img.getLayoutParams();
            params.width=indicatorWidth;
            
            img.setLayoutParams(params);
            setlisenten();
 private void setlisenten() {
            vp.setOnPageChangeListener(new OnPageChangeListener() {
                
                @Override
                public void onPageSelected(int position) {
                    if(rg!=null && rg.getChildCount()>position){
                        ((RadioButton)rg.getChildAt(position)).performClick();
                    }
                }
                
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }
                
                @Override
                public void onPageScrollStateChanged(int arg0) {
                }
            });
            rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                private int currentIndicatorLeft;

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                     switch (checkedId) {
                    case R.id.zixun:
                           setimg(0);
                            
                        break;
                    case R.id.redian
                    :
                        setimg(1);
                        
                        break;
                    case R.id.boke:
                        setimg(2);
                        
                        break;
                    case R.id.tuijian:
                        setimg(3);
                        
                        break;

                    default:
                        break;
                    }
                     }

                private void setimg(int checkedId) {
                    TranslateAnimation animation = new TranslateAnimation(
                                currentIndicatorLeft ,
                                ((RadioButton) rg.getChildAt(checkedId)).getLeft(), 0f, 0f);
                        animation.setDuration(100);
                        animation.setFillAfter(true);
                        
                        //执行位移动画
                        img.startAnimation(animation);
                        
                        vp.setCurrentItem(checkedId);    //ViewPager 跟随一起 切换
                        
                        //记录当前 下标的距最左侧的 距离
                        currentIndicatorLeft = ((RadioButton) rg.getChildAt(checkedId)).getLeft();
                }
                
            });
        }

fragment中

 View rootView = inflater.inflate(R.layout.fragment_selection_common, container, false);
            
            TextView tv_tabName = (TextView) rootView.findViewById(R.id.tv_tabName);
            
            Bundle bundle = getArguments();
            
            tv_tabName.setText(bundle.getString(MainActivity.ARGUMENTS_NAME, ""));
            
            return rootView;

适配器

 class TabFragmentPagerAdapter extends FragmentPagerAdapter{

            private Fragments ft;

            public TabFragmentPagerAdapter(FragmentManager fm) {
                super(fm);
                // TODO Auto-generated constructor stub
            }

            @Override
            public Fragment getItem(int arg0) {
                ft = new Fragments();
                
                Bundle args = new Bundle();
                args.putString(ARGUMENTS_NAME, tabTitle[arg0]);
                ft.setArguments(args);
                return ft;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return tabTitle.length;
            }
            
        }
  

 

以上是关于radiobutton与viewpager联动的主要内容,如果未能解决你的问题,请参考以下文章

Android ViewPager与Fragment联动

Android ViewPager与Fragment联动

Android ViewPager与Fragment联动

android tablayout 与viewpager联动 怎样显示tab标题

Android导航菜单横向左右滑动并和下方的控件实现联动

Android ViewPager + Fragment 实现导航栏(可以左右滑动)