1. 程式人生 > >RecyclerView實現瀑布流

RecyclerView實現瀑布流

效果圖:

Main佈局只是一個RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/video_recycler_rv"
        android:layout_height="match_parent"/>

</RelativeLayout>

 MainActivity程式碼如下:

public class MainActivity extends AppCompatActivity {
    private String path = "";
    private List<VideoBean.MessageEntity.AnchorsEntity> videoList = new ArrayList<>();
    private RecyclerView videoRecycler;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    videoList= (List<VideoBean.MessageEntity.AnchorsEntity>) 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<VideoBean.MessageEntity.AnchorsEntity> 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,條目佈局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/pic_iv"
        android:layout_width="match_parent"
        android:scaleType="centerCrop"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/name_tv"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="20sp" />
</LinearLayout>