欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

轮播图

程序员文章站 2023-12-30 16:39:10
...

.activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TwoActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp3"
        android:layout_width="wrap_content"
        android:layout_height="250dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#88000000"
        android:orientation="vertical"
        android:padding="8dp"
        android:gravity="center_horizontal"
        android:layout_alignBottom="@+id/vp3">

        <LinearLayout
            android:id="@+id/dian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

.TwoActivity

package com.example.lll.yuekaomoni;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lll.yuekaomoni.adapter.BannerAdapter;
import com.example.lll.yuekaomoni.bean.BNews;
import com.example.lll.yuekaomoni.util.Httputil;
import com.google.gson.Gson;

import java.util.List;

public class TwoActivity extends BaseActivity {

    private ViewPager vp3;
    private BannerAdapter bannerAdapter;
    private LinearLayout dian;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
     
       int currentItem = vp3.getCurrentItem();
            if (currentItem==bannerAdapter.getList().size()-1){
                currentItem=0;
            }else{
                currentItem++;
            }
            vp3.setCurrentItem(currentItem);
            handler.sendEmptyMessageDelayed(0,2000);
        }
    };

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        vp3 = findViewById(R.id.vp3);
        dian = findViewById(R.id.dian);

        bannerAdapter = new BannerAdapter(getLayoutInflater());
        vp3.setAdapter(bannerAdapter);
        httpData();
        handler.sendEmptyMessageDelayed(0,2000);
    }

    private void httpData() {
        final String path = "http://www.xieast.com/api/banner.php";

        new AsyncTask<String,String,List<BNews.DataBean>>(){
            @Override
            protected List<BNews.DataBean> doInBackground(String... strings) {
                //调用网络请求工具类
                String string = Httputil.getString(path);
                BNews bNews = new Gson().fromJson(string, BNews.class);
                return bNews == null ? null : bNews.getData();
            }

            @Override
            protected void onPostExecute(List<BNews.DataBean> resultData) {
                super.onPostExecute(resultData);
                bannerAdapter.setDatas(resultData);
            }
        }.execute(path);
    }


    @Override
    protected int initContentView() {
        return R.layout.activity_two;
    }
}

上一篇:

下一篇: