MVP v层

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();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值