gpt4 book ai didi

java - 在 recyclerview 的第一项上添加行分隔符

转载 作者:行者123 更新时间:2023-12-01 19:52:08 25 4
gpt4 key购买 nike

如何在第一项 (10) 的上方添加一行?

enter image description here

我正在使用 RecyclerView 和 LinearLayoutManager 来实现我的设计。

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<SeekBar
android:id="@+id/timesTablesSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

list_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="beginning">

<TextView
android:id="@+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />
</LinearLayout>

最佳答案

首先,使用 LinearLayout 的 android:showDividers 来划分项目并不是最佳实践。首先,我将从您的 list_layout.xml 中删除外部 LinearLayout,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/timeTables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textSize="70px"
android:textStyle="bold|italic" />

要显示分隔线,最好的方法是使用RecyclerView.ItemDecoration。请参阅 DividerItemDecoration 绘制分隔线: https://developer.android.com/reference/androidx/recyclerview/widget/DividerItemDecoration

dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

那么唯一缺少的就是绘制顶线。您可以像这样实现自定义 ItemDecoration:

public class TopLineDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

private final Drawable divider;

public TopLineDecoration(Context context) {
TypedArray a = context.obtainStyledAttributes(ATTRS);
divider = a.getDrawable(0);
a.recycle();
}

@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);

if (parent.getChildAdapterPosition(child) == 0) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int top = child.getTop() - params.topMargin;
int bottom = top + divider.getIntrinsicHeight();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();

divider.setBounds(left, top, right, bottom);
divider.draw(c);
}
}
}
}

你的最终实现将是这样的:

dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
topLineDecoration = new TopLineDecoration(recyclerView.getContext())
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.addItemDecoration(topLineDecoration)

关于java - 在 recyclerview 的第一项上添加行分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59077278/

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