无限轮播+搜索

1.主类

 

package com.example.shipilianxi;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.R.id.list;

public class MainActivity extends AppCompatActivity {
   int i=0;
    private EditText name;
    private String s;
    private String path;
    private ViewPager vp;
Handler h=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        i++;
        vp.setCurrentItem(i);
    }
};

    private ListView lv;
    private ImageView aa;
    private TextView textview1;
    private TextView textview2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aa = (ImageView) findViewById(R.id.aa);
        name = (EditText) findViewById(R.id.name);
        vp = (ViewPager) findViewById(R.id.vp);
        textview1 = (TextView) findViewById(R.id.textview1);
        textview2 = (TextView) findViewById(R.id.textview2);
        jishi();
    }

    private void jishi() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
            h.sendEmptyMessage(i);
            }
        },0,1000);
    }

    public void go(View view){
        s = name.getText().toString();
        AsyncTask<Void,Void,String> asyncTask =new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    path = "http://op.juhe.cn/onebox/movie/video?key=90daf4317665b4ead0f7566e3cbb169d&q="+ URLEncoder.encode(s,"utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    URL url = new URL(path);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setReadTimeout(5000);
                    urlConnection.setConnectTimeout(5000);
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode==200){
                        InputStream inputStream = urlConnection.getInputStream();
                        String s = convertStreamToString(inputStream);
                        return s;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(String s) {
                Gson gson = new Gson();
                Data data = gson.fromJson(s, Data.class);
                //无线轮播集合
                List<Data.ResultBean.VideoRecBean> video_rec = data.getResult().getVideo_rec();
                //图片
                String desc = data.getResult().getCover();
                //标题
                String title = data.getResult().getTitle();
                //国际
                String area = data.getResult().getArea();
                //适配器
                Myadapter myadapter = new Myadapter(video_rec,MainActivity.this);
                vp.setAdapter(myadapter);
                //设置图片
                ImageLoader.getInstance().displayImage(desc,aa,ImageUrl.getto());
                //设置标题
                textview1.setText("影片名称:"+title);
                //设置国家
                textview2.setText("国际:"+area);
            }
        };
        asyncTask.execute();
    }
    /**
     * 将输入流转成字符串
     *
     * @param is
     *            输入流
     * @return 返回字符串
     */
    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

2.适配器

 

 

package com.example.shipilianxi;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2017/10/11 0011.
 */

public class Myadapter extends PagerAdapter {
    List<Data.ResultBean.VideoRecBean> list;
    Context con;
    public Myadapter(List<Data.ResultBean.VideoRecBean> list, Context con) {
        this.list = list;
        this.con = con;
    }

    //向一个方向能滑动n多次
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;  //
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0==arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgv=new ImageView(con);  //创建 图片控件
        int index= position%list.size();  //取余数  3--0  4---1  5---2  6--0
        ImageLoader.getInstance().displayImage(list.get(index).getCover(),imgv,ImageUrl.getto());
        container.addView(imgv);
        return imgv;
    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值