scrollview+radiogroup+viewpager

来源:互联网 发布:centos python 编辑:程序博客网 时间:2024/04/29 04:33

布局:(如果点击颜色改变,需要添加selector选择器)

选择器

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


radio的布局中添有textColor属性


//主布局

<HorizontalScrollView
        android:id="@+id/mHsv"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:fadingEdge="none"
        android:scrollbars="none" >

        <RelativeLayout
            android:id="@+id/rl_nav"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#5AB0EB" >

            <RadioGroup
                android:id="@+id/rg_nav_content"
                android:layout_width="fill_parent"
                android:layout_height="38dip"
                android:layout_alignParentTop="true"
                android:background="#F2F2F2"
                android:orientation="horizontal"/ >

                <ImageView
                    android:id="@+id/iv_nav_indicator"
                    android:layout_width="1dip"
                    android:layout_height="5dip"
                    android:layout_alignParentBottom="true"
                    android:background="#0f0"
                    android:contentDescription="nav_desc"
                    android:scaleType="matrix" />
        </RelativeLayout>
    </HorizontalScrollView>

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

RadioButton的布局:


<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:background="#F2F2F2"
    android:button="@null"
    android:checked="true"
    android:gravity="center"
    android:text=""
    android:textSize="14.0dip" />

代码:在fragment中的代码:(内有侧滑)

public class ShouyeFragment extends Fragment {
    private ImageButton setting;
    private ImageButton serach_btn;
    private SlidingMenu menu;
    private List<ImageView> lists = new ArrayList<ImageView>();
    private ViewPager vp;
    private LinearLayout ll;
    private List<Integer> list;
    
    private int currentIndicatorLeft=0;
    private ListView lv;
    private View view;
    private RelativeLayout rl_nav;
    private HorizontalScrollView mHsv;
    private RadioGroup rg_nav_content;
    private int indicatorWidth;
    private LayoutInflater mInflater;
    private String[] tabTitle=new String[]{"推荐","热点","娱乐", "科技","汽车","体育","财经","军事","国际", "时尚","视频", "社会"};
    private TabFragmentPagerAdapter mAdapter;
    private ImageView iv_nav_indicator;
    private RadioButton rb;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        System.out.println("aaa");
        view = inflater.inflate(R.layout.fragment_shou, null);
        vp = (ViewPager) view.findViewById(R.id.vp);
        System.out.println("bbb");
        setting = (ImageButton) view.findViewById(R.id.mysetting_btn);
        rl_nav = (RelativeLayout)view.findViewById(R.id.rl_nav);
        mHsv = (HorizontalScrollView)view.findViewById(R.id.mHsv);
        rg_nav_content = (RadioGroup)view.findViewById(R.id.rg_nav_content);
        iv_nav_indicator = (ImageView) view.findViewById(R.id.iv_nav_indicator);
    
        // 侧滑
        MysettingView();
        initView();
        setListener2();
        //设置按钮可以展示侧滑页面
        setting.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                menu.setMode(SlidingMenu.LEFT);
                menu.showMenu();
                MysettingView();
            }
        });
        return view;
    }
    
    private void setListener2() {
        vp.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if(rg_nav_content!=null && rg_nav_content.getChildCount()>position){
                    ((RadioButton)rg_nav_content.getChildAt(position)).performClick();
                }
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        //radioGroup的监听事件
        rg_nav_content.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                
                if(rg_nav_content.getChildAt(checkedId)!=null){
                    
                    TranslateAnimation animation = new TranslateAnimation(
                            currentIndicatorLeft ,
                            ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft(), 0f, 0f);
                    animation.setInterpolator(new LinearInterpolator());
                    animation.setDuration(100);
                    animation.setFillAfter(true);
                    
                    //执行位移动画
                    iv_nav_indicator.startAnimation(animation);
                    
                    vp.setCurrentItem(checkedId);    //ViewPager 跟随一起 切换
                    
                    //记录当前 下标的距最左侧的 距离
                    currentIndicatorLeft = ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft();
                    
                     mHsv.smoothScrollTo(
                            (checkedId > 1 ? ((RadioButton) rg_nav_content.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) rg_nav_content.getChildAt(2)).getLeft(), 0);
                 }
            }
        });
    }
    //初始化图形
    private void initView() {
    //    System.out.println("aaa");
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        indicatorWidth = dm.widthPixels / 4;
        LayoutParams cursor_Params = (android.widget.LinearLayout.LayoutParams) iv_nav_indicator.getLayoutParams();
        cursor_Params.width = indicatorWidth;// 初始化滑动下标的宽
        iv_nav_indicator.setLayoutParams(cursor_Params);
        //填充布局填充器
        mInflater = (LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
    //    System.out.println("aaa");
        //另一种方式获取
//        LayoutInflater mInflater = LayoutInflater.from(this);  
        initNavigationHSV();
        mAdapter = new TabFragmentPagerAdapter(getChildFragmentManager(),tabTitle);
        vp.setAdapter(mAdapter);
    }
    //循环将标题插进单选按钮中
    private void initNavigationHSV() {
        rg_nav_content.removeAllViews();
        for (int j = 0; j < tabTitle.length; j++) {
            rb = (RadioButton) mInflater.inflate(R.layout.nav_radiogroup_item, null);
            rb.setId(j);
            rb.setText(tabTitle[j]);
            rb.setLayoutParams(new LayoutParams(indicatorWidth,
                    LayoutParams.MATCH_PARENT));
            rg_nav_content.addView(rb);
        }
        }
            
    // 设置页面的侧滑

    public void MysettingView() {
        menu = new SlidingMenu(getActivity());
        // 设置 那侧滑动
        menu.setMode(SlidingMenu.LEFT);
        // menu.toggle(true);
        // menu.showMenu(true);
        // menu.setAnimation(set);
        // 设置触屏模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        // 设置策划的宽度
        // menu.setShadowWidth(250);
        menu.setBehindOffset(250);
        // 绑定Activity
        menu.attachToActivity(getActivity(), SlidingMenu.SLIDING_CONTENT);
        menu.setFadeDegree(0.25f);
        menu.setMenu(R.layout.setting_shou);

    }


//适配器

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private String[] tabTitle;

    public ViewPagerAdapter(FragmentManager fm,String[] tabTitle) {
        super(fm);
        this.tabTitle=tabTitle;
        System.out.println("eee");
    }
    @Override
    public int getCount() {
        System.out.println("fff");
        return tabTitle.length;
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment ft;
    switch (arg0) {
    case 0:
        System.out.println("ggg");
        ft=new Fragment1();
        break;

    default:
        ft=new Fragment1();
        
        break;
    }
        return ft;
    }

}



0 0
原创粉丝点击