[英]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
經過兩整天的奮斗,我像在聯系人中一樣以編程方式開發了字母滾動條。
U可以根據您的要求修改代碼並使其適合。
NamSrnVcc可以是您正在使用的“活動”或“屏幕”的任何名稱。
如果您需要放置列表視圖或回收者視圖,請將其放置在超級相對布局內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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.