【问题标题】:Android RecycleView Add or Enable ScrollBar ProgrammaticallyAndroid RecycleView 以编程方式添加或启用滚动条
【发布时间】:2017-10-18 17:53:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-recyclerview scrollbar vertical-scroll


    【解决方案1】:

    要以编程方式启用 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
    

    【讨论】:

    • 我们能否以编程方式执行 R.style.ScrollbarRecyclerView,因为我只想在所有项目中的任何地方复制单个 Java 代码,而不需要 xml 中的任何内容。
    【解决方案2】:

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

    您可以根据您的要求修改代码并适应。
    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多