无限轮播+gridview






 





  //布局页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:orientation="vertical"
    >



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="240dp">

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

                <LinearLayout
                    android:id="@+id/ll_points"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_alignParentBottom="true"
                    android:gravity="center"
                    android:orientation="horizontal"></LinearLayout>
            </RelativeLayout>

<bwei.com.day_0523_demo1.MyGridView
    android:id="@+id/main_gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"></bwei.com.day_0523_demo1.MyGridView>

        </LinearLayout>



</LinearLayout>

//获取网络请求工具类


package bwei.com.day_0523_demo1.https;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import bwei.com.day_0523_demo1.utils.ConUtils;

public class HttpUtils {


    private static final String TAG = "HttpUtils-----";
    //单例模式
    private static HttpUtils httpUtils = new HttpUtils();

    private static final int SUCCESS = 0;
    private static final int ERROR = 1;
    private HttpListener httpListener;


    //设置handler事件处理机制

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case SUCCESS:
                    String json = (String) msg.obj;
                    httpListener.getSuccess(json);
                    break;

                case ERROR:
                    String error = (String) msg.obj;
                    httpListener.getError(error);
                    break;
            }


        }
    };


    //构造方法私有化
    private HttpUtils() {
    }

//判断

    public static HttpUtils getHttpUtils() {

        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        return httpUtils;


    }


    //封装get方法
    public void get(final String url) {

        //开启线程

        new Thread() {
            @Override
            public void run() {

                try {
                    URL u = new URL(url);

                    HttpURLConnection con = (HttpURLConnection) u.openConnection();
                    con.setConnectTimeout(5000);

                    if (con.getResponseCode() == 200) {

                        InputStream inputStream = con.getInputStream();
                        String json = ConUtils.in(inputStream);

                        //发送消息
                        Message me = new Message();
                        me.what = SUCCESS;
                        me.obj = json;
                        //发送handler请求
                       handler.sendMessage(me);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                    //发送消息
                    Message me = new Message();
                    me.what = ERROR;
                    me.obj = e.getMessage();
                    //发送handler请求
                    handler.sendMessage(me);
                }

            }
        }.start();


    }


    //接口回调
    public interface HttpListener {

        void getSuccess(String json);

        void getError(String error);
    }


//设置外部访问的方法

    public void setHttpListener(HttpListener httpListener) {

        this.httpListener = httpListener;
    }

    //判断是否有网的方法
    public boolean hasNet(Context context) {

        ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = con.getActiveNetworkInfo();

        return networkInfo != null;

    }
}


//工具类

package bwei.com.day_0523_demo1.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

public class ConUtils {


    public static String in(InputStream inputStream) throws IOException {

        InputStreamReader is = new InputStreamReader(inputStream);

        BufferedReader br = new BufferedReader(is);

        String s;

        StringBuffer stringBuffer = new StringBuffer();


        while((s=br.readLine())!=null){

            stringBuffer.append(s);

        }

        return stringBuffer.toString();
    }

}

//My App图片加载


package bwei.com.day_0523_demo1;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Handler;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader();
    }

    private void initImageLoader() {
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(this);

        //Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);
    }

    public static DisplayImageOptions getOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .displayer(new FadeInBitmapDisplayer(500))
                .handler(new Handler()) // default
                .build();
        return options;
    }
}


//My GridView

package bwei.com.day_0523_demo1;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 *
 * 为了解决和ScrollView嵌套时候只显示一行的问题
 *
 */
public class MyGridView extends GridView{
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //因为和尺寸相关,所以在onMeasure里面处理,哪里有问题处理那个

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, height);
    }
}












 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值