XlistView上拉刷新下拉加载+轮播+添加头布局
来源:互联网 发布:手机报表软件下载 编辑:程序博客网 时间:2024/05/20 07:14
头布局viewpager
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="180dp" android:id="@+id/viewpager" ></android.support.v4.view.ViewPager></LinearLayout>主布局
<LinearLayout 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" > <com.example.monthtext.Utils.XListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xxlist" ></com.example.monthtext.Utils.XListView></LinearLayout>listview布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/it_im" android:layout_weight="1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/it_tv" android:textSize="17sp" android:layout_weight="1" /></LinearLayout>
代码文件
Pager适配器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.util.Log;import android.view.View;import com.example.monthtext.Adapters.ListsAdapter;import com.example.monthtext.Adapters.ViewPagerAdapter;import com.example.monthtext.UserBeanBean.User;import com.example.monthtext.Utils.XListView;import com.google.gson.Gson;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private ViewPagerAdapter viewPagerAdapter; private List<User.DataBean.ComicsBean> comics; private ListsAdapter listsAdapter;; private XListView xlistview; private int count; private String path = "http://api.kkmh.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veCIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9uZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRkYzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9 "; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: String sss = msg.getData().getString("SSS"); Gson gson = new Gson(); User aa = gson.fromJson(sss, User.class); List<User.DataBean.ComicsBean> fffs = aa.getData().getComics(); comics.addAll(fffs); listsAdapter.notifyDataSetChanged(); viewPagerAdapter.notifyDataSetChanged(); close(); break; case 1: listsAdapter.notifyDataSetChanged(); viewPagerAdapter.notifyDataSetChanged(); close(); break; case 3: //得到当前viewpager的索引 int position = vp.getCurrentItem(); //让当前的的viewpager加1 position++; //给viewPager设置当前的索引 vp.setCurrentItem(position); sendMsg(); break; } } }; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getDataID(); getFristData(); //让viewpager的索引默认的指在100000 vp.setCurrentItem(100000); //自动轮博 sendMsg();// wuxian(); } private void getDataID() { //初始化组件 xlistview = (XListView) findViewById(R.id.xxlist); xlistview.setPullLoadEnable(true); xlistview.setPullRefreshEnable(true); xlistview.setXListViewListener(this); View inflate = View.inflate(MainActivity.this, R.layout.vpxml, null); vp = inflate.findViewById(R.id.viewpager); xlistview.addHeaderView(inflate); } private void getFristData() { new Thread(){ private ByteArrayOutputStream out = new ByteArrayOutputStream(); @Override public void run() { super.run(); try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if(responseCode == 200){ InputStream inputStream = urlConnection.getInputStream(); int read; byte[] buffer = new byte[1024]; while ((read = inputStream.read(buffer))!=-1){ out.write(buffer,0,read); } } } catch (Exception e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { Gson gson = new Gson(); User user = gson.fromJson(out.toString(), User.class); comics = user.getData().getComics(); listsAdapter = new ListsAdapter(comics, MainActivity.this); xlistview.setAdapter(listsAdapter); viewPagerAdapter = new ViewPagerAdapter(comics, MainActivity.this); vp.setAdapter(viewPagerAdapter); } }); } }.start(); } //刷新 @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { comics.clear(); new Thread(){ private ByteArrayOutputStream out; @Override public void run() { super.run(); try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); out = new ByteArrayOutputStream(); if(responseCode == 200){ InputStream inputStream = urlConnection.getInputStream(); int read; byte[] buffer = new byte[1024]; while ((read = inputStream.read(buffer))!=-1){ out.write(buffer,0,read); } Message message = new Message(); Bundle bundle = new Bundle(); bundle.putString("SSS", out.toString()); message.setData(bundle); message.what=0; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } },2000); } //加载 @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { comics.addAll(comics); handler.sendEmptyMessage(1); } },2000); } //关闭 public void close(){ xlistview.stopLoadMore(); xlistview.stopRefresh(); xlistview.setRefreshTime("刚刚"); } public void wuxian(){ Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(3); } },0,2000); } private void sendMsg() { handler.sendEmptyMessageDelayed(3, 2000); }}
import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import com.example.monthtext.MainActivity;import com.example.monthtext.R;import com.example.monthtext.UserBeanBean.User;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by xsj on 2017/9/20. */public class ViewPagerAdapter extends PagerAdapter { private List<User.DataBean.ComicsBean> list; private Context context; public ViewPagerAdapter(List<User.DataBean.ComicsBean> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); //在这里记得磨掉集合的全部 ImageLoader.getInstance().displayImage(list.get(position%list.size()).getCover_image_url(),imageView); container.addView(imageView); return imageView; } }ListView适配器
import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.monthtext.R;import com.example.monthtext.UserBeanBean.User;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by xsj on 2017/9/20. */public class ListsAdapter extends BaseAdapter { private List<User.DataBean.ComicsBean> list; private Context context; private LayoutInflater inflater; public ListsAdapter(List<User.DataBean.ComicsBean> list, Context context) { this.list = list; this.context = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View conview, ViewGroup viewGroup) { ViewHoder hoder = new ViewHoder(); if(conview == null){ conview = inflater.inflate(R.layout.xlistviewitem,null); hoder.im=(ImageView)conview.findViewById(R.id.it_im); hoder.tv=(TextView) conview.findViewById(R.id.it_tv); conview.setTag(hoder); }else{ hoder = (ViewHoder) conview.getTag(); } ImageLoader.getInstance().displayImage(list.get(i).getCover_image_url(),hoder.im); hoder.tv.setText(list.get(i).getTitle()); return conview; } class ViewHoder{ TextView tv; ImageView im; }}
阅读全文
0 0
- XlistView上拉刷新下拉加载+轮播+添加头布局
- XlistView的上拉刷新下拉加载 和Fragment 和无限轮播
- XlistView上拉刷新下拉加载
- xListView上拉刷新下拉加载
- xListView上拉加载下拉刷新
- XListView上拉加载,下拉刷新
- xlistview上拉加载下拉刷新
- XListView上拉加载下拉刷新
- XListView上拉刷新,下拉加载
- 上拉刷新--下拉加载XListView
- XListView上拉加载下拉刷新
- xlistview上拉刷新、下拉加载
- XListView上拉加载下拉刷新
- XListView上拉加载下拉刷新
- XListView上拉加载下拉刷新
- Xlistview上拉加载下拉刷新
- xlistview上拉刷新,下拉加载
- 上拉刷新下拉加载xlistview-------string
- 解决 Android Studio 创建项目时极其的慢的尴尬
- The activity must be exported or contain an intent-filter解决方法
- office 系列产品 -- visio 2013 / project 2013 破解工具
- spring概述
- CXF部署到weblogic,客户端访问报错:Cannot create a secure XMLInputFactory
- XlistView上拉刷新下拉加载+轮播+添加头布局
- STP协议作用及工作过程
- bzoj 1433: [ZJOI2009]假期的宿舍
- 基于空闲资源的弹性计算实践
- ifix5.8实时报警
- 3747: [POI2015]Kinoman
- Viewpager OnPageChangeListener 滑动事件讲解
- ifix5.8历史报警-保存历史报警到sqlserver2012
- C#调用默认浏览器打开网页的几种方法