用SwipyRefreshLayout实现下拉刷新,上滑加载更多

时间:2022-12-22 20:44:22

1.build.gradle中加入依赖包

 compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.github.orangegangsters:swipy:1.1.0@aar'

2.布局文件layout代码

 <com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout
android:id="@+id/swipy_refresh_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:direction="both">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout>

3.MainActivity.java代码

public class MainActivity extends AppCompatActivity {


private SwipyRefreshLayout mSwipyRefreshLayout;
private ListView mListView;
private List<Integer> mNumberLists = new ArrayList<>();
private final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

initView();
initData();
initEvent();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

private void initEvent() {
mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(SwipyRefreshLayoutDirection direction) {
if (direction == SwipyRefreshLayoutDirection.TOP) {
Log.i(TAG, "SwipyRefreshLayoutDirection: " + direction.name());
mSwipyRefreshLayout.setRefreshing(false);
} else if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
Log.i(TAG, "SwipyRefreshLayoutDirection: " + direction.name());
mSwipyRefreshLayout.setRefreshing(false);
}else {
Log.i(TAG, "SwipyRefreshLayoutDirection: " + direction.name());
mSwipyRefreshLayout.setRefreshing(false);
}
}
});
}

private void initData() {
for (int i = 0; i < 30; i++) {
mNumberLists.add(i);
}
ArrayAdapter arrayAdapter = new ArrayAdapter(this,
android.R.layout.test_list_item,
android.R.id.text1,
mNumberLists);
mListView.setAdapter(arrayAdapter);
}

private void initView() {
mSwipyRefreshLayout = (SwipyRefreshLayout) findViewById(R.id.swipy_refresh_layout);
mListView = (ListView) findViewById(R.id.listview);
}
}

4.源码下载地址

点击下载DEMO源码 http://pan.baidu.com/s/1c2HXqNI