(4.3.1.6)【项目一】子Fragment之ViewPager和FragmentPagerAdapter

  • 创建FragmentPagerAdapter
  • 2调用适配器
  • 设置pager监听

创建FragmentPagerAdapter

public class NewsFragmentPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragments;
    private FragmentManager fm;

    public NewsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    public NewsFragmentPagerAdapter(FragmentManager fm,
            ArrayList<Fragment> fragments) {
        super(fm);
        this.fm = fm;
        this.fragments = fragments;
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void setFragments(ArrayList<Fragment> fragments) {
        if (this.fragments != null) {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment f : this.fragments) {
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
         YunShiArticleFragment articleFragment= (YunShiArticleFragment) super.instantiateItem(container, position);
        return articleFragment;
    }

}

2、调用适配器

    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    /** 用户选择的新闻分类列表*/
    private ArrayList<ChannelItem> userChannelList=new ArrayList<ChannelItem>();

    /** 获取Column栏目 数据*/
    private void initColumnData() {
        ArrayList<ChannelItem> defaultUserChannels = new ArrayList<ChannelItem>();
        for(int i=0;i<9;i++){
            defaultUserChannels.add(new ChannelItem(i+1, Constants.ARTICLE_KIND_NAME[i], i+1, 1));
        }
        userChannelList = defaultUserChannels;
    }

    /** * 初始化Column栏目项 * */
    private void initTabColumn() {
        mRadioGroup_content.removeAllViews();
        int count =  userChannelList.size();
        mColumnHorizontalScrollView.setParam(getActivity(), mScreenWidth, mRadioGroup_content, shade_left, shade_right, ll_more_columns, rl_column);
        for(int i = 0; i< count; i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth , LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
// TextView localTextView = (TextView) mInflater.inflate(R.layout.column_radio_item, null);
            TextView columnTextView = new TextView(getActivity());
            columnTextView.setTextAppearance(getActivity(), R.style.top_category_scroll_view_item_text);
// localTextView.setBackground(getResources().getDrawable(R.drawable.top_category_scroll_text_view_bg));
            columnTextView.setBackgroundResource(R.drawable.radio_buttong_bg);
            columnTextView.setGravity(Gravity.CENTER);
            columnTextView.setPadding(5, 5, 5, 5);
            columnTextView.setId(i);
            columnTextView.setText(userChannelList.get(i).getName());
            columnTextView.setTextColor(getResources().getColorStateList(R.drawable.yunshi_zixun_wenzi_yanse_index));
            if(columnSelectIndex == i){
                columnTextView.setSelected(true);
                columnTextView.setTextColor(getResources().getColor(R.color.white));
            }
            columnTextView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                      for(int i = 0;i < mRadioGroup_content.getChildCount();i++){
                          View localView = mRadioGroup_content.getChildAt(i);
                          if (localView != v){
                              localView.setSelected(false);
                              TextView ttTextView=(TextView) localView;
                              ttTextView.setTextColor(getResources().getColor(R.color.black));
                          }
                          else{//选中
                              localView.setSelected(true);
                              TextView ttTextView=(TextView) localView;
                              ttTextView.setTextColor(getResources().getColor(R.color.white));
                              mViewPager.setCurrentItem(i);
                          }
                      }
                     // Toast.makeText(getActivity(), userChannelList.get(v.getId()).getName(), Toast.LENGTH_SHORT).show();
                }
            });
            mRadioGroup_content.addView(columnTextView, i ,params);
        }
    }

        /** * 初始化Fragment * */
    private void initFragment() {
        fragments.clear();//清空
        int count =  userChannelList.size();
        for(int i = 0; i< count;i++){
            Bundle data = new Bundle();
            data.putString("text", userChannelList.get(i).getName());
            data.putInt("id", userChannelList.get(i).getId());
            YunShiArticleFragment newfragment = new YunShiArticleFragment();
            newfragment.setArguments(data);
            fragments.add(newfragment);
        }
        NewsFragmentPagerAdapter mAdapetr = new NewsFragmentPagerAdapter(getChildFragmentManager(), fragments);
// mViewPager.setOffscreenPageLimit(0);
        mViewPager.setAdapter(mAdapetr);
        mViewPager.setOnPageChangeListener(pageListener);
    }

设置pager监听

    /** * ViewPager切换监听方法 * */
    public OnPageChangeListener pageListener= new OnPageChangeListener(){

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub

            //mViewPager.setCurrentItem(position);
            selectTab(position);//导航tab的联动
        }
    };

    /** * 选择的Column里面的Tab * */
    private void selectTab(int tab_postion) {
        columnSelectIndex = tab_postion;
        for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) {
            View checkView = mRadioGroup_content.getChildAt(tab_postion);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            // rg_nav_content.getParent()).smoothScrollTo(i2, 0);
            mColumnHorizontalScrollView.smoothScrollTo(i2, 0);
            // mColumnHorizontalScrollView.smoothScrollTo((position - 2) *
            // mItemWidth , 0);
        }
        //判断是否选中
        for (int j = 0; j <  mRadioGroup_content.getChildCount(); j++) {
            View checkView = mRadioGroup_content.getChildAt(j);
            boolean ischeck;
            if (j == tab_postion) {
                ischeck = true;
                TextView tView=(TextView) checkView;
                tView.setTextColor(getResources().getColor(R.color.white));
            } else {
                ischeck = false;
                TextView tView=(TextView) checkView;
                tView.setTextColor(getResources().getColor(R.color.black));
            }
            checkView.setSelected(ischeck);
        }
    }

你可能感兴趣的:((4.3.1.6)【项目一】子Fragment之ViewPager和FragmentPagerAdapter)