自动轮播图+自定义小圆点

//布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linear"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:layout_marginTop="10dp"
            android:padding="25dp"
            android:layout_width="match_parent"
            android:layout_height="360dp"
            android:id="@+id/dianying_pager"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >
            <LinearLayout
                android:id="@+id/dots"
                android:layout_alignParentRight="false"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_marginLeft="5dp"
                android:id="@+id/desc"
                android:text="DADAD"
                android:textSize="17dp"
                android:textColor="#ff3333"
                android:layout_height="wrap_content" />

         </LinearLayout>


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

//主方法 我的是Fragment

package com.example.lenovo.myapplication.Fragment.Fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.myapplication.Adapter.MyimagerAdapter;
import com.example.lenovo.myapplication.Bean.MyloadBean;
import com.example.lenovo.myapplication.R;
import com.example.lenovo.myapplication.Utils.ImageUtils;
import com.example.lenovo.myapplication.Utils.Utils;

import java.util.List;

public class DianyingFragment extends Fragment {

    private ViewPager dianying_viewPager;
    private TextView desc;
    private LinearLayout dots;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View  view = inflater.inflate(R.layout.xiaoxi_dianying,container,false);
        desc = view.findViewById(R.id.desc);
        dots = view.findViewById(R.id.dots);

        return view;

    }
    private String urlStr="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dianying_viewPager = view.findViewById(R.id.dianying_pager);
        LoadData();
    }

    private void LoadData() {

        final MyimagerAdapter adapter = new MyimagerAdapter(getActivity());
        dianying_viewPager.setAdapter(adapter);

        dianying_viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private int cacheIndex = -1;
            //页面滚动
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            //页面选择
            @Override
            public void onPageSelected(int i) {
                desc.setText(adapter.getItem(i).getNews_title());
                //改变当前的点
                dots.getChildAt(i%dots.getChildCount()).setSelected(true);
                if(cacheIndex>=0){
                    dots.getChildAt(cacheIndex%dots.getChildCount()).setSelected(false);
                }
                cacheIndex=i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        Utils.getInstance().getRequest03(urlStr, MyloadBean.class, new Utils.Callback<MyloadBean>() {
            @Override
            public void onSuccess(MyloadBean myloadBean) {
                if(myloadBean ==null){
                    Toast.makeText(getActivity(), "请求错误", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    adapter.setList_image(myloadBean.getData());
                }
                //初始化圆点
                initYuan(myloadBean.getData().size());
                //切换到中间
                int center =adapter.getCount()/2;
                center = center-center%myloadBean.getData().size();
                dianying_viewPager.setCurrentItem(center);
                //开启轮播
                startlopper();
            }
        });

    }
    @SuppressLint("HandlerLeak")
    private Handler Handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            dianying_viewPager.setCurrentItem(dianying_viewPager.getCurrentItem()+1);
            Handler.sendEmptyMessageDelayed(0, 2000);
        }
    };
    private void startlopper(){
        Handler.removeMessages(0);

        Handler.sendEmptyMessageDelayed(0, 2000);
    }

    private void initYuan(int data) {
        dots.removeAllViews();
        for (int i = 0; i < data; i++) {
            ImageView imageView = new ImageView(getActivity());

            imageView.setBackgroundResource(R.drawable.seletor_dot);

            //布局参数: 宽高包裹  左右8dp
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());

            params.leftMargin = margin;

            params.rightMargin = margin;

            dots.addView(imageView, params);

        }
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onResume() {
        super.onResume();
        LoadData();

    }
}

//另外附带两个 draweble
//未选中的圆角图形的背景颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_sel_dot"
        android:state_selected="true" />
    <item android:drawable="@drawable/shape_normal_dot" />
</selector>

//圆角shape

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

//shape选中的颜色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <size
        android:width="8dp"
        android:height="8dp" />
    <solid android:color="#900000" />
</shape>

猜你喜欢

转载自blog.csdn.net/qq_41972756/article/details/84101876