简体   繁体   English

在RecyclerView.OnScrollListener中使用locationlistener

[英]Use locationlistener in RecyclerView.OnScrollListener

I need to get user location and send request by this location to server. 我需要获取用户位置并将该位置的请求发送到服务器。 I have a recyclerview for showing the data of server. 我有一个用于显示服务器数据的recyclerview。 To update location I use this code in onStart in the main activity and want to get location in idle state of recyclerview. 为了更新位置,我在主要活动的onStart中使用此代码,并希望在recyclerview的空闲状态下获取位置。 Now I don't know how can I implement the location listener. 现在,我不知道如何实现位置侦听器。

Maint Activity: 维护活动:

if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,  new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }else{
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 10000, 0, locationListener);

    }

and: 和:

public abstract class StoryPaginationScrollListener extends RecyclerView.OnScrollListener{

LinearLayoutManager layoutManager;
LocationListener locationListener;
Context context;


public StoryPaginationScrollListener(LinearLayoutManager layoutManager, LocationListener locationListener, Context context) {
    this.layoutManager = layoutManager;
    this.locationListener = locationListener;
    this.context = context;
}


@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

    if(newState == SCROLL_STATE_IDLE){

    }
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    int visibleItemCount = layoutManager.getChildCount();
    int totalItemCount = layoutManager.getItemCount();
    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();





    if (!isLoading() && !isLastPage()) {
        }
        if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0) {
            loadMoreItems();
        }
    }
}



protected abstract void loadMoreItems();

public abstract int getTotalPageCount();

public abstract boolean isLastPage();

public abstract boolean isLoading();


}

In need a location in this condition: if(newState == SCROLL_STATE_IDLE) and use this location in loadMoreItems(). 在这种情况下需要一个位置: if(newState == SCROLL_STATE_IDLE)并在loadMoreItems()中使用此位置。

try to implement LocationListener like this 尝试像这样实现LocationListener

class MyActivity extends Activity implements LocationsListener{
    public Location location

    @Override
    public void onLocationChanged(Location location) {
        this.location = location
    }

    public void someLogic(){
        new RecyclerView.OnScrollListener(){
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState == SCROLL_STATE_IDLE){
                if(location != null){
                    //write your code here
                }
            }

        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    };
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView.OnScrollListener问题 - Problem with RecyclerView.OnScrollListener 无法从RecyclerView.OnScrollListener调用notifyItemInserted() - Cannot call notifyItemInserted() from RecyclerView.OnScrollListener 为什么RecyclerView.OnScrollListener不是接口而是抽象类? - Why RecyclerView.OnScrollListener is not an interface but an abstract class? 为什么RecyclerView.OnScrollListener()仅影响recyclerview的最后一项 - why RecyclerView.OnScrollListener() affects on only last item of recyclerview 具有动态加载功能的Android RecyclerView.OnScrollListener无法更新UI /怪异行为? - Android RecyclerView.OnScrollListener with dynamic loading not updating UI/ weird behavior? RecyclerView.OnScrollListener:一次滚动实例被多次调用 - RecyclerView.OnScrollListener: being called multiple times for once instance of scroll 在 RecyclerView.OnScrollListener 中设置视图可见性时的性能 - Performance when setting visibility of a View in RecyclerView.OnScrollListener RecyclerView.OnScrollListener和ScrollingView:我无法为滚动结束创建逻辑 - RecyclerView.OnScrollListener and ScrollingView : I am unable to create logic for scroll end 我们如何区分 RecyclerView.OnScrollListener 中的用户滚动和 scrollToPosition() 滚动? - How Do We Distinguish User Scrolls from scrollToPosition() Scrolls in RecyclerView.OnScrollListener? RecyclerView OnScrollListener()问题 - RecyclerView OnScrollListener() Issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM