//mian主页面
import android.graphics.pdf.PdfDocument;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.lianxiyuekao.R;
import com.example.lianxiyuekao.adapter.MAdapter;
import com.example.lianxiyuekao.gsonbean.Bean;
import com.example.lianxiyuekao.utile.HttpUtile;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class Fragmenthome extends Fragment {
private PullToRefreshListView plv;
private List<Bean.DataBean> list=new ArrayList<>();
private MAdapter mAdapter;
private int page=1;
private String url;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragmenthome,container,false);
plv = view.findViewById(R.id.plv);
plv.setMode(PullToRefreshListView.Mode.BOTH);
mAdapter = new MAdapter(getActivity(), list);
url=getArguments().getString("url");
plv.setAdapter(mAdapter);
new MyTask().execute(url+page);
setListener();
return view;
}
public void setListener(){
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page=1;
list.clear();
new MyTask().execute(url+page);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
new MyTask().execute(url+page);
}
});
}
class MyTask extends AsyncTask<String,Void,List<Bean.DataBean>>{
@Override
protected List<Bean.DataBean> doInBackground(String... strings) {
try {
String s = HttpUtile.get(strings[0]);
Bean bean = new Gson().fromJson(s, Bean.class);
return bean.getData();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Bean.DataBean> resultBeans) {
super.onPostExecute(resultBeans);
list.addAll(resultBeans);
mAdapter.notifyDataSetChanged();
plv.onRefreshComplete();
}
}
}
//madapter
import android.content.Context;
import android.graphics.Bitmap;
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.lianxiyuekao.R;
import com.example.lianxiyuekao.gsonbean.Bean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
public class MAdapter extends BaseAdapter {
private Context context;
private List<Bean.DataBean> list=new ArrayList<>();;
private final DisplayImageOptions display;
private ImageLoader imageLoader=ImageLoader.getInstance();
public MAdapter(Context context, List<Bean.DataBean> list) {
this.context = context;
this.list = list;
display = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.showImageOnFail(R.mipmap.ic_launcher)
.displayer(new RoundedBitmapDisplayer(20))
.build();
}
@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 View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView==null){
holder=new Holder();
convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
holder.title =convertView.findViewById(R.id.title);
holder.image =convertView.findViewById(R.id.image);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
holder.title.setText(list.get(position).getNews_title());
imageLoader.displayImage(list.get(position).getPic_url(),holder.image,display);
return convertView;
}
class Holder{
ImageView image;
TextView title;
}
}
//app
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration imageLoader=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(imageLoader);
}
}
//Utile
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
public class HttpUtile {
public static String get(String urlStr) {
String result="";
try {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
if (httpURLConnection.getResponseCode()==200){
result=getInputStr(httpURLConnection.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private static String getInputStr(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader isr = new BufferedReader(new InputStreamReader(inputStream));
for (String temp=isr.readLine();temp!=null;temp=isr.readLine()){
stringBuilder.append(temp);
}
return stringBuilder.toString();
}
}
//mian布局
<?xml version="1.0" encoding="utf-8"?>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plv"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
</com.handmark.pulltorefresh.library.PullToRefreshListView>
//item布局
<?xml version="1.0" encoding="utf-8"?>