Android----------RecyclerView数据展示

1.首先导入依赖:

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

2.适配器类:

public class NewsAdapter extends RecyclerView.Adapter {
    private View view;
    private Context mcontext;
    private ArrayList<NewsData.DataBean> mlist=new ArrayList<>();

    public NewsAdapter(Context mcontext) {
        this.mcontext = mcontext;
    }

    //找到需要导入的布局
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mcontext).inflate(R.layout.news_item, null);
        return new ViewHolder(view);

    }
    //加载相应的内容
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      ViewHolder viewholder=(ViewHolder)holder;
        viewholder.textview.setText(""+getItem(position).getNews_title());
        Glide.with(mcontext).load(getItem(position).getPic_url()).into(viewholder.ima);
    }

    //手写的重置数据的方法
    public void setdata(ArrayList<NewsData.DataBean> list){
        if (list!=null&&list.size()>0){
           mlist.addAll(list);
        }

    }
    //手写的获得视图的方法
    public NewsData.DataBean getItem(int position){
        return mlist.get(position);

    };

    @Override
    public int getItemCount() {
        return mlist.size();
    }


    //定义的ViewHolder类继承RecyclerView.ViewHolder 并找到布局中的ID
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView textview;
        private ImageView ima;
        public ViewHolder(View itemView) {
            super(itemView);
           textview=(TextView)itemView.findViewById(R.id.tv);
         ima=(ImageView)itemView.findViewById(R.id.image);
        }
    }
}

3.主界面(用到RecyclerView的主要是initdata()方法和Handler):

public class MainActivity extends AppCompatActivity implements IMainView{
    private MainPresenter mainp;
    private NewsAdapter md;
    private RecyclerView recy;
    private  ArrayList<NewsData.DataBean> mlist=new ArrayList<>();
    private String url="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    private Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Gson gson=new Gson();
            if (msg.what==1) {
                NewsData newsData = gson.fromJson(msg.obj.toString(), NewsData.class);
               mlist=(ArrayList<NewsData.DataBean>) newsData.getData();

               md.setdata((ArrayList<NewsData.DataBean>) mlist);
                md.notifyDataSetChanged();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initdata();
        initdatafromserver();
    }

    private void initview() {
        recy=(RecyclerView)findViewById(R.id.recy);
    }

    private void initdatafromserver() {
        //加载网络数据,找P层去要
      mainp.loadDataFromServer(url);

    }

    private void initdata() {
      mainp=new MainPresenter(this);
        //设置适配器
      md=new NewsAdapter(this);
        //创建默认的线性LayoutManager
        recy.setLayoutManager(new LinearLayoutManager(this));
      recy.setAdapter(md);
    }

    //重写的IMainView类接口的两个方法
    @Override
    public void successfulCallback(String str) {
        Message msg = Message.obtain();
        msg.what=1;
        msg.obj=str;
        hand.sendMessage(msg);
    }

    @Override
    public void errCallback(int code, String msg) {

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值