当前位置:   article > 正文

Android进阶之路 - RecyclerView实现横、纵向滑动列表_recyclerview可以滑动的横向布局

recyclerview可以滑动的横向布局

说来惭愧,在这么久的开发中一直很少使用到横向的列表效果,即使有类似场景也是使用了 ViewPager处理,特此在有时间的基础下给新人写个入门的dome玩玩 ~

Effect ~
在这里插入图片描述

众所周知,RecyclerView的使用中我们会设置布局管理者,所以此篇的关键,在于LinearLayoutManager 的设置方向,关键代码如下 ~

 LinearLayoutManager layoutManager= new LinearLayoutManager(this);
 //这行是关键,设置了布局方向,如未设置HORIZONTAL方向,则默认是垂直状态
 layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
 mRv.setLayoutManager(hLayoutManager);
  • 1
  • 2
  • 3
  • 4

越来越懒了,这里我用的适配器是在目前项目中使用的 BaseQuickAdapter ~

build(project)

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

build(app)

  implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.35'
  implementation 'com.android.support:recyclerview-v7:27.1.1'
  • 1
  • 2

MainActivity

package nkwl.com.crosswisedemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView mRvH = findViewById(R.id.rv_horizontal);
        RecyclerView mRvV = findViewById(R.id.rv_vertical);

        //模拟数据
        List dataList = new ArrayList<Integer>();
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);
        dataList.add(R.mipmap.scenery);

        //横向
        MyAdapter horizontalAdapter = new MyAdapter(R.layout.item_layout, dataList);
        LinearLayoutManager hLayoutManager = new LinearLayoutManager(this);
        hLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvH.setLayoutManager(hLayoutManager);
        mRvH.setAdapter(horizontalAdapter);
        horizontalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "横向 - 当前角标:" + position, Toast.LENGTH_SHORT).show();
            }
        });

        //竖向
        MyAdapter verticalAdapter = new MyAdapter(R.layout.item_layout, dataList);
        LinearLayoutManager vLayoutManager = new LinearLayoutManager(this);
        vLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvV.setLayoutManager(vLayoutManager);
        mRvV.setAdapter(verticalAdapter);
        verticalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "竖向 - 当前角标:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

activity_main

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:background="#225"
        android:layout_width="match_parent"
        android:id="@+id/rv_horizontal"
        android:layout_height="wrap_content"/>

    <android.support.v7.widget.RecyclerView
        android:background="#255"
        android:layout_width="match_parent"
        android:id="@+id/rv_vertical"
        android:layout_height="match_parent"/>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

MyAdapter

package nkwl.com.crosswisedemo;

import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author MrLiu
 * @date 2019/11/26
 * desc
 */
public class MyAdapter extends BaseQuickAdapter<Integer,BaseViewHolder> {

    public MyAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Integer item) {
        helper.setImageResource(R.id.item_img,item);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

item_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_margin="5dp"
        android:scaleType="fitXY"
        android:id="@+id/item_img"
        android:layout_width="130dp"
        android:src="@mipmap/scenery"
        android:layout_height="130dp" />

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/174643?site
推荐阅读
相关标签
  

闽ICP备14008679号