RecyclerView做的瀑布流

MainActivty

public class MainActivity extends AppCompatActivity {
    private List<Bean.ResultsBean> list=new ArrayList<>();
        private RecyclerView recyclerview;
        private Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0){
                    String obj = (String) msg.obj;
                    Gson gson = new Gson();
                    Bean bean = gson.fromJson(obj, Bean.class);
                    List<Bean.ResultsBean> list = bean.getResults();
                    MyAdapter adapter = new MyAdapter(MainActivity.this,list);
                    recyclerview.setAdapter(adapter);
                }
            }
        };
    private MyAdapter adapter;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerview = (RecyclerView) findViewById(R.id.recyclerView);
            //设置布局管理器
            final StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerview.setLayoutManager(manager);
            //设置适配器
        adapter = new MyAdapter(MainActivity.this,list);
            recyclerview.setAdapter(adapter);
        OkHHttpClientUtils.doget("http://gank.io/api/data/福利/10/1", 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 = new Message();
                    message.what = 0;
                    message.obj = json;
                    handler.sendMessage(message);
                }
            });

    }


}
 
主MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhangyongbo.myrecycleview.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </android.support.v7.widget.RecyclerView>
</LinearLayout>
适配器文件

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    private  Context context;  //上下文
    private List<Bean.ResultsBean> list;//创建集合
        //有参构造
    public MyAdapter(Context context, List<Bean.ResultsBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //无法自适应 获取屏幕的宽度
        View view = View.inflate(context, R.layout.itme, null);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);


        //参考父布局
        view = LayoutInflater.from(context).inflate(R.layout.itme, parent, false);
        final MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        //加载布局文件
        ViewGroup.LayoutParams params = holder.iv_item.getLayoutParams();
        //给图片设置高
        if(position == 0){
            params.height = 150;
        }else{
            params.height = 300;
        }
        holder.iv_item.setLayoutParams(params);//给图片设置布局  setLayoutParams线性布局
        Picasso.with(holder.iv_item.getContext()).load(list.get(position).getUrl()).into(holder.iv_item);
        holder.iv_item.setImageResource(R.mipmap.ic_launcher);
        holder.tv_title.setText(list.get(position).getType());
        //用于点击条目
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,Gouche.class);
                context.startActivity(intent);
            }
        });
    }
    //返回list集合的数据长度
    @Override
    public int getItemCount() {
        return list.size();
    }
    //优化适配器数据
    public class  MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv_title;
        ImageView iv_item;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            iv_item = (ImageView) itemView.findViewById(R.id.iv_item);
        }
    }

}
子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_item"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
依赖文件

compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.7.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
compile 'com.squareup.picasso:picasso:2.5.2'
这里事网络请求的用到的事OKHttp

需要封装好的okhttp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值