Android 水平recyclerview 自动滚动到中心
Android horizontal recyclerview automatically scrolls to the centre
我希望我的水平 recyclerview 在设置适配器后始终显示第一个项目,而不是滚动到中心项目。我怎样才能做到这一点?
这是我的 recommendation.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"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_marginLeft="10dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/zone"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/xyz"
app:layout_constraintEnd_toEndOf="@+id/xyz"
android:layout_marginTop="3dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xyz"
android:gravity="center"
android:textSize="16sp"
android:layout_marginLeft="15dp"
app:layout_constraintTop_toBottomOf="@id/zone"
android:layout_marginTop="5dp"
app:layout_constraintStart_toEndOf="@id/img"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/day"
android:gravity="center"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/xyz"
app:layout_constraintStart_toStartOf="@id/xyz"
app:layout_constraintEnd_toEndOf="@id/xyz"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="6dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的适配器class:
class recom_adapter extends RecyclerView.Adapter<recom_adapter.ViewHolder> {
private List<saved_zone> zoneList;
private Context context;
public static PrefConfig prefConfig;
public recom_adapter(List<saved_zone> zoneList, Context context) {
this.zoneList = zoneList;
this.context = context;
}
@NonNull
@Override
public recom_adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation,parent,false);
return new recom_adapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull recom_adapter.ViewHolder holder, int position) {
saved_zone sz = zoneList.get(position);
Glide.with(context).load(sz.getImageUrl).into(holder.img);
holder.xyz.setText(sz.getXYZ());
holder.day.setText(sz.getDate());
}
@Override
public int getItemCount() {
return zoneList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
TextView zone, xyz, day;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
zone = itemView.findViewById(R.id.zone);
xyz = itemView.findViewById(R.id.xyz);
day = itemView.findViewById(R.id.day);
}
}
}
这就是我在 mainActivity 的适配器中设置 recyclerView 的方式:
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, true));
recomData = response.body();
recom_adapter = new recom_adapter(recomData, getActivity());
recomm_recycler.setAdapter(recom_adapter);
垂直回收器视图也有类似的问题。在我的例子中添加
android:focusableInTouchMode="true"
RecyclerView 的父级帮助了我。
在您的主要 XML 父布局中试试这个
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, true));
你上面那行有问题
将 true
更改为 false
新代码
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false));
我希望我的水平 recyclerview 在设置适配器后始终显示第一个项目,而不是滚动到中心项目。我怎样才能做到这一点?
这是我的 recommendation.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"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_marginLeft="10dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/zone"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/xyz"
app:layout_constraintEnd_toEndOf="@+id/xyz"
android:layout_marginTop="3dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xyz"
android:gravity="center"
android:textSize="16sp"
android:layout_marginLeft="15dp"
app:layout_constraintTop_toBottomOf="@id/zone"
android:layout_marginTop="5dp"
app:layout_constraintStart_toEndOf="@id/img"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/day"
android:gravity="center"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/xyz"
app:layout_constraintStart_toStartOf="@id/xyz"
app:layout_constraintEnd_toEndOf="@id/xyz"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="6dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的适配器class:
class recom_adapter extends RecyclerView.Adapter<recom_adapter.ViewHolder> {
private List<saved_zone> zoneList;
private Context context;
public static PrefConfig prefConfig;
public recom_adapter(List<saved_zone> zoneList, Context context) {
this.zoneList = zoneList;
this.context = context;
}
@NonNull
@Override
public recom_adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation,parent,false);
return new recom_adapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull recom_adapter.ViewHolder holder, int position) {
saved_zone sz = zoneList.get(position);
Glide.with(context).load(sz.getImageUrl).into(holder.img);
holder.xyz.setText(sz.getXYZ());
holder.day.setText(sz.getDate());
}
@Override
public int getItemCount() {
return zoneList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
TextView zone, xyz, day;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
zone = itemView.findViewById(R.id.zone);
xyz = itemView.findViewById(R.id.xyz);
day = itemView.findViewById(R.id.day);
}
}
}
这就是我在 mainActivity 的适配器中设置 recyclerView 的方式:
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, true));
recomData = response.body();
recom_adapter = new recom_adapter(recomData, getActivity());
recomm_recycler.setAdapter(recom_adapter);
垂直回收器视图也有类似的问题。在我的例子中添加
android:focusableInTouchMode="true"
RecyclerView 的父级帮助了我。
在您的主要 XML 父布局中试试这个
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, true));
你上面那行有问题
将 true
更改为 false
新代码
recomm_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false));