0

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

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

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

2 回答 2

0

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

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

如果您需要放置 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;
    }
}
于 2017-05-20T08:23:13.407 回答
0

要以编程方式启用 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
于 2017-05-19T06:06:15.933 回答