android:RecyclerView(横向、纵向、瀑布流)详解

今天我要讲的是RecycleView的几个常用布局方法的使用,也是非常的简单且灵活多变。
这是RecycleView的横向滚动,下面是效果图:

在这里插入图片描述
在这里插入图片描述

适配器的代码:
在这里插入图片描述

package com.example.a51044.app2.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.bumptech.glide.Glide;
import com.example.a51044.app2.R;
import com.example.a51044.app2.bean.MyData;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MyData.DataBean> mList;
    private Context mContex;

    public MyAdapter(List<MyData.DataBean> mList, Context mContex) {
        this.mList = mList;
        this.mContex = mContex;
    }

    public void setmList(List<MyData.DataBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.ityem,viewGroup,false);
        ViewHolder holder=new ViewHolder(view);
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=holder.getAdapterPosition();
                MyData.DataBean data=mList.get(position);
                Toast.makeText(mContex,data.getTitle(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int i) {
        MyData.DataBean data=mList.get(i);
        Glide.with(mContex).load(data.getThumbnail_pic_s()).into(viewHolder.iv);
        viewHolder.tv.setText(data.getAuthor_name());
    }

    @Override
    public int getItemCount() {
        return mList.isEmpty()?0:mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv;
        TextView tv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tv=itemView.findViewById(R.id.tv);
        }
    }
}

MainActivity里的代码:

package com.example.a51044.app2;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.a51044.app2.adapter.MyAdapter;
import com.example.a51044.app2.bean.MyData;
import com.example.a51044.app2.util.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyview;
    private List<MyData.DataBean>mList=new ArrayList<>();
    private MyAdapter myAdapter;
    private String mUrl="http://www.xieast.com/api/news/news.php?page=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //得到StaggeredGrid的管理类用来实现瀑布流布局
        StaggeredGridLayoutManager layoutManager =new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        //获取LinearLayout的管理类
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        //设置方向为横向,不设置默认为纵向
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyview.setLayoutManager(layoutManager);
        myAdapter=new MyAdapter(mList,MainActivity.this);
        recyview.setAdapter(myAdapter);
        new MyTask().execute(mUrl);
    }

    private void initView() {
        recyview = (RecyclerView) findViewById(R.id.recyview);
    }

    class MyTask extends AsyncTask<String,Void,List<MyData.DataBean>>
    {

        @Override
        protected List<MyData.DataBean> doInBackground(String... strings) {
            try {
                String s = HttpUtils.get(mUrl);
                Gson gson=new Gson();
                MyData data = gson.fromJson(s, MyData.class);
                return data.getData();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(List<MyData.DataBean> dataBeans) {
            super.onPostExecute(dataBeans);
            mList.addAll(dataBeans);
            myAdapter.notifyDataSetChanged();
        }
    }





}

发布了56 篇原创文章 · 获赞 5 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览