大家好我正在开发一个应用程序,其中我使用了recyclerview和mapfragment。我已经制作了一个带有recyclerview的旋转木马和循环再循环视图,我试图根据特定项目的LatLong改变摄像机位置,现在我的问题是当我每次返回同一位置时滚动recyclerview。我之前已经实现了viewpager,并且在那里我使用了onPageSelected方法,该方法工作正常,但现在我已经切换到recyclerview我遇到了这个问题。 我不知道我哪里出错了 所以,如果有人可以在这里帮助我。 谢谢
MainActivity代码
carousel_recycler = (RecyclerView)findViewById(R.id.carousel_recycler);
providers = new ArrayList<>();
manager = new CenterZoomLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
final int scrollPosition = manager.findFirstVisibleItemPosition();
carousel_recycler.setLayoutManager(manager);
carousel_recycler.setHasFixedSize(true);
place_data = getResources().getStringArray(R.array.nearby_place);
for(String places : place_data){
NearbyDataProvider nearbyDataProvider = new NearbyDataProvider(listItems[i],places,latitude[j],longitude[k]);
providers.add(nearbyDataProvider);
i++;
j++;
k++;
}
adapter = new CarouselRecycler_Adapter(providers);
carousel_recycler.setAdapter(adapter);
LinearSnapHelper snapHelper = new LinearSnapHelper(){
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
View centerView = findSnapView(layoutManager);
if (centerView == null)
return RecyclerView.NO_POSITION;
int position = layoutManager.getPosition(centerView);
int targetPosition = -1;
if (layoutManager.canScrollHorizontally()) {
if (velocityX < 0) {
targetPosition = position - 1;
} else {
targetPosition = position + 1;
}
}
if (layoutManager.canScrollVertically()) {
if (velocityY < 0) {
targetPosition = position - 1;
} else {
targetPosition = position + 1;
}
}
final int firstItem = 0;
final int lastItem = layoutManager.getItemCount() - 1;
targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
return targetPosition;
}
};
snapHelper.attachToRecyclerView(carousel_recycler);
carousel_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
index = newState;
if(providers.size()>0){
if(!providers.get(index).getLattitude().isEmpty()
&& !providers.get(index).getLongitude().isEmpty()){
double lat = Double.parseDouble(providers.get(index).getLattitude());
double lang = Double.parseDouble(providers.get(index).getLongitude());
changeCameraPosition(lat,lang);
}
else {
Toast.makeText(MapsMarkerActivity.this, "No last location found", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
/*index = scrollPosition;
if(providers.size()>0){
if(!providers.get(scrollPosition).getLattitude().isEmpty()
&& !providers.get(scrollPosition).getLongitude().isEmpty()){
double lat = Double.parseDouble(providers.get(scrollPosition).getLattitude());
double lang = Double.parseDouble(providers.get(scrollPosition).getLongitude());
changeCameraPosition(lat,lang);
}
else {
Toast.makeText(MapsMarkerActivity.this, "No last location found", Toast.LENGTH_SHORT).show();
}
}*/
}
});
}
答案 0 :(得分:1)
这会解决您的问题吗?
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
try {
LinearLayoutManager layoutManager = ((LinearLayoutManager) rv.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
Log.e("refresh", "State - " + newState + " : firstVisiblePosition" + firstVisiblePosition);
} catch (Exception e) {
e.printStackTrace();
}
}
});