[英]Android : Handling child recyclerview vertical scroll and click in parent recyclerview header
I need to implement a recyclerview with fixed height (250 dp) which scrolls vertically in the header of parent recyclerview which also scrolls vertically.我需要实现一个具有固定高度(250 dp)的recyclerview,它在父recyclerview 的header 中垂直滚动,它也垂直滚动。 And also header has a button in it. header 里面还有一个按钮。 I am not able to get the child recyclerview's click or scroll.我无法获得子 recyclerview 的点击或滚动。 How we can achieve both parent recyclerview scroll and child recyclerview scroll(which is in header of parent)?我们如何实现父recyclerview滚动和子recyclerview滚动(在父级的header中)?
In fact, it will be easier to answer if you add the code you applied while asking the question.事实上,如果您在提问时添加您应用的代码,会更容易回答。 Other than that, you can use a dotindicator for the recyclerview.除此之外,您可以为 recyclerview 使用 dotindicator。 This recyclerview will scroll automatically as it scrolls.此回收器视图将在滚动时自动滚动。
Example:例子:
final int radius = getResources().getDimensionPixelSize(R.dimen.radius);
final int dotsHeight = getResources().getDimensionPixelSize(R.dimen.dotsHeight);
final int color = ContextCompat.getColor(getApplicationContext(), R.color.appcolor1);
recyclerViewSlider.addItemDecoration(new DotsIndicatorDecoration(radius, radius * 4, dotsHeight, color, color));
DotsIndicatorDecoration.java点指示器装饰.java
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;
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
Log.d("olley", parent.getHeight() + "--" + indicatorHeight);
float indicatorPosY = parent.getHeight()*0.9f - 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; //evil just like cat on box
}
}
I hope you want something like this.我希望你想要这样的东西。 You can make adjustments etc. in the:) redirect.您可以在 :) 重定向中进行调整等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.