<androidx.recyclerview.widget.RecyclerView android:id="@+id/city_recycler" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/city_line" android:divider="@null"></androidx.recyclerview.widget.RecyclerView>
时间: 2024-03-15 20:42:50 浏览: 146
这是一个 Android 布局文件中的代码片段,其中定义了一个 RecyclerView 控件,它的 ID 是 city_recycler,宽度和高度都设置为 match_parent,表示填满父容器。android:layout_below="@id/city_line" 表示该控件在布局中的位置应该在 ID 为 city_line 的控件下方。android:divider="@null" 表示该 RecyclerView 不应该显示分割线。RecyclerView 是一个高度可定制化的控件,它可以用来展示列表数据,并且支持各种不同的布局方式和交互方式。
相关问题
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:src="@drawable/add" android:contentDescription="@string/add_button_description"/> </LinearLayout> </ScrollView> 报错Cannot resolve class RecyclerView
这个错误通常是因为没有在项目的 build.gradle 文件中添加 RecyclerView 的依赖。你可以尝试将以下代码添加到你的 build.gradle 文件中:
```
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
```
如果你已经添加了这个依赖但是仍然遇到问题,可以尝试在 Android Studio 中点击 "File" -> "Invalidate Caches / Restart" 重启 Android Studio。如果问题仍然存在,你可以尝试清除项目的 Gradle 缓存,方法是在命令行中运行 `./gradlew clean`。
'scrollBy(int, androidx.recyclerview.widget.RecyclerView.Recycler, androidx.recyclerview.widget.RecyclerView.State)' is not public in 'androidx.recyclerview.widget.LinearLayoutManager'. Cannot be accessed from outside package
抱歉,我的回答还是有误。这是因为scrollBy是LinearLayoutManager的一个非公开的方法,只能在LinearLayoutManager内部使用,不能从外部访问。因此,我们需要通过其他方式来实现RecyclerView的平滑滚动。以下是一个基于Scroller类的实现方法:
```
public void smoothScrollBy(int dx, int dy) {
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
};
smoothScroller.setTargetPosition(getPositionForVelocity(dx, dy));
startSmoothScroll(smoothScroller);
}
protected int getPositionForVelocity(int dx, int dy) {
if (getChildCount() == 0) {
return 0;
}
final View nearestChild = findNearestChild();
if (nearestChild == null) {
return 0;
}
final int currentPosition = getPosition(nearestChild);
if (currentPosition == RecyclerView.NO_POSITION) {
return 0;
}
final int direction = computeScrollDirectionForPosition(currentPosition, dx, dy);
if (direction == RecyclerView.NO_POSITION) {
return 0;
}
final int targetPosition = currentPosition + direction;
if (targetPosition < 0 || targetPosition >= getItemCount()) {
return 0;
}
return targetPosition;
}
protected int computeScrollDirectionForPosition(int targetPosition, int dx, int dy) {
if (getChildCount() == 0) {
return RecyclerView.NO_POSITION;
}
final View firstChild = getChildAt(0);
if (firstChild == null) {
return RecyclerView.NO_POSITION;
}
final int currentPosition = getPosition(firstChild);
if (currentPosition == RecyclerView.NO_POSITION) {
return RecyclerView.NO_POSITION;
}
final int direction = getScrollDirectionForPosition(currentPosition, targetPosition);
if (direction == RecyclerView.NO_POSITION) {
return RecyclerView.NO_POSITION;
}
final int distance = computeDistanceToPosition(targetPosition);
if (distance == 0) {
return RecyclerView.NO_POSITION;
}
return (int) Math.ceil((double) distance / (double) getDistancePerChild());
}
protected int getDistancePerChild() {
return Math.round(getChildAt(0).getHeight() * 1.0f);
}
protected int computeDistanceToPosition(int targetPosition) {
final int targetChildIndex = targetPosition - getPosition(getChildAt(0));
return targetChildIndex * getDistancePerChild();
}
protected int getScrollDirectionForPosition(int currentPosition, int targetPosition) {
return targetPosition < currentPosition ? -1 : 1;
}
protected View findNearestChild() {
final int childCount = getChildCount();
if (childCount == 0) {
return null;
}
View nearestChild = null;
int nearestDistance = Integer.MAX_VALUE;
final int centerX = getWidth() / 2;
final int centerY = getHeight() / 2;
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final int distanceX = Math.abs(getLeftDecorationWidth(child) + getRightDecorationWidth(child) + child.getMeasuredWidth() / 2 - centerX);
final int distanceY = Math.abs(getTopDecorationHeight(child) + getBottomDecorationHeight(child) + child.getMeasuredHeight() / 2 - centerY);
final int distance = distanceX * distanceX + distanceY * distanceY;
if (distance < nearestDistance) {
nearestChild = child;
nearestDistance = distance;
}
}
return nearestChild;
}
```
这个实现方法是通过LinearSmoothScroller来实现的,首先创建一个LinearSmoothScroller对象,然后设置目标位置,最后启动平滑滚动。在getPositionForVelocity、computeScrollDirectionForPosition、computeDistanceToPosition和findNearestChild等函数中,实现了计算滚动距离、滚动方向、目标位置和最近的子View等功能。
阅读全文