布局文件
<com.example.pulltolibrary.PullToRefreshListView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/Pull_List"
></com.example.pulltolibrary.PullToRefreshListView>
fragment代码
package com.example.monitwo.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.monitwo.R;
import com.example.monitwo.bean.MyData;
import com.example.monitwo.fragmentadapter.MyAdapter;
import com.example.monitwo.utils.HttpUtils;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
- A simple {@link Fragment} subclass.
*/
public class AAAFragment extends Fragment {
private List<MyData.ResultBean> result=new ArrayList<>();
private View v;
private String mUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?userld=18&sessionld=15320748258726&page=1&count=1";
private PullToRefreshListView Pull_List;
private int count=1;
private MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_aaa, null);
initView(v);
adapter = new MyAdapter(result,getActivity());
Pull_List.setAdapter(adapter);
Pull_List.setMode(PullToRefreshBase.Mode.BOTH);
new MyTask().execute(mUrl+count);
getlisin();
return v;
}
private void initView(View v) {
Pull_List = (PullToRefreshListView) v.findViewById(R.id.Pull_List);
}
public void getlisin(){
Pull_List.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
result.clear();
count=1;
new MyTask().execute(mUrl +count);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
count++;
new MyTask().execute(mUrl +count);
}
});
}
class MyTask extends AsyncTask<String, Void, List<MyData.ResultBean>> {
@Override
protected List<MyData.ResultBean> doInBackground(String... strings) {
try {
String jsonStr = HttpUtils.getData(strings[0]);
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonStr, MyData.class);
return myData.getResult();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<MyData.ResultBean> resultBeans) {
super.onPostExecute(resultBeans);
result.addAll(resultBeans);
adapter.notifyDataSetChanged();
Pull_List.onRefreshComplete();
}
}
}
适配器
package com.example.monitwo.fragmentadapter;
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.example.monitwo.R;
import com.example.monitwo.bean.MyData;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.util.List;
public class MyAdapter extends BaseAdapter {
List<MyData.ResultBean> result;
Context mContext;
private ImageLoader imageLoader=ImageLoader.getInstance();
private DisplayImageOptions imageOptions;
public MyAdapter(List<MyData.ResultBean> result, Context mContext) {
this.result = result;
this.mContext = mContext;
}
@Override
public int getCount() {
return result.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(mContext,R.layout.pull_item,null);
holder=new ViewHolder();
holder.Pull_ImageView=convertView.findViewById(R.id.Pull_ImageView);
holder.Pull_TextView=convertView.findViewById(R.id.Pull_TextView);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.Pull_TextView.setText(result.get(position).getName());
imageLoader.displayImage(result.get(position).getImageUrl(),holder.Pull_ImageView);
return convertView;
}
class ViewHolder{
ImageView Pull_ImageView;
TextView Pull_TextView;
}
}
HttpUtils 工具类
package com.example.monitwo.fragmentadapter;
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.example.monitwo.R;
import com.example.monitwo.bean.MyData;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.util.List;
public class MyAdapter extends BaseAdapter {
List<MyData.ResultBean> result;
Context mContext;
private ImageLoader imageLoader=ImageLoader.getInstance();
private DisplayImageOptions imageOptions;
public MyAdapter(List<MyData.ResultBean> result, Context mContext) {
this.result = result;
this.mContext = mContext;
}
@Override
public int getCount() {
return result.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(mContext,R.layout.pull_item,null);
holder=new ViewHolder();
holder.Pull_ImageView=convertView.findViewById(R.id.Pull_ImageView);
holder.Pull_TextView=convertView.findViewById(R.id.Pull_TextView);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.Pull_TextView.setText(result.get(position).getName());
imageLoader.displayImage(result.get(position).getImageUrl(),holder.Pull_ImageView);
return convertView;
}
class ViewHolder{
ImageView Pull_ImageView;
TextView Pull_TextView;
}
}