轮播,项目中用到的,从项目中拿出分享给大家

首先,要轮播的Fragment,代码如下

/**

 * Created by Y201603 on 2016/7/6.
 * 发现中的首页fragment
 */
public class DscvShouyeFragment extends Fragment {


    private ViewPager vpLunbo;
    private TextView tvLunbo;
    private LinearLayout llLunbo;
    private List<String> imgStr = new ArrayList<>();//图片地址
    private List<ImageView> imgs = new ArrayList<>();//存放点点的集合
    private DscvSyLunboAdapter adapter;
    private int lbCunt;
    private MyRecycleView sq_recycle_grid;//表格布局
    private MyRecycleView sq_recycle;//list布局
    private ScrollView scrollView;


    private DscvGridAdapter gridAdapter;
    private DscvListAdapter listAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dscv_shouye, null, false);
        vpLunbo = (ViewPager) view.findViewById(R.id.vp_lunbo);
        tvLunbo = (TextView) view.findViewById(R.id.tv_lunbo);
        llLunbo = (LinearLayout) view.findViewById(R.id.ll_lunbo);
        sq_recycle_grid = (MyRecycleView) view.findViewById(R.id.sq_recycle_grid);
        sq_recycle = (MyRecycleView) view.findViewById(R.id.sq_recycle);
        scrollView= (ScrollView) view.findViewById(R.id.scrollView);


        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initData();
      /*  view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });*/
    }


    private void initData() {


        imgStr.add("ddddddddddddddd");
        imgStr.add("ssssssssssssss");
        imgStr.add("dddddddddddd");
        imgStr.add("ssssssssss");
        imgStr.add("dddddddd");
        lbCunt=Integer.MAX_VALUE/4/imgStr.size()*imgStr.size();
        LogUtil.i("lbCunt=="+lbCunt);
        adapter = new DscvSyLunboAdapter(imgStr, getActivity());
        vpLunbo.setAdapter(adapter);
        vpLunbo.setCurrentItem(lbCunt);
        //设置轮播点点
        setDots();


        sq_recycle_grid.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        sq_recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        sq_recycle.addItemDecoration(new UiUtils.ItemDivider(getActivity(), R.drawable.driver_shape));


        gridAdapter = new DscvGridAdapter();
        listAdapter = new DscvListAdapter();
        sq_recycle_grid.setAdapter(gridAdapter);
        sq_recycle.setAdapter(listAdapter);


        //开始轮播
        handler.sendEmptyMessageDelayed(HandlerUtils.LUNBO, 3000);


        //手指按下时,轮播的状态
        vpLunbo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        handler.removeMessages(HandlerUtils.LUNBO);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        handler.removeMessages(HandlerUtils.LUNBO);
                        break;
                    case MotionEvent.ACTION_UP:
                        handler.sendEmptyMessageDelayed(HandlerUtils.LUNBO, 3000);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        handler.sendEmptyMessageDelayed(HandlerUtils.LUNBO,3000);
                        break;
                }
                return false;
            }
        });


        vpLunbo.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }


            @Override
            public void onPageSelected(int position) {
                resetDots();
                int pos = position % imgStr.size();
                imgs.get(pos).setImageResource(R.drawable.lunbo_selected);
                tvLunbo.setText("哈哈哈ha" + pos);
                lbCunt = position;
            }


            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });
    }




    //设置点点
    private void setDots() {
        for (String str : imgStr) {
            ImageView img = new ImageView(getActivity());
            img.setImageResource(R.drawable.lunbo_normal);
            int pad = UiUtils.dp2px(getActivity(), 4);
            img.setPadding(pad, 0, pad, 0);
            imgs.add(img);
            llLunbo.addView(img);
        }
    }


    private void resetDots(){
        for(ImageView img : imgs){
            img.setImageResource(R.drawable.lunbo_normal);
        }
    }




    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == HandlerUtils.LUNBO) {
                //还原
                resetDots();
                vpLunbo.setCurrentItem(++lbCunt);
                int pos = lbCunt % imgStr.size();
                imgs.get(pos).setImageResource(R.drawable.lunbo_selected);
                tvLunbo.setText("哈哈哈ha" + pos);
                handler.sendEmptyMessageDelayed(HandlerUtils.LUNBO, 3000);
            }
        }
    };

}


/**
 * Created by Y201603 on 2016/7/6.
 * 发现首页 轮播的adapter
 */
public class DscvSyLunboAdapter extends PagerAdapter {
    private List<String> imgStrs = new ArrayList<>();
    private List<ImageView> imgs = new ArrayList<>();


    public DscvSyLunboAdapter(List<String> imgStrs, Context context) {
        this.imgStrs = imgStrs;
        for (String str : imgStrs) {
            imgs.add(new ImageView(context));
        }
    }


    @Override
    public int getCount() {
        return Integer.MAX_VALUE / 2;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(imgs.get(position % imgStrs.size()));
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView img = imgs.get(position % imgStrs.size());
        //ImgUtils.displayImage(imgStrs.get(position%imgStrs.size()), img);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        img.setLayoutParams(params);
        img.setImageResource(R.drawable.lunbo);
        img.setScaleType(ImageView.ScaleType.FIT_XY);


        remove(img);
        container.addView(img);
        return img;
    }


    private void remove(ImageView img){
        ViewParent parent = img.getParent();
        if(parent!=null && parent instanceof ViewGroup){
            ViewGroup group= (ViewGroup) parent;
            group.removeView(img);
        }
    }
}


Framgent  的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:background="#aaff0000">


        <android.support.v4.view.ViewPager
            android:id="@+id/vp_lunbo"
            android:layout_width="match_parent"
            android:layout_height="160dp"/>
        <TextView
            android:id="@+id/tv_lunbo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textSize="14sp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="5dp"
            android:text="欧洲杯来了,曼联VS利物浦 多特VS热刺"/>
        <LinearLayout
            android:id="@+id/ll_lunbo"
            android:layout_alignTop="@id/tv_lunbo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp"/>
    </RelativeLayout>


</LinearLayout>


点的selector  的布局

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="6dp" android:width="6dp"/>
    <solid android:color="@color/gray_low"/>
</shape>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:height="6dp" android:width="6dp"/>
    <solid android:color="@color/yellow"/>
</shape>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值