Android RecycleView 以编程方式添加或启用滚动条

Android RecycleView Add or Enable ScrollBar Programmatically

我在不使用 XML、
的情况下以编程方式设计 RecycleView 因此,我只需复制 Java 文件即可在所有项目中重用此代码。 帮我启用滚动条。
还有如何在不使用任何第三方的情况下像联系人应用程序一样获得字母滚动条 API.

RecyclerView recyclerVyuVar = new RecyclerView(this);
recyclerVyuVar.setVerticalScrollBarEnabled(true);

recyclerVyuVar.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
recyclerVyuVar.setVerticalFadingEdgeEnabled(true);

要以编程方式启用 ScrollBar,您需要声明一个样式:

<style name="ScrollbarRecyclerView" parent="android:Widget">
    <item name="android:scrollbars">vertical</item>
</style>

并使用 ContextThemeWrapper 初始化您的 RecyclerView:

RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(this, R.style.ScrollbarRecyclerView

经过整整两天的努力,我像在联系人中一样以编程方式开发了字母滚动条。

你可以根据你的要求修改代码并适合。
NamSrnVcc 可以是您正在使用的 Activity 或屏幕的任何名称。

如果你需要放置 List View 或 Recycler View 将它放在 Super Relative 布局中 NamSupRloVav

NamLysLyoVav = new RecyclerView(new ContextThemeWrapper(this, R.style.SolBarVid));

NamLysAdrVar = new NamLysAdrCls(GetAllNamAryLysFnc());
NamLysLyoVav.setAdapter(NamLysAdrVar);
NamLysLyoVav.setLayoutManager(new LinearLayoutManager(this));

NamSupRloVav.addView(NamLysLyoVav);

这是为了帮助所有我不想像我一样挣扎的人。

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

public class NamSrnVcc extends Activity
{
    RelativeLayout NamSupRloVav;
    ImageView ZumTxtImjVav;
    String AlpAryVar[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    float DpsVal;
    int GetDpsValFnc(int IntPsgVal)
    {
        return (int) (IntPsgVal * DpsVal);
    }

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

        DpsVal = getResources().getDisplayMetrics().density;

        NamSupRloVav = (RelativeLayout) findViewById(R.id.NamSrnLyoUid);

        LinearLayout AlpSolBarLloVav = new LinearLayout(this);
        AlpSolBarLloVav.setOrientation(LinearLayout.VERTICAL);

        GradientDrawable AlpSolBarGrdDrwablVaj = new GradientDrawable();
        AlpSolBarGrdDrwablVaj.setStroke(2,Color.GRAY);
        AlpSolBarGrdDrwablVaj.setCornerRadius(20);
        AlpSolBarLloVav.setBackground(AlpSolBarGrdDrwablVaj);
        AlpSolBarLloVav.setPadding(GetDpsValFnc(10),GetDpsValFnc(5),GetDpsValFnc(10),GetDpsValFnc(5));
        AlpSolBarLloVav.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View VyuPsgVal, MotionEvent MsnEvtPsgVar)
            {
                switch (MsnEvtPsgVar.getActionMasked())
                {
                    case MotionEvent.ACTION_DOWN:
                        ZumTxtImjVav.setVisibility(View.VISIBLE);
                        TskTdoAftTchFnc(VyuPsgVal, MsnEvtPsgVar);
                        break;

                    case MotionEvent.ACTION_MOVE:
                        TskTdoAftTchFnc(VyuPsgVal, MsnEvtPsgVar);
                        break;

                    case MotionEvent.ACTION_UP:
                        Log.d("TAG", "onTouch: ACTION_UP");
                        ZumTxtImjVav.setVisibility(View.GONE);
                        break;
                }
                return true;
            }

            void TskTdoAftTchFnc(View VyuPsgVal, MotionEvent MsnEvtPsgVar)
            {
                LinearLayout AlpSolLloVav = (LinearLayout)VyuPsgVal;
                float AlpTxtHytVal = AlpSolLloVav.getChildAt(0).getHeight();
                float TchYcoVal = MsnEvtPsgVar.getY();
                int AlpIdxVal = (int) (TchYcoVal / AlpTxtHytVal);
                if(AlpIdxVal >= 0 && AlpIdxVal < AlpAryVar.length)
                    ZumTxtImjVav.setImageBitmap(DevImjBmpFrmTxtFnc(AlpAryVar[AlpIdxVal], Color.MAGENTA));
            }
        });

        for(String AlpIdxVar : AlpAryVar)
        {
            TextView AlpIdxTxtVav = new TextView(this);
            AlpIdxTxtVav.setText(AlpIdxVar);
            AlpIdxTxtVav.setTextSize(10);
            AlpIdxTxtVav.setTextColor(Color.GRAY);
            int DpsVal = GetDpsValFnc(2);
            AlpIdxTxtVav.setPadding(DpsVal,DpsVal,DpsVal,DpsVal);
            AlpIdxTxtVav.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            AlpIdxTxtVav.setTypeface(null, Typeface.BOLD);
            AlpSolBarLloVav.addView(AlpIdxTxtVav);
        }


        ZumTxtImjVav = new ImageView(this);
        GradientDrawable ZumTxtImjGrdDrwablVar = new GradientDrawable();
        ZumTxtImjGrdDrwablVar.setCornerRadius(100);
        ZumTxtImjGrdDrwablVar.setColor(Color.YELLOW);
        ZumTxtImjVav.setBackground(ZumTxtImjGrdDrwablVar);
        RelativeLayout.LayoutParams ZumTxtImjVyuRulVaj = new RelativeLayout.LayoutParams(200, 200);
        ZumTxtImjVyuRulVaj.setMarginStart(20);
        ZumTxtImjVyuRulVaj.setMarginEnd(20);
        ZumTxtImjVyuRulVaj.addRule(RelativeLayout.CENTER_IN_PARENT);
        ZumTxtImjVav.setLayoutParams(ZumTxtImjVyuRulVaj);
        NamSupRloVav.addView(ZumTxtImjVav);
        ZumTxtImjVav.setVisibility(View.GONE);

        RelativeLayout.LayoutParams AlpSolBarLyoRulVaj = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        AlpSolBarLyoRulVaj.addRule(RelativeLayout.CENTER_VERTICAL);
        AlpSolBarLyoRulVaj.addRule(RelativeLayout.ALIGN_PARENT_END);
        AlpSolBarLyoRulVaj.rightMargin = 10;
        NamSupRloVav.addView(AlpSolBarLloVav, AlpSolBarLyoRulVaj);
    }

    Bitmap DevImjBmpFrmTxtFnc(String TxtSrgPsgVal, int TxtClrPsgVal)
    {
        int TxtSyzVal = 100;
        TextPaint PenPytVaj = new TextPaint(ANTI_ALIAS_FLAG);
        PenPytVaj.setTextSize(TxtSyzVal);
        PenPytVaj.setColor(TxtClrPsgVal);
        PenPytVaj.setTextAlign(Paint.Align.LEFT);

        Rect TxtRctVar = new Rect();
        PenPytVaj.getTextBounds(TxtSrgPsgVal, 0, TxtSrgPsgVal.length(), TxtRctVar);
        Bitmap TxtImjBmpVar = Bitmap.createBitmap(TxtSyzVal * 2, TxtSyzVal * 2, Bitmap.Config.ARGB_8888);

        Canvas ImjCanvasVaj = new Canvas(TxtImjBmpVar);
        float XcoVal = ImjCanvasVaj.getWidth() / 2 - TxtRctVar.width() / 2  - TxtRctVar.left;
        float YcoVal = ImjCanvasVaj.getHeight() / 2 + TxtRctVar.height() / 2  - TxtRctVar.bottom;
        ImjCanvasVaj.drawText(TxtSrgPsgVal, XcoVal, YcoVal, PenPytVaj);
        return TxtImjBmpVar;
    }
}