1. 程式人生 > >RecyclerView三種佈局管理器

RecyclerView三種佈局管理器

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 android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Bean.ResultBean.DataBean> mlist = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView mRecyclerView = (RecyclerView)findViewById(R.id.recyview);
        //瀑布流管理器
        //StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,1);
        //網格
        //GridLayoutManager manager = new GridLayoutManager(this,5);
        //線性
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(manager);
        new HttpHelper().get("http://v.juhe.cn/toutiao/index?type=&key=83a69c67e9272f816e42450ef0eb50ee").result(new HttpHelper.Httplistenner() {
            @Override
            public void success(String data) {

                Gson gson = new Gson();
                Bean bean = gson.fromJson(data, Bean.class);
                Bean.ResultBean result = bean.getResult();
                List<Bean.ResultBean.DataBean> data1 = result.getData();
                mlist.clear();
                mlist.addAll(data1);
                //Toast.makeText(MainActivity.this,"author_name"+author_name,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void error(String error) {

            }
        });
        MyAdapter myAdapter = new MyAdapter(mlist,this);
        mRecyclerView.setAdapter(myAdapter);
    }
}