gpt4 book ai didi

java - RecyclerView 中的 SmoothScroller

转载 作者:行者123 更新时间:2023-11-29 18:42:28 25 4
gpt4 key购买 nike

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp">

<View
android:id="@+id/item_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:background="#D3D3D3" />

<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="22sp"
tools:text="Léon: The Professional" />

<LinearLayout
android:id="@+id/sub_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical">

<TextView
android:id="@+id/sub_item_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Genre: Crime, Drama, Thriller" />

<TextView
android:id="@+id/sub_item_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Year: 1993" />
</LinearLayout>
</LinearLayout>

在我的 recyclerview 适配器中:

@Override
public void onBindViewHolder(RecViewHolder holder, int position) {
Movie movie = list.get(position);

boolean expanded = movie.isExpanded();
subItem.setVisibility(expanded ? View.VISIBLE : View.GONE);

holder.title.setOnClickListener(v -> {
boolean expanded = movie.isExpanded();
movie.setExpanded(!expanded);
notifyItemChanged(position);
});
}

正如您在代码中看到的,当您单击某个项目时,子项目会隐藏/显示。

我想要实现的是:当您单击列表中的最后一项时,我希望它显示整行(包括子项)。目前它将可见性更改为 VISIBLE,但是该行的 subItem 对用户是不可见的直到用户滚动到它

在下图中,God Father 是最后一个可见项。如何显示整行包括 God Father 作为标题和子项 当用户单击时

Picture

最佳答案

如果滚动到 RecyclerView 扩展项的底部,问题就会解决。关键点是使用 LinearSmoothScroller 进行滚动。试试下面的代码:

public class RecAdapter extends RecyclerView.Adapter<RecAdapter.RecViewHolder> {

private List<Movie> list;
private RecyclerView mRecyclerView;
private RecyclerView.SmoothScroller mSmoothScroller;

public RecAdapter(List<Movie> list) {
this.list = list;
}

@Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.item_movie, parent, false);
return new RecViewHolder(view);
}

@Override
public void onBindViewHolder(RecViewHolder holder, int position) {
Movie movie = list.get(position);

holder.bind(movie);

holder.itemView.setOnClickListener(v -> {
boolean expanded = movie.isExpanded();
movie.setExpanded(!expanded);
notifyItemChanged(position);

if (movie.isExpanded()) {
mSmoothScroller.setTargetPosition(position);
new Handler().postDelayed(() -> mRecyclerView.getLayoutManager().startSmoothScroll(mSmoothScroller), 100);
}
});
}

@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;

mSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_ANY;
}

@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return 150f / displayMetrics.densityDpi;
}
};
}

public class RecViewHolder extends RecyclerView.ViewHolder {

private TextView title;
private TextView genre;
private TextView year;
private View subItem;

public RecViewHolder(View itemView) {
super(itemView);

title = itemView.findViewById(R.id.item_title);
genre = itemView.findViewById(R.id.sub_item_genre);
year = itemView.findViewById(R.id.sub_item_year);
subItem = itemView.findViewById(R.id.sub_item);
}

private void bind(Movie movie) {
boolean expanded = movie.isExpanded();

subItem.setVisibility(expanded ? View.VISIBLE : View.GONE);

title.setText(movie.getTitle());
genre.setText("Genre: " + movie.getGenre());
year.setText("Year: " + movie.getYear());
}
}
}

结果如下:

enter image description here

关于java - RecyclerView 中的 SmoothScroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823129/

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