gpt4 book ai didi

android - 如何在水平 RecyclerView 下添加圆点?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:55:41 36 4
gpt4 key购买 nike

我有 RecyclerView 可以显示我想要的图像

    RecyclerView detailsRecycleImage = (RecyclerView) view.findViewById(R.id.detailsRcycleImage);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(
getActivity().getApplicationContext(),
LinearLayoutManager.HORIZONTAL,
false
);

detailsRecycleImage.setLayoutManager(mLayoutManager);
ImagesAdapter imgAdapter = new
ImagesAdapter(getActivity(),contactsData.getContactImages());
detailsRecycleImage.setAdapter(imgAdapter);

我的适配器代码是

     public class ImagesAdapter extends  RecyclerView.Adapter<ImagesAdapter.MyViewHolder>{
private Context cnt;
public ArrayList<String> imgsUrls;
public ImagesAdapter(Context cnt, ArrayList<String> imgsUrls) {
this.cnt=cnt;
this.imgsUrls=imgsUrls;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_img,parent,false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
String singleImg = imgsUrls.get(position);
Picasso.with(cnt).load(singleImg).into(holder.img);
}

@Override
public int getItemCount() {
return imgsUrls.size();
}


public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView img;

public MyViewHolder(View view) {
super(view);
img = (ImageView) view.findViewById(R.id.img);
}
}
}

我的布局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/img"
/>
</RelativeLay

我只需要在 recyclerview 下添加点作为此图像的指示器 enter image description here enter image description here
有什么方法可以向我的代码中添加一些内容并获得此 View 吗?

最佳答案

我稍微更改了 LinePagerIndicatorDecoration 使其成为 DotsIndicatorDecoration 并同时支持 GridLayoutManager 和 LinearLayoutManager

public class DotsIndicatorDecoration extends RecyclerView.ItemDecoration {

private final int indicatorHeight;
private final int indicatorItemPadding;
private final int radius;

private final Paint inactivePaint = new Paint();
private final Paint activePaint = new Paint();

public DotsIndicatorDecoration(int radius, int padding, int indicatorHeight, @ColorInt int colorInactive, @ColorInt int colorActive) {
float strokeWidth = Resources.getSystem().getDisplayMetrics().density * 1;
this.radius = radius;
inactivePaint.setStrokeCap(Paint.Cap.ROUND);
inactivePaint.setStrokeWidth(strokeWidth);
inactivePaint.setStyle(Paint.Style.STROKE);
inactivePaint.setAntiAlias(true);
inactivePaint.setColor(colorInactive);

activePaint.setStrokeCap(Paint.Cap.ROUND);
activePaint.setStrokeWidth(strokeWidth);
activePaint.setStyle(Paint.Style.FILL);
activePaint.setAntiAlias(true);
activePaint.setColor(colorActive);

this.indicatorItemPadding = padding;
this.indicatorHeight = indicatorHeight;
}

@Override
public void onDrawOver(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);

final RecyclerView.Adapter adapter = parent.getAdapter();

if (adapter == null) {
return;
}

int itemCount = adapter.getItemCount();

// center horizontally, calculate width and subtract half from center
float totalLength = this.radius * 2 * itemCount;
float paddingBetweenItems = Math.max(0, itemCount - 1) * indicatorItemPadding;
float indicatorTotalWidth = totalLength + paddingBetweenItems;
float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2f;

// center vertically in the allotted space
float indicatorPosY = parent.getHeight() - indicatorHeight / 2f;

drawInactiveDots(c, indicatorStartX, indicatorPosY, itemCount);

final int activePosition;

if (parent.getLayoutManager() instanceof GridLayoutManager) {
activePosition = ((GridLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
} else if (parent.getLayoutManager() instanceof LinearLayoutManager) {
activePosition = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
} else {
// not supported layout manager
return;
}

if (activePosition == RecyclerView.NO_POSITION) {
return;
}

// find offset of active page if the user is scrolling
final View activeChild = parent.getLayoutManager().findViewByPosition(activePosition);
if (activeChild == null) {
return;
}

drawActiveDot(c, indicatorStartX, indicatorPosY, activePosition);
}

private void drawInactiveDots(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
// width of item indicator including padding
final float itemWidth = this.radius * 2 + indicatorItemPadding;

float start = indicatorStartX + radius;
for (int i = 0; i < itemCount; i++) {
c.drawCircle(start, indicatorPosY, radius, inactivePaint);
start += itemWidth;
}
}

private void drawActiveDot(Canvas c, float indicatorStartX, float indicatorPosY,
int highlightPosition) {
// width of item indicator including padding
final float itemWidth = this.radius * 2 + indicatorItemPadding;
float highlightStart = indicatorStartX + radius + itemWidth * highlightPosition;
c.drawCircle(highlightStart, indicatorPosY, radius, activePaint);
}

@Override
public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.bottom = indicatorHeight;
}
}

用法:

        RecyclerView recyclerView = resourceLayout.getSuccessView().findViewById(R.id.cardsRecyclerView);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(manageCardGenericAdapter);
final int radius = getResources().getDimensionPixelSize(R.dimen.radius);
final int dotsHeight = getResources().getDimensionPixelSize(R.dimen.dots_height);
final int color = ContextCompat.getColor(getContext(), R.color.primaryBlue);
recyclerView.addItemDecoration(new DotsIndicatorDecoration(radius, radius * 4, dotsHeight, color, color));
new PagerSnapHelper().attachToRecyclerView(recyclerView);

关于android - 如何在水平 RecyclerView 下添加圆点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43656253/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com