-
Notifications
You must be signed in to change notification settings - Fork 444
Quick Setup
Running man! edited this page Dec 27, 2016
·
8 revisions
Assume you have added lib dependency. If you have no idea how to add lib dependency, check here.
Step 1. Create an Simple RefreshHeaderView
public class RefreshHeaderView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {
public RefreshHeaderView(Context context) {
super(context);
}
public RefreshHeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onRefresh() {
setText("REFRESHING");
}
@Override
public void onPrepare() {
setText("");
}
@Override
public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
if (!isComplete) {
if (yScrolled >= getHeight()) {
setText("RELEASE TO REFRESH");
} else {
setText("SWIPE TO REFRESH");
}
} else {
setText("REFRESH RETURNING");
}
}
@Override
public void onRelease() {
}
@Override
public void onComplete() {
setText("COMPLETE");
}
@Override
public void onReset() {
setText("");
}
}
Step 2. Create an Simple LoadMoreFooterView
public class LoadMoreFooterView extends TextView implements SwipeTrigger, SwipeLoadMoreTrigger {
public LoadMoreFooterView(Context context) {
super(context);
}
public LoadMoreFooterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onLoadMore() {
setText("LOADING MORE");
}
@Override
public void onPrepare() {
setText("");
}
@Override
public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
if (!isComplete) {
if (yScrolled <= -getHeight()) {
setText("RELEASE TO LOAD MORE");
} else {
setText("SWIPE TO LOAD MORE");
}
} else {
setText("LOAD MORE RETURNING");
}
}
@Override
public void onRelease() {
setText("LOADING MORE");
}
@Override
public void onComplete() {
setText("COMPLETE");
}
@Override
public void onReset() {
setText("");
}
}
Step 3. Edit activity layout xml
<?xml version="1.0" encoding="utf-8"?>
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeToLoadLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.aspsine.swipetoloadlayoutdemo.MainActivity">
<com.aspsine.swipetoloadlayoutdemo.RefreshHeaderView
android:id="@id/swipe_refresh_header"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp" />
<ListView
android:id="@id/swipe_target"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.aspsine.swipetoloadlayoutdemo.LoadMoreFooterView
android:id="@id/swipe_load_more_footer"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp" />
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
Note:
- refresh header view
android:id="@id/swipe_refresh_header"
- target view
android:id="@id/swipe_target"
- load more footer view
android:id="@id/swipe_load_more_footer"
Step 4. Write java code
public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
SwipeToLoadLayout swipeToLoadLayout;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
ListView listView = (ListView) findViewById(R.id.swipe_target);
swipeToLoadLayout.setOnRefreshListener(this);
swipeToLoadLayout.setOnLoadMoreListener(this);
mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1);
listView.setAdapter(mAdapter);
autoRefresh();
}
@Override
public void onRefresh() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(false);
mAdapter.add("REFRESH:\n" + new Date());
}
}, 2000);
}
@Override
public void onLoadMore() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setLoadingMore(false);
mAdapter.add("LOAD MORE:\n" + new Date());
}
}, 2000);
}
private void autoRefresh() {
swipeToLoadLayout.post(new Runnable() {
@Override
public void run() {
swipeToLoadLayout.setRefreshing(true);
}
});
}
}
All done. Very simple, isn't it?