RecyclerView实现瀑布流

 

 

效果图:

RecyclerView实现瀑布流_第1张图片

Main布局只是一个RecyclerView




    

 MainActivity代码如下:

public class MainActivity extends AppCompatActivity {
    private String path = "";
    private List videoList = new ArrayList<>();
    private RecyclerView videoRecycler;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    videoList= (List) msg.obj;
                    MyAdapter myAdapter = new MyAdapter(MainActivity.this, videoList);
                    videoRecycler.setAdapter(myAdapter);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(path).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(string);
                    String message = jsonObject.optString("message");
                    JSONObject messageobj = new JSONObject(message);
                    JSONArray anchors = messageobj.optJSONArray("anchors");
                    List list=new ArrayList<>();
                    for (int i = 0; i < anchors.length(); i++) {
                        JSONObject anchorsObj = anchors.optJSONObject(i);
                        String name = anchorsObj.optString("name");
                        String pic = anchorsObj.optString("pic51");
                        String roomid = anchorsObj.optString("roomid");
                        VideoBean.MessageEntity.AnchorsEntity anchorsEntity = new VideoBean.MessageEntity.AnchorsEntity();
                        anchorsEntity.setName(name);
                        anchorsEntity.setPic51(pic);
                        anchorsEntity.setRoomid(roomid);
                        list.add(anchorsEntity);
                    }
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = list;
                    handler.sendMessage(msg);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initView() {
        videoRecycler = (RecyclerView) findViewById(R.id.video_recycler_rv);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        videoRecycler.setLayoutManager(manager);
    }
}

item_layoout,条目布局文件



    

    

 

你可能感兴趣的:(Android)