//一个Main总布局的pull 一个无限轮播适配器图片的布局
一个无限轮播v4包的布局 一个pull的适配器的布局 文字图片
package com.example.yue1;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
String path="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603";
private PullToRefreshListView pull;
private ViewPager vp;
private LinearLayout ll;
List<User.ResultBean.RowsBean> arr=new ArrayList<User.ResultBean.RowsBean>();
private Madapter madapter;
private ArrayList<ImageView> ivlist;
private ImageView iv;
private User.ResultBean result;
private Madapters madapters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull =(PullToRefreshListView)findViewById(R.id.pull_refresh_list);//控件
pull.setMode(PullToRefreshBase.Mode.BOTH);//上下拉
//添加头布局
ListView ListView = pull.getRefreshableView();
View view = View.inflate(MainActivity.this, R.layout.vp, null);
ListView.addHeaderView(view);
vp =(ViewPager)view.findViewById(R.id.vp);//控件
ll =(LinearLayout)view.findViewById(R.id.ll);
madapter = new Madapter();//无限轮播的适配器
vp.setAdapter(madapter);
new MasayTask().execute(path);//数据
//pull的监听
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<android.widget.ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
arr.clear();
new MasayTask().execute(path);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
new MasayTask().execute(path);
}
});
//无限轮播的滑动
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for(int x=0;x<ivlist.size();x++){
if (position%arr.size()==x){
ivlist.get(position%arr.size()).setImageResource(R.drawable.dot_focuable);
}else{
ivlist.get(x).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//自动2秒滑动
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
int i = vp.getCurrentItem()+1;
vp.setCurrentItem(i);
}
});
}
},2000,2000);
}
//小圆点
private void initdote() {
ivlist = new ArrayList<>();
if (ivlist!=null){
ivlist.clear();
}
for (int i = 0; i <arr.size(); i++) {
iv = new ImageView(MainActivity.this);
if (i==0){
iv.setImageResource(R.drawable.dot_focuable);
}else{
iv.setImageResource(R.drawable.dot_normal);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
layoutParams.setMargins(10,0,10,0);
ll.addView(iv,layoutParams);
ivlist.add(iv);
}
}
//适配器无限轮播
class Madapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View inflate = View.inflate(MainActivity.this, R.layout.imgg,null);
ImageView img = (ImageView)inflate.findViewById(R.id.img);
ImageLoader.getInstance().displayImage(arr.get(position%arr.size()).getInfo().getDefault_image(),img);
container.addView(inflate);
return inflate;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
//异步
class MasayTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String string="";
while ((string=bufferedReader.readLine())!=null){
builder.append(string);
}
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
User user = gson.fromJson(s, User.class);
result = user.getResult();
List<User.ResultBean.RowsBean> rows = result.getRows();
arr.addAll(rows);//创建新集合
initdote();//小圆点
madapters = new Madapters();//pull的监听
pull.setAdapter(madapters);
madapter.notifyDataSetChanged();//刷新适配器
pull.onRefreshComplete();//隐藏
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
//pull的适配器
class Madapters extends BaseAdapter{
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int position) {
return arr.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this,R.layout.item_shitu,null);
ImageView im = (ImageView) convertView.findViewById(R.id.ima);
TextView tv=(TextView) convertView.findViewById(R.id.tv);
ImageLoader.getInstance().displayImage(arr.get(position).getInfo().getDefault_image(),im);
tv.setText(arr.get(position).getInfo().getTags());
return convertView;
}
}
}