• viewpager+fragment+HorizontalScrollView详细版


    XML布局

        <HorizontalScrollView
                android:id="@+id/hsv"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/iamge_glide"
                android:scrollbars="none"
                android:background="#f0f0f0" >
                
                <LinearLayout  
                android:id="@+id/id_gallery"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center_vertical"  
                android:orientation="horizontal" />
            </HorizontalScrollView>

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

    activity代码实现

          private HorizontalScrollView hsv;
           
        private ViewPager vp;
        String[] name = new String[] { "推荐", "热点", "娱乐", "科技", "汽车", "体育", "财经",
               "军事", "国际", "时尚", "视屏", "社会" };

      private LinearLayout mGallery;
       private int mScreenWidth;
       private int mItemWidth = 0;
       private ArrayList<String> list;
       private int columnSelectIndex = 0;
       private ArrayList<Fragment> fragments = new ArrayList<Fragment>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            initData();
            initTabColumn();
            initFragment();
            
        }
        private void initTabColumn() {
            // TODO Auto-generated method stub
            initScrollView();
            int count = list.size();
            for (int i = 0; i < count; i++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        mItemWidth, LayoutParams.WRAP_CONTENT);
                params.leftMargin = 5;
                params.rightMargin = 5;
                TextView columnTextView = new TextView(MainActivity.this);
                columnTextView.setGravity(Gravity.CENTER);
                columnTextView.setPadding(5, 5, 5, 5);
                columnTextView.setId(i);
                columnTextView.setText(list.get(i));
                // 设置默认标签为选中状态
                if (columnSelectIndex == i) {
                    columnTextView.setSelected(true);
                    columnTextView.setTextColor(Color.RED);
                }
                /*
                 * 设置点击事件监听
                 */
                columnTextView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        for (int j = 0; j < mGallery.getChildCount(); j++) {
                            TextView localView = (TextView) mGallery.getChildAt(j);
                            if (localView != v) {
                                localView.setSelected(false);
                            } else {
                                localView.setSelected(true);
                                vp.setCurrentItem(j);
                            }
                        }
                        int index = mGallery.indexOfChild(v);
                    }
                });
                mGallery.addView(columnTextView, i, params);
            }
        }

    //把数组添加到list集合中
    private void initData() {
            list = new ArrayList<String>();
            for (int i = 0; i < name.length; i++) {
                list.add(name[i]);

            }
            System.out.println(list);
        }
    private void initView() {

    //        image_head = (ImageView) findViewById(R.id.image_head);
    //        right_head = (ImageView) findViewById(R.id.right_head);
            hsv = (HorizontalScrollView) findViewById(R.id.hsv);
    //        right_head = (ImageView) findViewById(R.id.iamge_glide);
            vp = (ViewPager) findViewById(R.id.vp);
            mGallery = (LinearLayout) findViewById(R.id.id_gallery);
    }

            /**
            image_head.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    sm.showMenu();//显示侧滑菜单
                }
            });
    */


    public void initFragment() {
            fragments.clear();
            int count = list.size();
            for (int i = 0; i < count; i++) {//根据list集合的长度创建fragment
                Bundle data = new Bundle();   //传值给每个fragment
                data.putString("text", list.get(i));
                data.putInt("id", i);
                MyFragment f = new MyFragment();
                f.setArguments(data);
                fragments.add(f);
            }
            MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
                    getSupportFragmentManager(), fragments);
            vp.setAdapter(adapter);
            vp.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    vp.setCurrentItem(arg0);

                    selectTab(arg0);
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }

        private void selectTab(int tab_position) {
            // TODO Auto-generated method stub
            columnSelectIndex = tab_position;
            View currTextView = mGallery.getChildAt(tab_position);
            int w = currTextView.getMeasuredWidth();
            int l = currTextView.getLeft();
            int i2 = l + w / 2 - mScreenWidth / 2;
            hsv.smoothScrollTo(i2, 0);
            // 判断是否选中
            for (int i = 0; i < mGallery.getChildCount(); i++) {
                TextView eveTxetView = (TextView) mGallery.getChildAt(i);
                boolean ischeck;
                if (i == tab_position) {
                    ischeck = true;
                    eveTxetView.setTextColor(Color.RED);
                } else {
                    ischeck = false;
                    eveTxetView.setTextColor(Color.BLACK);
                }
                eveTxetView.setSelected(ischeck);
            }
        }
        
        private void initScrollView() {
            mScreenWidth = NewsUtils.getWindowsWidth(MainActivity.this);
            mItemWidth = mScreenWidth / 7;//计算HorizontalScrollView可以展示几个
            mGallery.removeAllViews();
        }

    //NewsUtils中的计算

      public final static int getWindowsWidth(Activity activity) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            return dm.widthPixels;
        }

    //MyFragmentPagerAdapter 适配器中的代码

    package com.wt.ada;

    import java.util.ArrayList;
    import java.util.List;

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.view.ViewGroup;

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

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

        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return fragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            System.out.println(fragments.size());
            return fragments.size();
        }
        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return POSITION_NONE;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Object obj = super.instantiateItem(container, position);
            return obj;
        }

    }

  • 相关阅读:
    【瞎口胡】CDQ 分治
    题解 Luogu P2934 [USACO09JAN]Safe Travel G
    【瞎口胡】线段树优化建图
    git did not exit cleanly (exit code 128)如何解决
    转贴Linux服务器性能评估
    小记 SqlHelper
    zookeeper安装部署
    zookeeper原理介绍
    zookeeper基本操作
    zookeeper四字命令
  • 原文地址:https://www.cnblogs.com/wbp0818/p/5308568.html
Copyright © 2020-2023  润新知