輪播圖、pulltofresh上拉重新整理,下拉載入
@輪播圖、pulltofresh上拉重新整理,下拉載入TOC
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import com.bwei.day13_xiawu.MainActivity;
import com.bwei.day13_xiawu.R;
import com.bwei.day13_xiawu.adapters.AdAdapter;
import com.bwei.day13_xiawu.adapters.MyAdapter2;
import com.bwei.day13_xiawu.app.MyApp;
import com.bwei.day13_xiawu.beans.AdBean;
import com.bwei.day13_xiawu.beans.NewsBean;
import com.bwei.day13_xiawu.https.HttpConfig;
import com.bwei.day13_xiawu.https.HttpUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
/**
- 主頁
- 1.輪播圖
- 2.網路資料
- 3.下拉,上啦
*/
public class FoodFragment extends BaseFragment {
private View view; private ListView listView; private ViewPager viewPager; private MyHandler myHandler = new MyHandler(); private int page = 1; private int type = 0; List<NewsBean.DataBean> list = new ArrayList<>(); private MyAdapter2 myAdapter2; private PullToRefreshScrollView scrollView; @Override protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.food_fg_layout, null);
// 初始化頁面
initViews();
return view;
}
private void initViews() {
scrollView = view.findViewById(R.id.sy_pullto);
listView = view.findViewById(R.id.sy_listview);
viewPager = view.findViewById(R.id.sy_viewpager);
// 上啦 下拉
scrollView.setMode(PullToRefreshBase.Mode.BOTH);
scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
page = 1;
type = 1;
getDataFromNet();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
page++;
type = 2;
getDataFromNet();
}
});
}
@Override
protected void getDataFromNet() {
Log.d(TAG, "開始請求--------: ");
HttpUtils httpUtils = HttpUtils.getHttpUtils();
httpUtils.get(HttpConfig.listurl + page);
httpUtils.setOnHttpLoadListener(new HttpUtils.HttpLoadListener() {
@Override
public void loadSuccess(String json) {
Log.d(TAG, "loadSuccess:----- " + json);
Gson gson = new Gson();
NewsBean newsBean = gson.fromJson(json, NewsBean.class);
List<NewsBean.DataBean> list1 = newsBean.getData();
switch (type) {
case 0://第一次載入
myAdapter2 = new MyAdapter2(MyApp.getContext(), list1);
listView.setAdapter(myAdapter2);
break;
case 1://下拉重新整理
MyAdapter2 myAdapter3 = new MyAdapter2(MyApp.getContext(), list1);
listView.setAdapter(myAdapter3);
scrollView.onRefreshComplete();
break;
case 2://上啦
list.addAll(list1);
myAdapter2.notifyDataSetChanged();
scrollView.onRefreshComplete();
break;
}
// 去載入輪播圖
getDataAd();
}
@Override
public void loadError(String error) {
Log.d(TAG, "loadError: " + error);
}
});
}
// 載入輪播圖
private void getDataAd() {
Log.d(TAG, "開始請求--------: ");
HttpUtils httpUtils = HttpUtils.getHttpUtils();
httpUtils.get(HttpConfig.adurl);
httpUtils.setOnHttpLoadListener(new HttpUtils.HttpLoadListener() {
@Override
public void loadSuccess(String json) {
Log.d(TAG, "loadSuccess:----- " + json);
Gson gson = new Gson();
AdBean adBean = gson.fromJson(json, AdBean.class);
List<AdBean.DataBean> beanList = adBean.getData();
List<ImageView> imageViews = new ArrayList<>();
for (int i = 0; i < beanList.size(); i++) {
String url = beanList.get(i).getImg();
ImageView imageView = new ImageView(MyApp.getContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ImageLoader.getInstance().displayImage(url, imageView, MyApp.getOptions());
imageViews.add(imageView);
}
AdAdapter adAdapter = new AdAdapter(imageViews);
viewPager.setAdapter(adAdapter);
// 開始進行自動輪播
myHandler.sendEmptyMessageDelayed(0, 1000);
}
@Override
public void loadError(String error) {
Log.d(TAG, "loadError: " + error);
}
});
}
// handler
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "訊息------");
int currentItem = viewPager.getCurrentItem();
currentItem++;
viewPager.setCurrentItem(currentItem);
myHandler.sendEmptyMessageDelayed(0, 1000);
}
}
// 頁面銷燬的時候,停止handler傳送訊息
@Override
public void onPause() {
super.onPause();
myHandler.removeCallbacksAndMessages(null);
}
}