使用RecyclerView实现左右并排分类选择

本文展示了如何在Android项目中通过RecyclerView实现知识体系左右并排展示,包括数据接口调用、实体类解析及左右Adapter的交互设计。

大家在做项目时肯定都会使用到RecyclerView来显示列表数据,今天实现一个左右并排的效果。
在这里插入图片描述
用到的依赖库如下

 implementation 'com.squareup.retrofit2:retrofit:2.9.0'
 implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

界面中用到的Api接口来自于玩Android,同时感谢鸿洋大大的分享。
接口地址 我这里用的是体系数据相关的Api
接着是布局,xml中是一个线性布局,放置了一个TextView用来显示标题的,下面是两个RecyclerView 分别用来显示左侧内容和右侧内容。

<?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=".activity.KnowledgeActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:text="知识体系"
        android:textSize="15sp"
        android:textColor="@color/white"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_left_view"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:overScrollMode="never">
    </androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_right_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never">
    </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>

</LinearLayout>

接下来在项目中定义一个接口用来存放请求的Url

package com.ranlegeran.aroutertest.api;

import com.ranlegeran.aroutertest.bean.Knowledge;
import com.ranlegeran.aroutertest.bean.KnowledgeArticle;
import com.ranlegeran.aroutertest.bean.ProjectTree;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;

public interface ApiService {
   
   
    //体系数据
    @GET("tree/json")
    Call<Knowledge> getKnowledge();

    //知识体系下的文章
    @GET()
    Call<KnowledgeArticle> getKnowledgeArticle(@Url String url);

}

Knowledge类和KnowledgeArticle类的实体

package com.ranlegeran.aroutertest.bean;

import java.io.Serializable;
import java.util.List;

public class Knowledge {
   
   
    private int errorCode;
    private String errorMsg;
    private List<DataBean> data;

    public int getErrorCode() {
   
   
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
   
   
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
   
   
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
   
   
        this.errorMsg = errorMsg;
    }

    public List<DataBean> getData() {
   
   
        return data;
    }

    public void setData(List<DataBean> data) {
   
   
        this.data = data;
    }

    public static class DataBean {
   
   
        private int courseId;
        private int id;
        private String name;
        private int order;
        private int parentChapterId;
        private boolean userControlSetTop;
        private int visible;
        private List<ChildrenBean> children;

        public int getCourseId() {
   
   
            return courseId;
        }

        public void setCourseId(int courseId) {
   
   
            this.courseId = courseId;
        }

        public int getId() {
   
   
            return id;
        }

        public void setId(int id) {
   
   
            this.id = id;
        }

        public String getName() {
   
   
            return name;
        }

        public void setName(String name) {
   
   
            this.name = name;
        }

        public int getOrder() {
   
   
            return order;
        }

        public void setOrder(int order) {
   
   
            this.order = order;
        }

        public int getParentChapterId() {
   
   
            return parentChapterId;
        }

        public void setParentChapterId(int parentChapterId) {
   
   
            this.parentChapterId = parentChapterId;
        }

        public boolean isUserControlSetTop() {
   
   
            return userControlSetTop;
        }

        public void setUserControlSetTop(boolean userControlSetTop) {
   
   
            this.userControlSetTop = userControlSetTop;
        }

        public int getVisible() {
   
   
            return visible;
        }

        public void setVisible(int visible) {
   
   
            this.visible = visible;
        }

        public List<ChildrenBean> getChildren() {
   
   
            return children;
        }

        public void setChildren(List<ChildrenBean> children) {
   
   
            this.children = children;
        }

        public static class ChildrenBean {
   
   
            private int courseId;
            private int id;
            private String name;
            private int order;
            private int parentChapterId;
            
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值