PullToRefresh列表展示
阿新 • • 發佈:2018-11-21
匯入PullToRefresh庫
主佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.pulltolibrary.PullToRefreshListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/Pull_to_Refresh"></com.example.pulltolibrary.PullToRefreshListView> </LinearLayout>
item佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/img_View"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Get_Title"/> </LinearLayout>
對應介面卡
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.bumptech.glide.Glide; import com.example.week3_moni.R; import com.example.week3_moni.bean.MyBean; import java.util.List; public class MyAdapter extends BaseAdapter { private Context mContext; private List<MyBean.DataBean> datas; public MyAdapter(Context mContext, List<MyBean.DataBean> datas) { this.mContext = mContext; this.datas = datas; } @Override public int getCount() { return datas.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) { HolderView holderView = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.c1_adapter, null); holderView = new HolderView(); holderView.img_View = convertView.findViewById(R.id.img_View); holderView.Get_Title = convertView.findViewById(R.id.Get_Title); convertView.setTag(holderView); } else { holderView = (HolderView) convertView.getTag(); } holderView.Get_Title.setText(datas.get(position).getNews_title()); Glide.with(mContext).load(datas.get(position).getPic_url()).into(holderView.img_View); return convertView; } class HolderView { ImageView img_View; TextView Get_Title; } }
MainActivity
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.example.week3_moni.R;
import com.example.week3_moni.adapter.MyAdapter;
import com.example.week3_moni.bean.MyBean;
import com.example.xlistviewflush.view.XListView;
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;
public class C2Fragment extends Fragment implements View.OnClickListener {
private XListView XList_View;
private String mUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private int mStartPage = 1;
private List<MyBean.DataBean> data = new ArrayList<>();
private PullToRefreshListView Pull_to_Refresh;
private MyAdapter adapter;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
data.addAll((List<MyBean.DataBean>) msg.obj);
adapter.notifyDataSetChanged();
//取消一直重新整理的方法
Pull_to_Refresh.onRefreshComplete();
}
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.c2_item, null);
initView(v);
setListener();
getTheData(mStartPage);
return v;
}
private void initView(View v) {
Pull_to_Refresh = (PullToRefreshListView) v.findViewById(R.id.Pull_to_Refresh);
Pull_to_Refresh.setMode(PullToRefreshBase.Mode.BOTH);
adapter = new MyAdapter(getActivity().getApplicationContext(), data);
Pull_to_Refresh.setAdapter(adapter);
}
private void setListener() {
Pull_to_Refresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉重新整理
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
mStartPage = 1;
data.clear();
getTheData(mStartPage);
}
//上拉載入
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
mStartPage++;
getTheData(mStartPage);
}
});
}
private void getTheData(final int i) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(mUrl + i);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Gson gson = new Gson();
MyBean myBean = gson.fromJson(reader, MyBean.class);
mHandler.sendMessage(mHandler.obtainMessage(0, myBean.getData()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onClick(View v) {
}
}