gpt4 book ai didi

java - 如何在具有多种类型的customViewHolder的recyclerView中实现recyclerView.smoothScrollToPosition(position)?

转载 作者:行者123 更新时间:2023-12-02 10:12:34 28 4
gpt4 key购买 nike

我的recycleView有3种类型的viewHolder。

位置 0 是 viewHolder1。

位置1-41是viewHolder2。

位置 42 是 viewHolder3。

但如果 myDataSource.size() 超过 42,它将创建 viewHolder3。

所以我需要使 recyclerView.smoothScrollToPosition(position) 工作。

请帮忙......

这是我的 LinearLayoutManagerWithSmoothScroller

public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {



public LinearLayoutManagerWithSmoothScroller(Context context) {
super(context);
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
View firstVisibleChild = recyclerView.getChildAt(0);
final int childHeight = firstVisibleChild.getHeight();

int distanceInPixels = ((findFirstVisibleItemPosition() - position) * childHeight);
if (distanceInPixels == 0) {
distanceInPixels = (int) Math.abs(firstVisibleChild.getY());
}




SmoothScroller smoothScroller = new SmoothScroller(recyclerView.getContext(), Math.abs(distanceInPixels), 1000);
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}



private class SmoothScroller extends LinearSmoothScroller {
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
private final float distanceInPixels;
private final float duration;

public SmoothScroller(Context context, int distanceInPixels, int duration) {
super(context);
this.distanceInPixels = distanceInPixels;
float millisecondsPerPx = calculateSpeedPerPixel(context.getResources().getDisplayMetrics());
this.duration = distanceInPixels < TARGET_SEEK_SCROLL_DISTANCE_PX ?
(int) (Math.abs(distanceInPixels) * millisecondsPerPx) : duration;
}

@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManagerWithSmoothScroller.this
.computeScrollVectorForPosition(targetPosition);
}

@Override
protected int calculateTimeForScrolling(int dx) {
float proportion = (float) dx / distanceInPixels;
return (int) (duration * proportion);
}
}

}

并在 Activity 中调用它

call = Quiz5Manager.getInstance(QuizGameRankingActivity.this).getQuiz5Interface().loadQuiz5Ranking(BaseApplication.sharedPreferences.getString("facebook_id", ""));
call.enqueue(new Callback<Quiz5Ranking_Model>() {
@Override
public void onResponse(Call<Quiz5Ranking_Model> call, Response<Quiz5Ranking_Model> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Quiz5Ranking_Model quiz5Ranking_model = response.body();
List<Quiz5Ranking_UserRank_Model> all_rank_model = quiz5Ranking_model.getRankUsers();
all_rank_model.add(quiz5Ranking_model.getUserRank());
mAdapter = new QuizRankingAdapter(QuizGameRankingActivity.this, all_rank_model);
recycleview.setAdapter(mAdapter);
recycleview.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(QuizGameRankingActivity.this));
mAdapter.notifyDataSetChanged();
recycleview.smoothScrollToPosition(20);

int user_rank = quiz5Ranking_model.getUserRank().getRankId();
if (user_rank > 44) {
showToast("more than 44");
} else {
if (user_rank <= 3) {
showToast("Top 3");
} else if (user_rank >= 4 && user_rank <= 44) {
showToast("Normal");
}
}
}
}
}

@Override
public void onFailure(Call<Quiz5Ranking_Model> call, Throwable t) {
showToast("Fail to load. Please try again later.");
}
});

我在这一行遇到了空指针异常

View firstVisibleChild = recyclerView.getChildAt(0);

最佳答案

既然您已经可以使用默认滚动条,为什么还要使用自定义平滑滚动条呢?并且避免在回调后初始化适配器。在创建时使用空白数组列表对其进行初始化,然后将数据更新到适配器中,这将是一个很好的做法,否则适配器将在每个每个 api 回调中进行初始化。

此外,您还需要进行后期运行以在后帧中平滑滚动,例如,

recyclerview.post(new Runnable() {
@Override
public void run() {
recycleview.smoothScrollToPosition(20);
}
});

关于java - 如何在具有多种类型的customViewHolder的recyclerView中实现recyclerView.smoothScrollToPosition(position)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54898887/

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