我正在使用向下滑动来刷新功能。它工作正常。
这里是我使用的代码:
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);
}
});
然后我添加了向上滑动以加载更多功能。但之后我注意到,在我向下滚动到第N个项目之前,我无法向上滚动到上一个项目。当我尝试向上滚动时,它会调用RefreshLayout。
我使用的代码(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);
}
});
完整代码:
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);
}
}
答案 0 :(得分:0)
通过删除以下内容解决:
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 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);
}
答案 1 :(得分:0)
如果您使用此库'com.lzy.widget:view-core:0.2.1'
并与SwipeRefresh
集成添加此代码,请为我工作。
scrollableLayout.setOnScrollListener(new HeaderViewPager.OnScrollListener() {
@Override
public void onScroll(int currentY, int maxY) {
pagerHeader.setTranslationY(currentY / 2);
int topRowVerticalPostion = (rvRestaurant == null || rvRestaurant.getChildCount() == 0) ? 0 : rvRestaurant.getChildAt(0).getTop();
mSwipeRefreshLayout.setEnabled(currentY == 0 && topRowVerticalPostion >= 0);
}
});