Last active
September 8, 2017 11:15
-
-
Save MuhammadRabeet/4af607f7665025f0aff68af2ea97a077 to your computer and use it in GitHub Desktop.
Swipe View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="56dp" | |
android:gravity="center_vertical" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/party_size_text_view" | |
android:layout_width="40dp" | |
android:layout_height="40dp" | |
android:layout_gravity="center" | |
android:background="@drawable/circle" | |
android:fontFamily="sans-serif" | |
android:gravity="center" | |
android:textColor="@android:color/white" | |
android:textSize="16sp" | |
android:text="@string/default_party_size"/> | |
<TextView | |
android:id="@+id/name_text_view" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="16dp" | |
android:layout_weight="1" | |
android:fontFamily="sans-serif" | |
android:textSize="16sp" | |
android:text="@string/default_guest_name"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.waitlist; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.example.android.waitlist.data.WaitlistContract; | |
public class GuestListAdapter extends RecyclerView.Adapter<GuestListAdapter.GuestViewHolder> { | |
private Context mContext; | |
private Cursor mCursor; | |
public GuestListAdapter(Context context, Cursor cursor) { | |
this.mContext = context; | |
this.mCursor = cursor; | |
} | |
/** | |
* Constructor using the context and the db cursor | |
* | |
* @param context the calling context/activity | |
* @param count | |
*/ | |
public GuestListAdapter(Context context, int count) { | |
this.mContext = context; | |
} | |
@Override | |
public GuestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
// Get the RecyclerView item layout | |
LayoutInflater inflater = LayoutInflater.from(mContext); | |
View view = inflater.inflate(R.layout.guest_list_item, parent, false); | |
return new GuestViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(GuestViewHolder holder, int position) { | |
if (!mCursor.moveToPosition(position)) | |
return; | |
String name = mCursor.getString(mCursor.getColumnIndex(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME)); | |
int partySize = mCursor.getInt(mCursor.getColumnIndex(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE)); | |
long id = mCursor.getLong(mCursor.getColumnIndex(WaitlistContract.WaitlistEntry._ID)); | |
holder.nameTextView.setText(name); | |
holder.partySizeTextView.setText(String.valueOf(partySize)); | |
holder.itemView.setTag(id); | |
} | |
public void swapCursor(Cursor newCursor) { | |
// COMPLETED (16) Inside, check if the current cursor is not null, and close it if so | |
// Always close the previous mCursor first | |
if (mCursor != null) mCursor.close(); | |
// COMPLETED (17) Update the local mCursor to be equal to newCursor | |
mCursor = newCursor; | |
// COMPLETED (18) Check if the newCursor is not null, and call this.notifyDataSetChanged() if so | |
if (newCursor != null) { | |
// Force the RecyclerView to refresh | |
this.notifyDataSetChanged(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return mCursor.getCount(); | |
} | |
/** | |
* Inner class to hold the views needed to display a single item in the recycler-view | |
*/ | |
class GuestViewHolder extends RecyclerView.ViewHolder { | |
// Will display the guest name | |
TextView nameTextView; | |
// Will display the party size number | |
TextView partySizeTextView; | |
/** | |
* Constructor for our ViewHolder. Within this constructor, we get a reference to our | |
* TextViews | |
* | |
* @param itemView The View that you inflated in | |
* {@link GuestListAdapter#onCreateViewHolder(ViewGroup, int)} | |
*/ | |
public GuestViewHolder(View itemView) { | |
super(itemView); | |
nameTextView = (TextView) itemView.findViewById(R.id.name_text_view); | |
partySizeTextView = (TextView) itemView.findViewById(R.id.party_size_text_view); | |
} | |
} | |
private boolean removeGuest(long id) { | |
// COMPLETED (2) Inside, call mDb.delete to pass in the TABLE_NAME and the condition that WaitlistEntry._ID equals id | |
return mDb.delete(WaitlistContract.WaitlistEntry.TABLE_NAME, WaitlistContract.WaitlistEntry._ID + "=" + id, null) > 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// COMPLETED (3) Create a new ItemTouchHelper with a SimpleCallback that handles both LEFT and RIGHT swipe directions | |
// Create an item touch helper to handle swiping items off the list | |
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { | |
// COMPLETED (4) Override onMove and simply return false inside | |
@Override | |
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | |
//do nothing, we only care about swiping | |
return false; | |
} | |
// COMPLETED (5) Override onSwiped | |
@Override | |
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { | |
// COMPLETED (8) Inside, get the viewHolder's itemView's tag and store in a long variable id | |
//get the id of the item being swiped | |
long id = (long) viewHolder.itemView.getTag(); | |
// COMPLETED (9) call removeGuest and pass through that id | |
//remove from DB | |
removeGuest(id); | |
// COMPLETED (10) call swapCursor on mAdapter passing in getAllGuests() as the argument | |
//update the list | |
mAdapter.swapCursor(getAllGuests()); | |
} | |
//COMPLETED (11) attach the ItemTouchHelper to the waitlistRecyclerView | |
}).attachToRecyclerView(waitlistRecyclerView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment