Skip to content

Commit

Permalink
fix bug #13
Browse files Browse the repository at this point in the history
  • Loading branch information
limedroid committed Nov 13, 2017
1 parent 8fd4b54 commit c244bc3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerView;

public class MainActivity extends Activity {

XRecyclerView recyclerView;
SwipeRefreshLayout swipeLayout;
TextView tv_next;

TestRecAdapter adapter;

static final int MAX_PAGE = 5;
private int pageSize = 12;
private int pageSize = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = (XRecyclerView) findViewById(R.id.recyclerView);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
tv_next = (TextView) findViewById(R.id.tv_next);

tv_next.setOnClickListener(new View.OnClickListener() {
Expand All @@ -37,6 +42,12 @@ public void onClick(View v) {
}
});

swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
recyclerView.onRefresh();
}
});
initAdapter();
loadData(1);
}
Expand All @@ -51,7 +62,7 @@ private void initAdapter() {
public void onItemClick(int position, TestRecAdapter.Item model, int tag, TestRecAdapter.ViewHolder holder) {
super.onItemClick(position, model, tag, holder);

switch (tag){
switch (tag) {
case TestRecAdapter.TAG_CLICK:
//TODO 事件处理
break;
Expand All @@ -75,6 +86,11 @@ public void onLoadMore(int page) {


private void loadData(final int page) {
if (page > 1) {
Log.e("2222", "load more");
} else {
Log.e("2222", "refresh");
}
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
Expand All @@ -85,6 +101,8 @@ public void run() {
adapter.setData(list);
}
recyclerView.setPage(page, MAX_PAGE);

swipeLayout.setRefreshing(false);
}
}, 500L);

Expand Down
15 changes: 12 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@
</RelativeLayout>


<cn.droidlover.xrecyclerview.XRecyclerView
android:id="@+id/recyclerView"
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent">

<cn.droidlover.xrecyclerview.XRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>




</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class XRecyclerView extends RecyclerView {
private int currentPage = 1;
private boolean isRefresh = false;
private boolean isRefreshEnabled = true; //是否可刷新
private int lastVelocityY = 0;

XRecyclerAdapter adapter;

Expand Down Expand Up @@ -179,6 +180,7 @@ public void setLayoutManager(LayoutManager layout) {

@Override
public boolean fling(int velocityX, int velocityY) {
lastVelocityY = velocityY;
return super.fling((int) (velocityX * xFactor), (int) (velocityY * yFactor));
}

Expand Down Expand Up @@ -553,6 +555,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

if (newState == RecyclerView.SCROLL_STATE_IDLE
&& !loadMore
&& lastVelocityY > 0
&& getLastVisibleItemPosition(recyclerView.getLayoutManager()) + LOAD_MORE_ITEM_SLOP > totalCount) {

if (totalPage > currentPage) {
Expand Down Expand Up @@ -580,7 +583,6 @@ && getLastVisibleItemPosition(recyclerView.getLayoutManager()) + LOAD_MORE_ITEM_
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

}
};

Expand Down

0 comments on commit c244bc3

Please sign in to comment.