Recyclerviews的使用(瀑布流)

1.导入依赖
compile ‘com.android.support:recyclerview-v7:23.2.0’

2.给RecycleView创建适配器通过

package com.bwei.recyclerviews.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwei.recyclerviews.R;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 1. 类的用途
 * 2. @author forever
 * 3. @date 2017/9/6 16:06
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    private JSONArray data;

    public MyAdapter(Context context, JSONArray data) {
        this.context = context;
        this.data = data;
    }

    //创建view设置给ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    //绑定数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        try {
            JSONObject jsonObject = data.getJSONObject(position);
            String image_url = jsonObject.optString("image_url");
            String title = jsonObject.getString("title");
            holder.tv_title.setText(title);
            ViewGroup.LayoutParams layoutParams = holder.iv.getLayoutParams();
            if (position == 0) {
                layoutParams.height = 200;

            }
            holder.iv.setLayoutParams(layoutParams);
            Picasso.with(context).load(image_url).placeholder(R.mipmap.ic_launcher).into(holder.iv);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //数据长度
    @Override
    public int getItemCount() {
        return data.length();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv;
        private TextView tv_title;


        public MyViewHolder(View itemView) {
            super(itemView);

            iv = (ImageView) itemView.findViewById(R.id.iv);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);

        }
    }
}

3.通过网络请求Json串进行解析添加适配器(实现三种样式)

package com.bwei.recyclerviews.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.bwei.recyclerviews.R;
import com.bwei.recyclerviews.adapter.MyAdapter;
import com.bwei.recyclerviews.utils.OkHttp3Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv;
    private String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=one";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String json = (String) msg.obj;
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        JSONArray data = jsonObject.getJSONArray("data");
                        //设置适配器
                        MyAdapter adapter = new MyAdapter(MainActivity.this, data);
                        rv.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        rv = (RecyclerView) findViewById(R.id.rv);
        //获取数据
        getData();



/**
     * 通过这三种方法进行三种不同的样式的显示
     */
       //设置LinearLayoutManager布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置GridLayoutManager布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

        //设置StaggeredGridLayoutManager布局管理器
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

        //把布局管理器给RecyclerView
        rv.setLayoutManager(staggeredGridLayoutManager);


    }

    private void getData() {
        OkHttp3Utils.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Message message = handler.obtainMessage(0, json);
                message.sendToTarget();
            }
        });

    }
}

分装好包调包给RecycleView添加分割线

 //添加华丽分割线
        MyDecoration decoration = new MyDecoration(this, LinearLayoutManager.VERTICAL);
        rv.addItemDecoration(decoration);
        //给表格添加华丽的分割线
       /* GridDivider gridDivider = new GridDivider(this, 2, Color.BLUE);
        rv.setLayoutManager(gridLayoutManager);
        rv.addItemDecoration(gridDivider);*/
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值