RecyclerView三种展示(列表,表格,瀑布)OkHttp网络请求数据

该博客详细介绍了如何使用RecyclerView结合OkHttp进行网络数据请求,并展示列表、表格和瀑布流布局。同时,还涵盖了如何在应用中实现添加和删除功能,以及添加必要的依赖和权限设置。
摘要由CSDN通过智能技术生成

例:

OkHttp网络请求数据

RecyclerView展示数据

三种展示(列表,表格,瀑布)

实现 添加,删除 功能




添加依赖:

dependencies {
 
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.2'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}

添加权限:

<uses-permission android:name="android.permission.INTERNET"/>


MainActivity主要类:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final int SUCCESS = 492;
    private MyAdapter myAdapter;
    private List<LogoBean.DataBean.PcFeedFocusBean> list;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUCCESS:
                    String json = (String) msg.obj;
                    Gson gson = new Gson();
                    LogoBean logoBean = gson.fromJson(json, LogoBean.class);
                    list = logoBean.getData().getPc_feed_focus();
                    myAdapter = new MyAdapter(list, MainActivity.this);
                    rv.setAdapter(myAdapter);
            }
        }
    };

    private RecyclerView rv;

    private Button ll;
    private Button bg;
    private Button pb;
    private Button add;
    private Button select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        initadd();
        rv = findViewById(R.id.rv);//RecyclerView
    }

    private void initadd() {
        ll = findViewById(R.id.ll);//线性布局
        bg = findViewById(R.id.bg);//表格布局
        pb = findViewById(R.id.pb);//瀑布流布局
        add = findViewById(R.id.add);//添加方法
        select = findViewById(R.id.select);//删除方法

        //按钮的监听
        ll.setOnClickListener(this);
        bg.setOnClickListener(this);
        pb.setOnClickListener(this);
        add.setOnClickListener(this);
        select.setOnClickListener(this);
    }

    //按钮的监听
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll:
                getpostll();
                //线性布局管理器
                rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
                break;
            case R.id.bg:
                getpostll();
                //表格布局管理器
                rv.setLayoutManager(new GridLayoutManager(this,3));
                break;
            case R.id.pb:
                getpostll();
                //瀑布式布局管理器
                rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值