user2496887 user2496887 - 3 months ago 31
Android Question

Cannot scroll up because of endless scroll - setOnScrollListener

I'm making app with Swipe down to refresh functionality. And it works fine.

Here the code I used:

swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeColors(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLUE, Color.CYAN);

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

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

int topRowVerticalPostion = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(dx == 0 && topRowVerticalPostion >= 0);
}
});


Then I added Swipe up to load more functionality. But after that I noticed, that I cannot scroll up to previous item, when before I scrolled down to N-th item. When I tried scroll up, it call RefreshLayout.

Code I used (https://gist.github.com/ssinss/e06f12ef66c51252563e):

mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
@Override
public void onLoadMore(int current_page) {
Log.d("SCROLL PAST UPDATE", "You hit me");

//maintain scroll position
int lastFirstVisiblePosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPosition(lastFirstVisiblePosition);

loadMore(articles);
}
});


Full code:

public class Tab3 extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

private boolean isLoading = true;
int currentFirstVisibleItem, currentVisibleItemCount, currentScrollState, SCROLL_STATE_IDLE = 0;

public static final String TAG = "MyRecyclerList";
private List<ListItems> listItemsList = new ArrayList<ListItems>();

private RecyclerView mRecyclerView;
private MyRecyclerViewAdapter2 mAdapter;
protected LinearLayoutManager mLayoutManager;

private int page = 1;
private int post_count;
private String count;
private String jsonCategory;
private static final String url = "http://example.com/api/get_category_posts/?slug=";
private static final String articles = "article/";

private ProgressDialog progressDialog;
SwipeRefreshLayout swipeRefreshLayout;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_3,container,false);

mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);

updateList(articles);


swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeColors(Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLUE, Color.CYAN);

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

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


int topRowVerticalPostion = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(dx == 0 && topRowVerticalPostion >= 0);
}
});


mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
@Override
public void onLoadMore(int current_page) {
Log.d("SCROLL PAST UPDATE", "You hit me");

//maintain scroll position
int lastFirstVisiblePosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPosition(lastFirstVisiblePosition);

loadMore(articles);
}
});

return v;
}

public void updateList (String category) {
page = 1;
category = url + category;

mAdapter = new MyRecyclerViewAdapter2(getActivity(), listItemsList);
mRecyclerView.setAdapter(mAdapter);

RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());

mAdapter.clearAdapter();

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, category, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Log.d(TAG, response.toString());
swipeRefreshLayout.setRefreshing(false);

try {
JSONArray posts = response.getJSONArray("posts");
post_count = response.getInt("count");

for (int i = 0; i < post_count; i++) {
JSONObject singlePost = posts.getJSONObject(i);

ListItems item = new ListItems();

try {
item.setTitle(singlePost.getString("title_plain"));
item.setSlug(singlePost.getString("slug"));
item.setUrl(singlePost.getString("url"));
item.setContent(singlePost.getString("content"));
item.setDate(singlePost.getString("date"));
item.setThumbnail(singlePost.getString("thumbnail"));
}catch (JSONException e){
//item.setThumbnail("drawable://" + R.drawable.website_placeholder);
e.printStackTrace();
}

//jsonCategory = singlePost.getString()
listItemsList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse (VolleyError error) {
VolleyLog.d(TAG, "Помилка" + error.getMessage());
swipeRefreshLayout.setRefreshing(false);
}
});

queue.add(jsonObjectRequest);
}

public void loadMore (String category) {
page = page + 1;
category = url + category + "&page=" + page;

mAdapter = new MyRecyclerViewAdapter2(getActivity(), listItemsList);
mRecyclerView.setAdapter(mAdapter);

RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());

showProgressDialog();

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, category, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Log.d(TAG, response.toString());
hideProgressDialog();

try {
JSONArray posts = response.getJSONArray("posts");
post_count = response.getInt("count");

for (int i = 0; i < post_count; i++) {
JSONObject singlePost = posts.getJSONObject(i);
//JSONArray singlePost = response.getJSONArray("posts").getJSONObject(i);

ListItems item = new ListItems();

try {
item.setTitle(singlePost.getString("title_plain"));
item.setSlug(singlePost.getString("slug"));
item.setUrl(singlePost.getString("url"));
item.setContent(singlePost.getString("content"));
item.setDate(singlePost.getString("date"));
item.setThumbnail(singlePost.getString("thumbnail"));
}catch (JSONException e){
//item.setThumbnail("drawable://" + R.drawable.website_placeholder);
e.printStackTrace();
}

//jsonCategory = singlePost.getString()
listItemsList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse (VolleyError error) {
VolleyLog.d (TAG, "Помилка" + error.getMessage());
hideProgressDialog();
}
});

queue.add(jsonObjectRequest);
}

public void showProgressDialog () {
if(progressDialog == null){
progressDialog = new ProgressDialog(this.getActivity());
progressDialog.setMessage("Будь ласка, зачекайте...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}

public void hideProgressDialog () {
if(progressDialog != null){
progressDialog.dismiss();
progressDialog = null;
}
}


@Override
public void onRefresh(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
updateList(articles);
mAdapter.notifyDataSetChanged();
}
}, 1000);
}

}

Answer

solved by deleting this:

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

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

                int topRowVerticalPostion = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
                swipeRefreshLayout.setEnabled(dx == 0 && topRowVerticalPostion >= 0);
            }
        });

And add this to mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {

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

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

            int topRowVerticalPostion = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
            swipeRefreshLayout.setEnabled(dx == 0 && topRowVerticalPostion >= 0);
        }
Comments