1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| /** * Created by Dell on 2017/5/10. * @author 小红妹 * 科技模块类 */ public class ScienceTechnology extends BaseFragment implements NewsJsonUtils.CallBackListener, SwipeRefreshLayout.OnRefreshListener{
private View view; private RecyclerView mRecyclerView; private ScienceTechnologyAdapter adapterB; //下拉刷新控件 private SwipeRefreshLayout sWipe; //记录最后一个下标 private int mFootItem; //或取数据的工具类 private NewsJsonUtils newsUtils; //保存了所有数据集合数组 private List<NewsBeanData>[] msgSt; //从msgSt中获取科技信息 private List<NewsBeanData> mData; //保存了所有图片播放数据集合 private List<String> imgItem; //保存了所有标题数据集合 private List<String> titleItem; //记录当前真正要显示item数据 private List<NewsBeanData> nowItem; //记录当前存放的数据条数 private int nowNum = 9; //记录每次刷新的数 private final int NUM = 5;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hp_fm_news_fragment_b, container, false); initView();//调用初始化方法 loadData();//调用加载数据方法 drawRecyclerView();//调用RecyclerView点击事件方法 initFootListener();//调用上拉加载事件监听方法 return view; }
/** * 初始化方法 */ private void initView() { imgItem = new ArrayList<>(); titleItem = new ArrayList<>(); nowItem = new ArrayList<>(); //下拉刷新 sWipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout); sWipe.setColorSchemeResources(android.R.color.holo_blue_bright); sWipe.setOnRefreshListener(this);
mRecyclerView = (RecyclerView) view.findViewById(R.id.one_b_recycler_view); adapterB = new ScienceTechnologyAdapter(getActivity(), imgItem, titleItem, nowItem); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); //如果可以确定每个item的高度是固定的,设置这个选项可以提高性能 mRecyclerView.setHasFixedSize(true); mRecyclerView.setAdapter(adapterB); }
/** * RecyclerView点击事件方法 */ private void drawRecyclerView() { //RecyclerView点击事件 adapterB.setOnItemClickListener(new ScienceTechnologyAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { String url = mData.get(position).getUrl(); Intent intent = new Intent(); intent.setClass(getActivity(), NewsDetailActivity.class); intent.putExtra("url",url); Log.e("URL", "onItemClick: "+ url); startActivity(intent); } }); }
/** * 上拉加载事件监听方法 */ private void initFootListener() { mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { //状态发生时触发 @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && mFootItem + 1 == adapterB.getItemCount()) { mHandler.postDelayed(new Runnable() { @Override public void run() { nowNum += NUM; initData(); sWipe.setRefreshing(false); } }, 1500); } }
//滚动时监听 @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager(); mFootItem = lm.findLastVisibleItemPosition(); } }); }
/** * 加载数据方法(获取网络数据) */ @Override public void loadData() { // super.loadData(); //注册本类去监听数据加载状态 newsUtils = new NewsJsonUtils(this); //加载数据 newsUtils.getUtils(); }
/** * 回调更新界面的方法 * @param msgSt */ @Override public void upData(List<OneBeanData> msgList, List<NewsBeanData>[] msgSt) { this.msgSt = msgSt; initData();//调用分配数据,填充布局的方法 }
//分配数据,填充布局的方法 public void initData(){ if (msgSt != null){ imgItem.clear();//清空缓存 titleItem.clear(); nowItem.clear(); mData = msgSt[4]; for (int i = 0; i < 3; i++) { imgItem.add(mData.get(i).getThumbnail()); titleItem.add(mData.get(i).getTitle()); } for (int i = 3; i < nowNum; i++) { nowItem.add(mData.get(i)); } } adapterB.notifyDataSetChanged(); }
/** * 下拉刷新方法 */ @Override public void onRefresh() { nowNum += NUM;//刷新操作执行后,显示增加的内容 initData();//调用分配数据,填充布局的方法 sWipe.setRefreshing(false); }
public Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); } }; }
|