PullToRefresh是很多项目都使用的刷新加载框架,大多数情况下我们不需要对源码做任何调整,它提供的强大配置功能就足以满足我们的需求,而且框架已经相当稳定,正因如此受到大家的欢迎!
PullToRefresh需要和module进行绑定,步骤如下:
然后在 New Module界面选择已经在本地的含有源代码的pullToRefresh。
打开如下图所示的open Module Settings 按钮
点击app中的Dependencies 中右边框的"+"按钮,选择第三个 ,如下所示
选择Modules : pullToRefreshLibrary ,点击OK
然后在build.gradle(Module:app)
或者你自己要写的那个android 程序的根文件夹的build.gradle中加入下面一句话
compile project(':pullToRefreshLibrary')
自此,pullToRefresh已经导入成功,可以新建一个pullToRefrenshListView验证一下。
布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refresh_list_view" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" >
,,,,,,,,,,,,,,,,listView的具体实现的代码
,,,,,,,,,先有一个主界面,然后再主界面中分出子布局(fragment)
package yaozhipu.bwei.com.week1.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import yaozhipu.bwei.com.week1.R; import yaozhipu.bwei.com.week1.Utils.JsonCallBack; import yaozhipu.bwei.com.week1.Utils.NetDataUtils; import yaozhipu.bwei.com.week1.halper.DatadateBean; import yaozhipu.bwei.com.week1.imag.ListViewAdapter; /** * Created by lenovo on 2017/10/18. */ public class FragmentListView extends Fragment { private PullToRefreshListView refreshListView; private Listlist = new ArrayList<>(); private ListViewAdapter myAdapter; private int num=1; private ILoadingLayout startLabels; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_listview, container, false); refreshListView = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //1.设置listView的适配器 getDataFrom(); //2.设置刷新模式 /*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */ refreshListView.setMode(PullToRefreshBase.Mode.BOTH); //3.通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别 (也就是设置向下拉的时候头部里面显示的文字) //此时这里设置的是下拉刷新的时候显示的文字,所以第一个设置true表示现在是刷新, 第二个设置为false startLabels = refreshListView.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新..."); startLabels.setRefreshingLabel("正在刷新..."); startLabels.setReleaseLabel("放开刷新..."); ILoadingLayout endLabels = refreshListView.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新..."); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2 () { //下拉刷新的时候调用的方法....请求第一页的数据,清空之前的数据,然后再添加设置适配器 @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { //下拉刷新....请求第一页的数据,清空之前的数据,然后再添加设置适配器 getReshData(); } //上拉刷新的时候调用的方法..page++,然后在请求数据 @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { //上拉刷新/加载...加载的时候页数++ num++; getDataFrom(); } }); } public void getDataFrom() { NetDataUtils.getData("http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android", getActivity(), new JsonCallBack() { @Override public void getJsonString(String json) { //解析 Gson gson = new Gson(); DatadateBean datadateBean = gson.fromJson(json, DatadateBean.class); Log.i("---",datadateBean.getData()+"---"); list.addAll(datadateBean.getData().getEssay()); setAdapter(); refreshListView.onRefreshComplete(); } }); } private void setAdapter() { if (myAdapter == null){ myAdapter = new ListViewAdapter(getActivity(), list); refreshListView.setAdapter(myAdapter); }else{ myAdapter.notifyDataSetChanged(); } } public void getReshData() { NetDataUtils.getData("http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android", getActivity(), new JsonCallBack() { @Override public void getJsonString(String json) { //解析 Gson gson = new Gson(); DatadateBean datadateBean = gson.fromJson(json, DatadateBean.class); //先清空一下数据 list.clear(); //添加到集合的最前边,,,,(0,,,,) list.addAll(0,datadateBean.getData().getEssay()); setAdapter(); refreshListView.onRefreshComplete(); startLabels.setLastUpdatedLabel("上次更新时间:"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis()))); } }); } }
、、、、、listView进行加载,并且 使用多条目,以及图片适配
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.nostra13.universalimageloader.core.ImageLoader; import java.util.List; import yaozhipu.bwei.com.week1.R; import yaozhipu.bwei.com.week1.halper.DatadateBean; /** * Created by lenovo on 2017/10/18. */ public class ListViewAdapter extends BaseAdapter{ private Context context; private Listlist; private int image1=0; private int image2=1; public ListViewAdapter(Context context, List list) { this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { if (position % 2==0){ return image1; } return image2; } //返回2个布局文件 @Override public int getViewTypeCount() { return 2; } //优化,进行多条目加载 @Override public View getView(int position, View convertView, ViewGroup parent) { if (getItemViewType(position)==image1) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = View.inflate(context, R.layout.layout_list, null); viewHolder.image = (ImageView) convertView.findViewById(R.id.image); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.tiem = (TextView) convertView.findViewById(R.id.time); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.tiem.setText(list.get(position).getHp_makettime()); viewHolder.title.setText(list.get(position).getHp_title()); ImageLoader.getInstance().displayImage(list.get(position).getStart_video(), viewHolder.image, ImageLOaderUtils.getcrioption()); }else if (getItemViewType(position) == image2){ Holder holder = null; if (convertView == null) { holder = new Holder(); convertView = View.inflate(context, R.layout.layout_lists, null); holder.title = (TextView) convertView.findViewById(R.id.title); holder.tiem = (TextView) convertView.findViewById(R.id.time); convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } holder.tiem.setText(list.get(position).getHp_makettime()); holder.title.setText(list.get(position).getHp_title()); } return convertView; } class ViewHolder{ ImageView image; TextView title; TextView tiem; } class Holder{ TextView title; TextView tiem; } }
,,,,,,,,,,,,,,,示例如下