Android: RecyclerView horizontal sliding + custom sliding bar

I found several RecyclerView articles on the Internet, and finally combined it to find the one that works. I hereby record it, the principle is not detailed

1. Install Recyciler View dependencies

    implementation 'com.makeramen:roundedimageview:2.3.0'

2. Create an entity class

public class News {
    
    
    public String title; // 标题
    public String content; //内容
}

3. Interface layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/recy_horizontal_style"

    />

</LinearLayout>

4. Each item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/recy_horizontal_style"

    />

</LinearLayout>

5. Core method

package cn.edu.cdut.huaxue;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

public class jiemian3 extends AppCompatActivity {
    
    

    RecyclerView mRecyclerView;
    MyAdapter mMyAdapter ;
    List<News> mNewsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiemian3);
        mRecyclerView = findViewById(R.id.recyclerview);

        // 构造一些数据
        for (int i = 0; i < 50; i++) {
    
    
            News news = new News();
            news.title = "标题" + i;
            news.content = "内容" + i;
            mNewsList.add(news);
        }
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(jiemian3.this,LinearLayoutManager.HORIZONTAL, false); //在此处修改水平or垂直
        mRecyclerView.setLayoutManager(layoutManager);
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
    
    

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    
    
            View view = View.inflate(jiemian3.this, R.layout.item_list, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
    
    
            News news = mNewsList.get(position);
            holder.mTitleTv.setText(news.title);
            holder.mTitleContent.setText(news.content);
        }

        @Override
        public int getItemCount() {
    
    
            return mNewsList.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
    
    
        TextView mTitleTv;
        TextView mTitleContent;

        public MyViewHoder(@NonNull View itemView) {
    
    
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.textView);
            mTitleContent = itemView.findViewById(R.id.textView2);
        }
    }
}

6. Scroll bar to beautify

    <style name="recy_horizontal_style">
        <item name="android:scrollbarSize">5dp</item>
        <item name="android:scrollbars">horizontal</item>
        <item name="android:scrollbarThumbHorizontal">@drawable/recy_vertical_scrollbar</item>
        <item name="android:scrollbarTrackHorizontal">@drawable/recy_vertical_scrollbar_bg</item>
    </style>

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq1198768105/article/details/115023726