RecyclerViewEnhanced
介绍:
一个为RecyclerView添加 swipe, click 以及其它功能的Library。
运行效果:
使用说明:
build.gradle
dependencies {
compile 'com.nikhilpanju.recyclerviewenhanced:recyclerviewenhanced:1.0.0'
}
创建RecyclerTouchListener实例
onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
设置IndependentViews和FadeViews(如果需要)
IndependentView是独立于整行可以被单独点击的view。FadeView是一行滑动至打开或者关闭的时候分别淡入和淡出的View。
onTouchListener.setIndependentViews(R.id.rowButton)
.setViewsToFade(R.id.rowButton)
使用setClickable()实现OnRowClickListener
setClickable()将开启item以及IndependentView的点击功能
.setClickable(new RecyclerTouchListener.OnRowClickListener() {
@Override
public void onRowClicked(int position) {
// Do something
}
@Override
public void onIndependentViewClicked(int independentViewID, int position) {
// Do something
}
})
开启Swipe功能
设置使用Swipe菜单并使用setSwipeable()启用Swipe。
.setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
.setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
@Override
public void onSwipeOptionClicked(int viewID, int position) {
if (viewID == R.id.add) {
// Do something
} else if (viewID == R.id.edit) {
// Do something
} else if (viewID == R.id.change) {
// Do something
}
}
});
代码片段
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mAdapter = new MainAdapter(this, getData());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
onTouchListener
.setIndependentViews(R.id.rowButton)
.setViewsToFade(R.id.rowButton)
.setClickable(new RecyclerTouchListener.OnRowClickListener() {
@Override
public void onRowClicked(int position) {
ToastUtil.makeToast(getApplicationContext(), "Row " + (position + 1) + " clicked!");
}
@Override
public void onIndependentViewClicked(int independentViewID, int position) {
ToastUtil.makeToast(getApplicationContext(), "Button in row " + (position + 1) + " clicked!");
}
})
.setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
.setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
@Override
public void onSwipeOptionClicked(int viewID, int position) {
String message = "";
if (viewID == R.id.add) {
message += "Add";
} else if (viewID == R.id.edit) {
message += "Edit";
} else if (viewID == R.id.change) {
message += "Change";
}
message += " clicked for row " + (position + 1);
ToastUtil.makeToast(getApplicationContext(), message);
}
});
mRecyclerView.addOnItemTouchListener(onTouchListener);
已下载
0