Android-图片的无限轮播
Posted 白日梦游
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android-图片的无限轮播相关的知识,希望对你有一定的参考价值。
//以下是Handler中的操作
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//获得当前的索引
int currentItem = vp.getCurrentItem();
//设置索引加1
int i = currentItem + 1;
vp.setCurrentItem(i);
handler.sendEmptyMessageDelayed(8, 3000);
}
};
----------------------------------------------分割线 -----------------------------------------------------------
//以下为OnCreate中的操作
List<Integer> list;
vp.setAdapter(new HAdapter(getActivity(),list));
//是图片的初始位置在中间
vp.setCurrentItem(Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%list.size());
handler.sendEmptyMessageDelayed(9, 3000);
----------------------------------------------分割线 -----------------------------------------------------------
//以下为适配器中的操作
package com.example.adapter;
import java.util.List;
import com.example.cartoon.R;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class HAdapter extends PagerAdapter {
private List<Integer> list;
private Context context;
public HAdapter(Context context,List<Integer> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
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) {
View view = View.inflate(context, R.layout.viewpager_item, null);
ImageView img = (ImageView)view.findViewById(R.id.img_viewpager);
img.setBackgroundResource(list.get(position%list.size()));
container.addView(view);
return view;
}
}
package com.example.fragment;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import com.example.adapter.GVHAdapter;
import com.example.adapter.HAdapter;
import com.example.cartoon.BookActivity;
import com.example.cartoon.R;
import com.example.vo.Category;
import com.google.gson.Gson;
public class HomeFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
private List<Integer> list;
private ViewPager vp;
private String path = "http://japi.juhe.cn/comic/category";
private String key = "8f0f9a1f31d02807d5196fa622cde039";
private List<Integer> imgList = new ArrayList<Integer>();
private List<String> dataList;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//获得当前的索引
int currentItem = vp.getCurrentItem();
//设置索引加1
int i = currentItem + 1;
vp.setCurrentItem(i);
handler.sendEmptyMessageDelayed(8, 3000);
}
};
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
dataList = (List<String>) msg.obj;
gv.setAdapter(new GVHAdapter(getActivity(),dataList,imgList));
//设置点击事件
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getActivity(),BookActivity.class);
intent.putExtra("typeName", dataList.get(position));
startActivity(intent);
}
});
}
};
private GridView gv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, null);
vp = (ViewPager)view.findViewById(R.id.vp_fragment_home);
gv = (GridView)view.findViewById(R.id.gv_fragment_home);
initHView();
new Thread(){
@Override
public void run() {
getHData();
}
}.start();
vp.setAdapter(new HAdapter(getActivity(),list));
vp.setCurrentItem(Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%list.size());
handler.sendEmptyMessageDelayed(9, 3000);
return view;
}
//创建一个初始化的数据
public void initHView(){
list = new ArrayList<Integer>();
//加载布局
list.add(R.drawable.vp1);
list.add(R.drawable.vp2);
list.add(R.drawable.vp3);
list.add(R.drawable.vp4);
imgList.add(R.drawable.shaonian);
imgList.add(R.drawable.qingnian);
imgList.add(R.drawable.shaonv);
imgList.add(R.drawable.danmei);
}
//创建一个解析数据的方法
public void getHData(){
//获得HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
//使用Post方法
HttpPost httpPost = new HttpPost(path);
//得到集合
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("key", key));
try {
//设置实体类
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
//
httpPost.setEntity(encodedFormEntity);
//开始请求
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = httpResponse.getEntity();
String data = EntityUtils.toString(httpEntity);
System.out.println(data);
//获得Gson对象
Gson gson = new Gson();
//解析数据
Category category = gson.fromJson(data, Category.class);
List<String> result = category.getResult();
Message msg = Message.obtain();
msg.obj = result;
mHandler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是关于Android-图片的无限轮播的主要内容,如果未能解决你的问题,请参考以下文章