gpt4 book ai didi

android - 如何滚动到 recyclerview 内的偏移量?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:35 27 4
gpt4 key购买 nike

如何在 recyclerview 中滚动到具有偏移量的特定位置?

我用过

public LinearLayoutManager linearlayoutmanager;
linearlayoutmanager.scrollToPositionWithOffset(0, scrolled);

我也用过scrollto

mRecyclerView.scrollTo(0, scrolled);

我也用过 smoothScrolltoposition

mRecyclerView.smoothScrollToPosition(scrolled);

但似乎没有一个适合我。

如果有人知道这件事,你能帮帮我吗

更新

设置 Recyclerview 适配器的功能

public void Nearby_Search_requestData() {
OkHttpClient client = new OkHttpClient();
client = MySSLTrust.trustcert(getActivity());

RestAdapter adapter = new RestAdapter.Builder().setEndpoint(SEARCH_MERCHANT_ENDPOINT).setClient(new OkClient(client)).build();
final RetrofitInterface api = adapter.create(RetrofitInterface.class);
if (api != null) {
api.post_Nearby_Search(page.toString(), new Callback<Nearby_Search_All>() {
@Override
public void success(final Nearby_Search_All server_data, Response response) {
if (server_data.getSuccess() == 1) {
if (server_data.getResults().size() == 0) {
if (page != 0) {
mRecyclerView.scrollToPosition(firstVis);
} else {
Log.d("hii", "zero");
// NO_MERCHANT.setVisibility(View.VISIBLE);
mAdapter = new searchrecyclerviewadapter(getActivity(), data, font);
mRecyclerView.setAdapter(mAdapter);
}
} else {
Log.d("scroll", "hii");
// NO_MERCHANT.setVisibility(View.INVISIBLE);
if (server_data.getSize() > server_data.getResults().size()) {
stay_on_this_page = true;
} else {
stay_on_this_page = false;
}
if (page == 0) {
prev_total = 0;
data.clear();
Log.d("hii", "Page is zero " + prev_total);
list_size = server_data.getSize();
} else {
Log.d("hii", "prev_total is " + prev_total + " Size is " + server_data.getSize() + " page is " + page);
prev_total = server_data.getSize() * page;
Log.d("hii", "Now prev_total is " + prev_total);
}
total_curr = server_data.getResults().size();

Log.d("hii", "first place is " + server_data.getResults().get(0).getShop_name());

for (int i = 0; i < total_curr; i++) {
information current = new information();
current.title = server_data.getResults().get(i).getShop_name();
String street = server_data.getResults().get(i).getAddress().getStreet();
String area = server_data.getResults().get(i).getAddress().getArea();
current.Address = street + " " + area;
String[] splited = area.split(",");
current.area1 = splited[1];
current.type = server_data.getResults().get(i).getShop_type();
current.rating = server_data.getResults().get(i).getRating();
current.distance = server_data.getResults().get(i).getDistance();
current.jeb_no = server_data.getResults().get(i).getJeb_no();
current.total_photos = server_data.getResults().get(i).getTotal_photos();
if (current.total_photos == 0) {

} else if (current.total_photos == 1) {
current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
} else if (current.total_photos == 2) {
current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
current.photo_2 = server_data.getResults().get(i).getPhotos().getPhoto_2();
} else {
current.photo_1 = server_data.getResults().get(i).getPhotos().getPhoto_1();
current.photo_2 = server_data.getResults().get(i).getPhotos().getPhoto_2();
}
data.add(current);
}
if (adapterinitialised) {
mAdapter.notifyDataSetChanged();
} else {
mAdapter = new searchrecyclerviewadapter(getActivity(), data, font);
// mAdapter.setClickListener(Search.class);
mRecyclerView.setAdapter(mAdapter);

adapterinitialised = true;
}
}
// mRecyclerView.scrollToPosition(firstVis);
Log.d("scroll", "hee");
scrolled = scrolled + 10;
Log.d("scroll", "scrolled: " + scrolled);
// ALL BELOW CODE I HAVE TRIED FOR SCROLLING BUT NONE WAS HELPFUL
/* mRecyclerView.post(new Runnable() {
@Override
public void run() {
mRecyclerView.smoothScrollToPosition(scrolled);

// or do mRecyclerView.scrollTo(0, scrolled);
}
});
*/
// l.scrollToPositionWithOffset(0, scrolled);
// mRecyclerView.smoothScrollToPosition(scrolled);
// mRecyclerView.scrollBy(0, scrolled);
// l.scrollToPosition();
// mRecyclerView.scrollToPosition(scrolled);
// mRecyclerView.smoothScrollBy(0, scrolled);
// mRecyclerView.scrollBy(0, 10);
// mRecyclerView.scrollTo(0, 10);

} else {
Log.d("hii", server_data.getMessage());
Toast.makeText(getActivity(), server_data.getMessage(), Toast.LENGTH_SHORT).show();
}
}

@Override
public void failure(RetrofitError retrofitError) {
Log.d("hii", "failure");
Log.e("hii", retrofitError.toString());
Toast.makeText(getActivity(), retrofitError.toString(), Toast.LENGTH_SHORT).show();
}
});
} else {
Log.d("hii", "api null");
}
}

最佳答案

这是我的答案。设置

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//or HORIZONTAL
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new Adapter(...);
recyclerView.setAdapter(adapter);

这提供了您的 RecyclerView、适配器和数据的方向(将 ... 替换为上下文、列表、数组或其他需要的数据)。比集

linearLayoutManager.scrollToPositionWithOffset(position, offset);

现在有趣的是:

  • 这将滚动到位置并将其放在列表的顶部,如果是 VERTICAL 线性布局管理器,则在 HORIZONTAL 的左侧
  • 如果 offset 为正数,VERTICAL 布局管理器会将此位置向下滚动 offset 像素(对于 HORIZONTAL 布局管理器向右)
  • 如果 position 为 0 且 offset 为正,则什么也不会发生,因为 RecyclerView 没有任何项目位于 0 位置的顶部(在 HORIZONTAL 布局管理器的左侧)
  • 如果 position 为 0 且 offset 为负,则 VERTICAL 布局管理器将向上滚动 0 位置(以像素为单位)(向左与 HORIZONTAL)
  • 如果 RecyclerView 的高度(水平方向的宽度)小于屏幕尺寸也不会发生任何事情
  • 如果要滚动到的位置在最后或接近末尾并且它的高度(水平方向情况下的宽度)相对较小,它也会错误地滚动

希望这会为您澄清 RecyclerView 的布局管理器的情况

关于android - 如何滚动到 recyclerview 内的偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271773/

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