BlankFragment
package com.bwie.banner_pulltorfl.BaseFragment;
import android.content.Context;
import android.os.AsyncTask;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.bwie.banner_pulltorfl.Bean;
import com.bwie.banner_pulltorfl.HttpUrlConnection;
import com.bwie.banner_pulltorfl.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by ${王新杰} on 2017/10/23.
*/
public class BlankFragment extends Fragment {
private PullToRefreshListView pullToRefreshListView;
private Banner banner;
String[] urls = {"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/22/22_12_58_629_76523_tp1.jpg",
"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/23/09_50_47_137_74167_mmexport1508723175976.jpg",
"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/22/22_12_58_629_76523_tp1.jpg",
"http://www.93.gov.cn/11002/upload/webcms/content/image/2017/10/23/09_50_47_137_74167_mmexport1508723175976.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg"};
private ListView lv;
private PullToRefreshListView plv;
private List<Bean.DataBean> list;
private ArrayList<Bean.DataBean> listA = new ArrayList<Bean.DataBean>();
private MAdapter mAdapter;
String baseUrl = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
int page = 0;
String url = baseUrl + page;
private com.nostra13.universalimageloader.core.ImageLoader instance;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.blankfragment, container, false);
View headbannerView = inflater.inflate(R.layout.headerbanner, null);
pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.plv);
banner = (Banner) headbannerView.findViewById(R.id.bannerContainer);
getBannerData();
//可以设置上下拉
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
new MAsynctask().execute(url);
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
/*
下拉刷新
*/
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
listA.clear();
new MAsynctask().execute(url);
}
/*
上拉加载
*/
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
getNetData();
}
});
instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
mAdapter = new MAdapter();
pullToRefreshListView.setAdapter(mAdapter);
lv = pullToRefreshListView.getRefreshableView();
lv.addHeaderView(headbannerView);
return view;
}
private void getNetData() {
new MAsynctask().execute(baseUrl + page);
}
private void getBannerData() {
//图片加载器
banner.setImageLoader(new BannerImageLoader());
// 设置图片集合
banner.setImages(Arrays.asList(urls));
//启动轮播
banner.start();
}
class MAdapter extends BaseAdapter {
@Override
public int getCount() {
return listA.size();
}
@Override
public Object getItem(int i) {
return listA.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(getActivity(), R.layout.list_item, null);
holder.tv1 = (TextView) view.findViewById(R.id.tv1);
holder.tv2 = (TextView) view.findViewById(R.id.tv2);
holder.img = (ImageView) view.findViewById(R.id.img);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tv1.setText(listA.get(i).getTITLE());
holder.tv2.setText(listA.get(i).getSHOWTIME());
//找不到图片路径
if (listA.get(i).getIMAGEURL() != null) {
instance.displayImage((String) listA.get(i).getIMAGEURL(), holder.img);
} else {
holder.img.setImageResource(R.drawable.a);
}
return view;
}
}
class ViewHolder {
TextView tv1;
TextView tv2;
ImageView img;
}
//加载Json串的异步处理
class MAsynctask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
// Log.e("TAG", strings[0] + "王新杰");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return HttpUrlConnection.getJson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Log.e("TAG", s + "王新杰");
Bean bean = gson.fromJson(s, Bean.class);
list = bean.getData();
listA.addAll(list);
mAdapter.notifyDataSetChanged();
pullToRefreshListView.onRefreshComplete();
}
}
public class BannerImageLoader extends ImageLoader {
/**
* @param context
* @param obj 这个传过来的的是图片接口
* @param imageView
*/
@Override
public void displayImage(Context context, Object obj, ImageView imageView) {
com.nostra13.universalimageloader.core.ImageLoader imageLoaderInstance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoaderInstance.displayImage((String) obj, imageView);
}
}
}
Fragment01
package com.bwie.banner_pulltorfl.BaseFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.banner_pulltorfl.R;
import java.util.ArrayList;
/**
* Created by ${王新杰} on 2017/10/23.
*/
public class Fragment01 extends Fragment {
private View view;
private TabLayout tab;
private ViewPager vp;
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
ArrayList<String> titleList = new ArrayList<String>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment01, container, false);
tab = (TabLayout) view.findViewById(R.id.tab);
vp = (ViewPager) view.findViewById(R.id.vp);
//设置TabLayout的模式
tab.setTabMode(TabLayout.MODE_FIXED);
//让tablayout和Viewpager关联;
tab.setupWithViewPager(vp);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MPagerAdapter mPagerAdapter = new MPagerAdapter(getActivity().getSupportFragmentManager());
initData();
vp.setAdapter(mPagerAdapter);
}
private void initData() {
titleList.add("推荐");
titleList.add("课程");
titleList.add("实战");
titleList.add("职业路径");
for (int i = 0; i < 4; i++) {
fragmentList.add(new BlankFragment());
}
}
class MPagerAdapter extends FragmentPagerAdapter {
public MPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
//需要重写个返回标题的方法;
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
}
Fragment02
package com.bwie.banner_pulltorfl.BaseFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.banner_pulltorfl.R;
/**
* Created by ${王新杰} on 2017/10/23.
*/
public class Fragment02 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment02, container, false);
return view;
}
}
Fragment03
package com.bwie.banner_pulltorfl.BaseFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.banner_pulltorfl.R;
/**
* Created by ${王新杰} on 2017/10/23.
*/
public class Fragment03 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment03, container, false);
return view;
}
}
Fragment04
package com.bwie.banner_pulltorfl.BaseFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bwie.banner_pulltorfl.R;
/**
* Created by ${王新杰} on 2017/10/23.
*/
public class Fragment04 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment04, container, false);
return view;
}
}