本文主要是介绍xlistview网络请求数据以及添加headerview_自动轮播viewpager控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主页代码:
package com.bwie.xlistview;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;import com.bwie.xlistview.ADAPTER.ViewPagerAdapter;
import com.bwie.xlistview.ADAPTER.XlistviewAdapter;
import com.bwie.xlistview.API.API;
import com.bwie.xlistview.BEAN.CartoonInfo;
import com.google.gson.Gson;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;import imageloader.bwie.com.mylibrary.XListView;public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener, ViewPager.OnPageChangeListener {private XListView xlistview;private XlistviewAdapter xlvadap;private boolean isfresh=false;private int SKIP=0;private networkreceiver receiver;private ViewPager viewpager;private List<String> imageurls=new ArrayList<>();private List<ImageView> imageviews=new ArrayList<>();private LinearLayout linear_dot;private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);int currentItem = viewpager.getCurrentItem();if (currentItem>=Integer.MAX_VALUE-1) {currentItem=0;}currentItem++;viewpager.setCurrentItem(currentItem);handler.sendEmptyMessageDelayed(0,1000);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//注册广播判断网络状态registerNetWorkReceiver();//获取控件initView();initViewPager();initData();}private void initViewPager() {imageurls.add(API.VIEWPAGER_IMAGEURL1);imageurls.add(API.VIEWPAGER_IMAGEURL2);imageurls.add(API.VIEWPAGER_IMAGEURL3);//画点drawDot();ViewPagerAdapter adpater=new ViewPagerAdapter(imageurls,this);viewpager.setAdapter(adpater);//启动轮播handler.sendEmptyMessageDelayed(0,1000);}private void drawDot() {if (imageurls!=null) {for (int i=0;i<imageurls.size();i++){ImageView image=new ImageView(this);if (i==0) {image.setImageResource(R.drawable.red_dot);}else{image.setImageResource(R.drawable.white_dot);}linear_dot.addView(image);imageviews.add(image);}}}private void registerNetWorkReceiver() {if (receiver==null) {receiver = new networkreceiver();}IntentFilter filter=new IntentFilter();filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);this.registerReceiver(receiver,filter);}private void initData() {new asynctask().execute(API.DATA_URL);}private void initView() {xlistview = (XListView) findViewById(R.id.xlistview);View header = View.inflate(this, R.layout.xlistview_headerview, null);viewpager = header.findViewById(R.id.viewpager);linear_dot = header.findViewById(R.id.linear_dot);xlistview.addHeaderView(header);xlistview.setPullRefreshEnable(true);xlistview.setPullLoadEnable(true);xlistview.setXListViewListener(this);viewpager.setOnPageChangeListener(this);}@Overridepublic void onRefresh() {isfresh=true;if (xlvadap!=null) {xlvadap=null;new asynctask().execute(API.DATA_URL);}}@Overridepublic void onLoadMore() {isfresh=false;SKIP=SKIP+20;String dataUrlSkip = API.DATA_URL_SKIP_FRONT+SKIP+API.DATA_URL_SKIP_AFTER;new asynctask().execute(dataUrlSkip);}@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {if (imageviews!=null) {for (int i = 0; i < imageviews.size(); i++) {ImageView imageView = imageviews.get(i);if (i==position%imageviews.size()) {imageView.setImageResource(R.drawable.red_dot);}else{imageView.setImageResource(R.drawable.white_dot);}}}}@Overridepublic void onPageScrollStateChanged(int state) {}class asynctask extends AsyncTask<String,Integer,List<String>>{@Overrideprotected List<String> doInBackground(String... strings) {StringBuffer buffer=new StringBuffer();try {URL url=new URL(strings[0]);HttpURLConnection httpurlcon = (HttpURLConnection) url.openConnection();httpurlcon.connect();int responseCode = httpurlcon.getResponseCode();if (200== responseCode) {InputStream inputStream = httpurlcon.getInputStream();BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));String line=null;while ((line=reader.readLine())!=null) {buffer.append(line);}ParseData(buffer.toString());}} catch (Exception e) {e.printStackTrace();}return null;}private void ParseData(String s) {CartoonInfo cartoonInfo = new Gson().fromJson(s, CartoonInfo.class);final List<CartoonInfo.Book> bookList = cartoonInfo.result.bookList;runOnUiThread(new Runnable() {@Overridepublic void run() {if (bookList!=null) {if (xlvadap==null) {xlvadap = new XlistviewAdapter(MainActivity.this,bookList);xlistview.setAdapter(xlvadap);}else{if (isfresh==false){xlvadap.add(bookList);xlvadap.notifyDataSetChanged();}xlvadap.notifyDataSetChanged();}}}});xlistview.stopLoadMore();xlistview.stopRefresh();}}public class networkreceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);NetworkInfo networkInfo1 = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);if (!networkInfo.isConnected()&&!networkInfo1.isConnected()) {System.out.println("网络已断开");Toast.makeText(context,"网络已断开",Toast.LENGTH_LONG).show();//跳转到系统网络设置页面
// Intent intent1 = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
// startActivity(intent1);}else{Toast.makeText(context,"网络已连接",Toast.LENGTH_LONG).show();}}}
}
Xlistview适配器代码:
package com.bwie.xlistview.ADAPTER;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import com.bwie.xlistview.BEAN.CartoonInfo;
import com.bwie.xlistview.R;
import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/*** Created by Administrator on 2017/8/23.*/public class XlistviewAdapter extends BaseAdapter {private Context context;private List<CartoonInfo.Book> list;private static int ITEM_TYPE_COUNT=2;final private static int ITEM_TYPE_A=0;final private static int ITEM_TYPE_B=1;public XlistviewAdapter(Context context, List<CartoonInfo.Book> list) {this.context = context;this.list = list;}public void add(List<CartoonInfo.Book> bookList){list.addAll(bookList);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int i) {return list.get(i);}@Overridepublic long getItemId(int i) {return i;}@Overridepublic int getItemViewType(int position) {if (position%2==0) {return ITEM_TYPE_A;}else{return ITEM_TYPE_B;}}@Overridepublic int getViewTypeCount() {return ITEM_TYPE_COUNT;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {int itemViewType = getItemViewType(i);ViewHolder viewholder;if (view==null) {viewholder=new ViewHolder();switch (itemViewType){case ITEM_TYPE_A:view=View.inflate(context, R.layout.clistview_itemtype_a,null);view.setTag(viewholder);viewholder.iv=view.findViewById(R.id.iv_a);viewholder.tv=view.findViewById(R.id.tv_a);break;case ITEM_TYPE_B:view=View.inflate(context, R.layout.clistview_itemtype_b,null);view.setTag(viewholder);viewholder.iv=view.findViewById(R.id.iv_b);viewholder.tv=view.findViewById(R.id.tv_b);break;}}else{viewholder= (ViewHolder) view.getTag();}viewholder.tv.setText(list.get(i).name);ImageLoader.getInstance().displayImage(list.get(i).coverImg,viewholder.iv);return view;}class ViewHolder{public ImageView iv;public TextView tv;}
}
viewpager代码:
package com.bwie.xlistview.ADAPTER;import android.content.Context;
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/8/24.*/public class ViewPagerAdapter extends PagerAdapter {private List<String> imageurls;private Context context;public ViewPagerAdapter(List<String> imageurls, Context context) {this.imageurls = imageurls;this.context = context;}@Overridepublic int getCount() {return Integer.MAX_VALUE;}@Overridepublic boolean isViewFromObject(View view, Object object) {return view==object;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}@Overridepublic Object instantiateItem(ViewGroup container, int position) {String imageurl = imageurls.get(position%imageurls.size());ImageView imageview=new ImageView(context);ImageLoader.getInstance().displayImage(imageurl,imageview);container.addView(imageview);return imageview;}
}
这篇关于xlistview网络请求数据以及添加headerview_自动轮播viewpager控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!