public class MainActivity extends AppCompatActivity implements IView{ private XRecyclerView rv; private ArrayList<AllBean.NewslistBean> lists; private RecyclerViewAdapter adapter; private NewsPresenter presenter; private int page=10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (XRecyclerView) findViewById(R.id.rv); presenter = new NewsPresenter(); lists = new ArrayList<>(); //设置Item增加、移除动画 rv.setItemAnimator(new DefaultItemAnimator()); //添加分割线 /*rv.addItemDecoration(new DividerItemDecoration( this, DividerItemDecoration.HORIZONTAL));*/ adapter = new RecyclerViewAdapter(this,lists); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); rv.setLayoutManager(linearLayoutManager); rv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { page=10; lists.clear(); getData(page); rv.refreshComplete(); } @Override public void onLoadMore() { page++; getData(page); rv.refreshComplete(); } }); adapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onClick(View view, int position) { Intent intent = new Intent(MainActivity.this,WebViewActivity.class); intent.putExtra("url",lists.get(position).getUrl()); startActivity(intent); } }); adapter.OnItemLongClickListener(new OnItemLongClickListener() { @Override public void setLongClick(View view, int position) { lists.remove(position); adapter.notifyItemRemoved(position); } }); getData(page); rv.setAdapter(adapter); presenter.attachView(this); } private void getData(int page) { presenter.getNews(page); } @Override public void success(String tag, List<AllBean.NewslistBean> news) { if (null != news) { lists.addAll(news); adapter.notifyDataSetChanged(); } } @Override public void failed(String tag, Exception error) { Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); if (presenter != null) { presenter.detachView(); } } }
MVP v层
最新推荐文章于 2022-08-09 18:18:24 发布