我在不使用XML的情况下以编程方式设计RecycleView,
因此,我可以通过处理Java文件在所有项目中重用此代码。
帮我启用滚动条。
另外,如何在不使用任何第三方API的情况下获取联系人应用程序中的字母滚动条。
RecyclerView recyclerVyuVar = new RecyclerView(this);
recyclerVyuVar.setVerticalScrollBarEnabled(true);
recyclerVyuVar.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
recyclerVyuVar.setVerticalFadingEdgeEnabled(true);
答案 0 :(得分:0)
要以编程方式启用ScrollBar,您需要声明样式:
<style name="ScrollbarRecyclerView" parent="android:Widget">
<item name="android:scrollbars">vertical</item>
</style>
初始化您的RecyclerView使用ContextThemeWrapper:
RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(this, R.style.ScrollbarRecyclerView
答案 1 :(得分:0)
经过两天的奋斗,我开发了一些字母滚动条,编程方式与联系人一样。
您可以根据您的要求修改代码并进行调整 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;
}
}