Needs
1.implementation 'com.daimajia.swipelayout:library:1.2.0@aar'2.DividetItemDecoration.java
3.MainActivity.java
4.SwipeRecyclerViewAdapter.java
5.YoutuberModel.java
6.activity_main.xml
7.swipe_layout.xml
Step 1:
implementation 'com.daimajia.swipelayout:library:1.2.0@aar'Step 2:
public class DividetItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private boolean mShowFirstDivider = false;
private boolean mShowLastDivider = false;
private DividetItemDecoration(Context context, AttributeSet attrs){
final TypedArray a = context
.obtainStyledAttributes(attrs, new int[android.R.attr.listDivider]);
mDivider = a.getDrawable(0);
a.recycle();
}
public DividetItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
boolean showLastDivider) {
this(context, attrs);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
public DividetItemDecoration(Drawable divider) {
mDivider = divider;
}
public DividetItemDecoration(Drawable divider, boolean showFirstDivider,
boolean showLastDivider) {
this(divider);
mShowFirstDivider = showFirstDivider;
mShowLastDivider = showLastDivider;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if(mDivider == null){
return;
}
if(parent.getChildPosition(view)<1){
return;
}
if(getOrientation(parent)== LinearLayout.VERTICAL){
outRect.top = mDivider.getIntrinsicHeight();
}else{
outRect.top = mDivider.getIntrinsicWidth();
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if(mDivider==null){
super.onDrawOver(c, parent, state);
return;
}
int left=0, right=0, top=0, bottom=0, size;
int orientation = getOrientation(parent);
int childCount = parent.getChildCount();
if(orientation == LinearLayoutManager.VERTICAL){
size = mDivider.getIntrinsicHeight();
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
}else{
size = mDivider.getIntrinsicWidth();
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
}
for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getTop() - params.topMargin;
bottom = top + size;
} else { //horizontal
left = child.getLeft() - params.leftMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
// divider terakhir
if (mShowLastDivider && childCount > 0) {
View child = parent.getChildAt(childCount - 1);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if (orientation == LinearLayoutManager.VERTICAL) {
top = child.getBottom() + params.bottomMargin;
bottom = top + size;
} else { // horizontal
left = child.getRight() + params.rightMargin;
right = left + size;
}
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private int getOrientation(RecyclerView parent){
if(parent.getLayoutManager() instanceof LinearLayoutManager){
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
}else{
throw new IllegalStateException("bla bla");
}
}
}
Step 3:
public class MainActivity extends AppCompatActivity {
private TextView tvEmptyTextView;
private RecyclerView mRecyclerView;
private ArrayList<YoutuberModel> mDataSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvEmptyTextView = (TextView) findViewById(R.id.empty_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mDataSet = new ArrayList<>();
loadData();
if(mDataSet.isEmpty()){
mRecyclerView.setVisibility(View.GONE);
tvEmptyTextView.setVisibility(View.VISIBLE);
}else{
mRecyclerView.setVisibility(View.VISIBLE);
tvEmptyTextView.setVisibility(View.GONE);
}
SwipeRecyclerViewAdapter mAdapter = new SwipeRecyclerViewAdapter(this, mDataSet);
((SwipeRecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Log.e("RecyclerView", "onScrollStateChanged");
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
public void loadData() {
for (int i = 0; i <= 20; i++) {
mDataSet.add(new YoutuberModel("Youtuber " + i, "andiaspin" + i + "@gmail.com"));
}
}
}
Step 4:
public class SwipeRecyclerViewAdapter extends RecyclerSwipeAdapter<SwipeRecyclerViewAdapter.SimpleViewHolder> {
private Context mContext;
private ArrayList<YoutuberModel> studentList;
public SwipeRecyclerViewAdapter(Context context, ArrayList<YoutuberModel> objects) {
this.mContext = context;
this.studentList = objects;
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.swipe_layout, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
final YoutuberModel item = studentList.get(position);
viewHolder.Name.setText(item.getName() + " - Row Position " + position);
viewHolder.EmailId.setText(item.getEmailId());
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
//dari kiri
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper1));
//dari kanan
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, viewHolder.swipeLayout.findViewById(R.id.bottom_wraper));
viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onClose(SwipeLayout layout) {
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
viewHolder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, " Click : " + item.getName() + " \n" + item.getEmailId(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked on Information " + viewHolder.Name.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.Share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on Share " + viewHolder.Name.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on Edit " + viewHolder.Name.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemManger.removeShownLayouts(viewHolder.swipeLayout);
studentList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, studentList.size());
mItemManger.closeAllItems();
Toast.makeText(v.getContext(), "Deleted " + viewHolder.Name.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
mItemManger.bindView(viewHolder.itemView, position);
}
@Override
public int getItemCount() {
return studentList.size();
}
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder{
public SwipeLayout swipeLayout;
public TextView Name;
public TextView EmailId;
public TextView Delete;
public TextView Edit;
public TextView Share;
public ImageButton btnLocation;
public SimpleViewHolder(View itemView) {
super(itemView);
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
Name = (TextView) itemView.findViewById(R.id.Name);
EmailId = (TextView) itemView.findViewById(R.id.EmailId);
Delete = (TextView) itemView.findViewById(R.id.Delete);
Edit = (TextView) itemView.findViewById(R.id.Edit);
Share = (TextView) itemView.findViewById(R.id.Share);
btnLocation = (ImageButton) itemView.findViewById(R.id.btnLocation);
}
}
}
Step 5:
public class YoutuberModel implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String emailId;
public YoutuberModel() {
}
public YoutuberModel(String name, String emailId) {
this.name = name;
this.emailId = emailId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
Step 6:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:elevation="2dp"
android:layout_weight="1"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Records"
android:visibility="gone" />
</LinearLayout>
Step 7:
swipe_layout.xml
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/swipe"
app:leftEdgeSwipeOffset="0dp"
app:rightEdgeSwipeOffset="0dp"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/bottom_wraper"
android:layout_width="240dp"
android:weightSum="3"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="@+id/Edit"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0076a5"
android:gravity="center"
android:text="Edit"
android:textColor="#fff"/>
<TextView
android:id="@+id/Share"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#2cbb00"
android:gravity="center"
android:text="Share"
android:textColor="#fff" />
<TextView
android:id="@+id/Delete"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000"
android:gravity="center"
android:text="Delete"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_wrapper1"
android:layout_width="80dp"
android:layout_height="match_parent"
android:weightSum="1">
<ImageButton
android:id="@+id/btnLocation"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:src="@android:drawable/ic_menu_info_details"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:elevation="5dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="12sp"/>
<TextView
android:id="@+id/EmailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Email ID"
android:textColor="@android:color/black"
android:textSize="15sp"/>
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
No comments:
Post a Comment