侧滑,无线轮播,Xlistview

主页代码:
package com.example.studioyuekaofuxi;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studioyuekaofuxi.User.XListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

    private ViewPager vp;
    private XListView xlv;

    private List list=new ArrayList();
    private Handler handler=new Handler(){



        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int item = vp.getCurrentItem();
            item++;
            vp.setCurrentItem(item);
            send();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        xlv = (XListView) findViewById(R.id.xlv);
        vpinit();
        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);
    }
    private void vpinit(){

        new Thread(){
            private ByteArrayOutputStream outputStream;
            String path="http://www.meirixue.com/api.php?c=index&a=index";
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    int code = connection.getResponseCode();
                    if (code==200){
                        InputStream inputStream = connection.getInputStream();
                        outputStream = new ByteArrayOutputStream();
                        byte[] read=new  byte[1024];
                        int len=-1;
                        while((len=inputStream.read(read))!=-1){
                            outputStream.write(read,0,len);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //Toast.makeText(MainActivity.this,outputStream.toString(),Toast.LENGTH_LONG).show();
                        try {
                            JSONObject jsonObject = new JSONObject(outputStream.toString());
                            JSONObject data = jsonObject.getJSONObject("data");
                            JSONArray adlist = data.getJSONArray("adlist");
                            for (int x=0;x"img");
                                String name = object.getString("name");
                                Userbean userbean = new Userbean();
                                userbean.setImg(img);
                                userbean.setName(name);
                                list.add(userbean);
                                vp.setAdapter(new Myter());
                                vp.setCurrentItem(10000);
                                xlv.setAdapter(new Myasdf());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                send();
                                Message message = new Message();
                                message.obj=list;
                                handler.sendMessage(message);
                            }
                        });
                    }
                });
            }
        }.start();
    }
    private void send(){
        handler.sendEmptyMessageDelayed(0,2000);
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
    vpinit();
    }

    class  Myasdf extends BaseAdapter{

        private Haner haner;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                haner = new Haner();
                convertView=View.inflate(MainActivity.this,R.layout.item,null);
                haner.imagename= (ImageView) convertView.findViewById(R.id.imagename);
                haner.textname= (TextView) convertView.findViewById(R.id.textname);
                convertView.setTag(haner);
            }else{
                haner= (Haner) convertView.getTag();
            }
            Userbean userbean = list.get(position);
            haner.textname.setText(userbean.getName());
            ImageLoader.getInstance().displayImage(userbean.getImg(),haner.imagename);
            return convertView;
        }
        class Haner{
            TextView textname;
            ImageView imagename;
        }
    }
    class  Myter extends PagerAdapter{

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = new ImageView(MainActivity.this);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoader.getInstance().displayImage(list.get(position%list.size()).getImg(),view);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}
主页布局:
xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/vp">android.support.v4.view.ViewPager>
    <com.example.studioyuekaofuxi.User.XListView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/xlv" />LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:src="@mipmap/ic_launcher"
        />
android.support.v4.widget.DrawerLayout>
 

你可能感兴趣的:(侧滑,无线轮播,Xlistview)