Android开发轮播图Demo

首先声明一下:布局文件里面的某些属性是不完整的,因为如果要完整粘贴完的话,设计的大大小小的xml文件比较多。这篇博文主要是记录轮播图实现逻辑: 


import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
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 java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG="yly";
    private ViewPager viewPager;
    private TextView tvTitle;
    private LinearLayout llPointGroup;

    private final int[] imageIds={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
    private final int[] tvTitles={R.string.a,R.string.b,R.string.c,R.string.d,R.string.e,R.string.f};

    private int prePosition=0;
    private List imageViewList;

    private boolean isDradding=false;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int item=viewPager.getCurrentItem()+1;
            viewPager.setCurrentItem(item);

            handler.sendEmptyMessageDelayed(0,2000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager=findViewById(R.id.view_pager);
        tvTitle=findViewById(R.id.tv_title);
        llPointGroup=findViewById(R.id.ll_point_group);

        imageViewList=new ArrayList<>();
        for (int i=0;i

布局文件:




    
    
        
        
    

 

你可能感兴趣的:(Android,实例小Demo)