1,封装bean接口 《列表uiConnBean》《轮播的图片uiNewsBen》
2.
public class Fragment6 extends Fragment implements HttpUtils.NetCall { //列表 private String url = "http://www.xieast.com/api/news/news.php?page="; private int pyte = 1; private PullToRefreshListView pull_listview; // 数据库相关 private NewsDao newsDao; // 网络请求相关 private HttpUtils httpUtils; private List<DataBean> dataBeanslist; private NewsListAdapter newsListAdapter; //轮播 private String connurl = "http://www.xieast.com/api/banner.php"; private ViewPager lb_view_page; private LinearLayout lb_poin; private List<ImageView> lunboimg; private List<ImageView> lbyuandian; private LunboCallBack lunbo; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment6, container, false); //初始化PullToRefreshListView pull_listview = view.findViewById(R.id.pull_listview); //获取PullToRefreshListView模型 pull_listview.setMode(PullToRefreshBase.Mode.BOTH); //实现上拉刷新下拉加载 pull_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { //上拉加载 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { pyte = 1; loadData(); } //下拉刷新 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { pyte++; loadData(); } }); //生成集合 存放databean dataBeanslist = new ArrayList<>(); newsListAdapter = new NewsListAdapter(getActivity(), dataBeanslist); pull_listview.setAdapter(newsListAdapter); // 请求数据并在ListView中展示 // 先从数据库中读取, 数据库中有直接展示 没有判断网络是否连接 连接请求网络数据 没有连接提示提示用户没有网络 newsDao = new NewsDao(getActivity()); httpUtils = HttpUtils.getInstance(); //列表 loadData(); //轮播 lunBo(); return view; } //展示轮播的类 private void lunBo() { //给PullToRefresh添加头布局 ListView refreshableView = pull_listview.getRefreshableView(); View lunbo_view = LayoutInflater.from(getActivity()).inflate(R.layout.lunbo_lunbo, null, false); //查找轮播图 lb_view_page = lunbo_view.findViewById(R.id.lb_view_page); lb_poin = lunbo_view.findViewById(R.id.lb_poin); //定义一个集合 lunboimg = new ArrayList<ImageView>(); lbyuandian = new ArrayList<ImageView>(); // 设置头布局的布局参数//宽高 AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 400); lunbo_view.setLayoutParams(params); //添加头布局 refreshableView.addHeaderView(lunbo_view); //请求轮播 lunbo = new LunboCallBack(); httpUtils.getDataFrom(connurl, lunbo); } //定义一个类实现一个网络接口--轮播 class LunboCallBack implements HttpUtils.NetCall { @Override public void onsuccess(String result) { Gson gson = new Gson(); UiConnBean uiConnBean = gson.fromJson(result, UiConnBean.class); for (UiConnBean.DataBean dataBean : uiConnBean.getData()) { // 添加图片 ImageView imageView = new ImageView(getActivity()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(dataBean.getImg(), imageView); lunboimg.add(imageView); // 添加小圆点 ImageView point = new ImageView(getActivity()); point.setImageResource(R.drawable.bin_lu); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40); params.leftMargin = 10; lb_poin.addView(point, params); lbyuandian.add(point); } // 设置Adapter lubopageAdapter lubopageAdapter = new lubopageAdapter(lunboimg); lb_view_page.setAdapter(lubopageAdapter); changePonintState(0); mHandler.sendEmptyMessageDelayed(0, 1000); } @Override public void onError(String errorMsg) { } } //循环小圆点 private void changePonintState(int i) { for (int j = 0; j < lbyuandian.size(); j++) { if (i == j) { lbyuandian.get(j).setImageResource(R.drawable.bin_lu); } else { lbyuandian.get(j).setImageResource(R.drawable.bin_hong); } } } // 轮播 private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: int currentItem = lb_view_page.getCurrentItem(); currentItem++; lb_view_page.setCurrentItem(currentItem); changePonintState(currentItem % lbyuandian.size()); sendEmptyMessageDelayed(0, 1000); break; } } }; @Override public void onDestroy() { super.onDestroy(); mHandler.removeCallbacksAndMessages(null); } //展示列表的类 进行判断 private void loadData() { //dao包的展示 NewsBean show = newsDao.selectnews(url + pyte); if (show != null) { showDate(show.json); } else { if (NewsUtils.isNetworkConnected(getActivity())) { httpUtils = HttpUtils.getInstance(); httpUtils.getDataFrom(url + pyte, this); } else { Toast.makeText(getActivity(), "请连接网络!", Toast.LENGTH_SHORT).show(); } } } //请求网络接口数据的成功 @Override public void onsuccess(String result) { Log.i("cccc", result); String json = result; // 存储到数据库 NewsBean bean = new NewsBean(url + pyte, json); newsDao.addnews(bean); //展示 showDate(json); } private void showDate(String json) { Gson gson = new Gson(); UiNewsBean json1 = gson.fromJson(json, UiNewsBean.class); /* 第一页数据 清空List */ if (pyte == 1) { dataBeanslist.clear(); } dataBeanslist.addAll(json1.getData()); // 使用HashSet去除重复 HashSet hashSet = new HashSet(dataBeanslist); dataBeanslist.clear(); dataBeanslist.addAll(hashSet); //刷新适配器 newsListAdapter.notifyDataSetChanged(); // 停止刷新, 如果读取本地数据库 可能时间太快 停止不了刷新,所以延时1秒钟停止刷新 pull_listview.postDelayed(new Runnable() { @Override public void run() { pull_listview.onRefreshComplete(); } }, 1000); } //请求网络接口数据的失败 @Override public void onError(String erroremg) { Log.i("vvvv", erroremg); } }
3所需要的adapter 《轮播》
public class lubopageAdapter extends PagerAdapter { private List<ImageView> imageViewList; public lubopageAdapter(List<ImageView> imageViewList) { this.imageViewList = imageViewList; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { ImageView imageView = imageViewList.get(position % imageViewList.size()); ViewGroup viewGroup = (ViewGroup) imageView.getParent(); if (viewGroup != null) { viewGroup.removeAllViews(); } container.addView(imageView); return imageView; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } }
4列表的adapter
public class NewsListAdapter extends BaseAdapter { private Context context; private List<UiNewsBean.DataBean> dataBeans; public NewsListAdapter(Context context, List<UiNewsBean.DataBean> dataBeans) { this.context = context; this.dataBeans = dataBeans; } @Override public int getCount() { return dataBeans.size(); } @Override public UiNewsBean.DataBean getItem(int position) { return dataBeans.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { int pyte=1; if (position==0){ pyte=0; return pyte; }else { pyte=1; return pyte; } } @Override public View getView(int position, View convertView, ViewGroup parent) { int pyte=getItemViewType(position); ViewHolder holder=null; if(convertView==null){ holder=new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.neirong01, null, false); holder.imageView = convertView.findViewById(R.id.n_img1); holder.textView = convertView.findViewById(R.id.n_text); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } String thumbnail_pic_s = dataBeans.get(position).getThumbnail_pic_s(); ImageLoader.getInstance().displayImage(thumbnail_pic_s, holder.imageView); holder.textView.setText(dataBeans.get(position).getTitle()); return convertView; } class ViewHolder{ ImageView imageView; TextView textView; } }