Skip to content

Instantly share code, notes, and snippets.

@jayhack7
Created September 27, 2016 19:08
Show Gist options
  • Save jayhack7/a8a96a5c5bb7713dfb6bd1e22c80d97f to your computer and use it in GitHub Desktop.
Save jayhack7/a8a96a5c5bb7713dfb6bd1e22c80d97f to your computer and use it in GitHub Desktop.
mport android.content.Context;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.slifers.dailyfreethrowtracker.R;
import com.slifers.dailyfreethrowtracker.model.ShotsModel;
import com.tr4android.recyclerviewslideitem.SwipeAdapter;
import com.tr4android.recyclerviewslideitem.SwipeConfiguration;
import java.util.ArrayList;
import java.util.List;
/**
* Created by user on 9/29/2016.
*/
public class SampleAdapter extends SwipeAdapter implements View.OnClickListener {
ArrayList<String> mDataset;
public static final int TIME_POST_DELAYED = 3000; // in ms
int[] colors = new int[]{R.color.color_red, R.color.color_pink, R.color.color_purple, R.color.color_deep_purple, R.color.color_indigo, R.color.color_blue, R.color.color_light_blue, R.color.color_cyan, R.color.color_teal, R.color.color_green, R.color.color_light_green, R.color.color_lime, R.color.color_yellow, R.color.color_amber, R.color.color_orange, R.color.color_deep_orange, R.color.color_brown, R.color.color_grey, R.color.color_blue_grey};
Context context;
List<ShotsModel> shots;
private RecyclerView mRecyclerView;
long initialCount;
public SampleAdapter(Context context, List<ShotsModel> shots, RecyclerView recyclerView) {
this.context = context;
this.shots = shots;
mRecyclerView = recyclerView;
}
public class vh_shots extends RecyclerView.ViewHolder {
TextView tv_makes, tv_dash, tv_attempts, tv_percent, tv_percent_dash, tv_date;
ImageView ic_makes, ic_percent, ic_calendar;
LinearLayout contentView;
View avatarView;
public vh_shots(View itemView) {
super(itemView);
contentView = (LinearLayout) itemView.findViewById(R.id.container);
tv_makes = (TextView) itemView.findViewById(R.id.tv_makes);
tv_dash = (TextView) itemView.findViewById(R.id.tv_dash);
tv_attempts = (TextView) itemView.findViewById(R.id.tv_attempts);
tv_percent = (TextView) itemView.findViewById(R.id.tv_percent);
tv_percent_dash = (TextView) itemView.findViewById(R.id.tv_percent_dash);
tv_date = (TextView) itemView.findViewById(R.id.tv_date);
ic_makes = (ImageView) itemView.findViewById(R.id.ic_makes);
ic_percent = (ImageView) itemView.findViewById(R.id.ic_percent);
ic_calendar = (ImageView) itemView.findViewById(R.id.ic_calendar);
}
}
@Override
public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.lv_custom_row, parent, true);
return new vh_shots(v);
}
@Override
public void onBindSwipeViewHolder(RecyclerView.ViewHolder vh_shots, int i) {
vh_shots sampleViewHolder = (vh_shots) vh_shots;
sampleViewHolder.tv_makes.setText("" + shots.get(i).getMakes());
sampleViewHolder.tv_attempts.setText("" + shots.get(i).getAttempts());
sampleViewHolder.tv_percent.setText("" + shots.get(i).getPercentage());
sampleViewHolder.tv_date.setText("" + shots.get(i).getDate());
}
@Override
public SwipeConfiguration onCreateSwipeConfiguration(Context context, int position) {
return new SwipeConfiguration.Builder(context)
.setLeftBackgroundColorResource(R.color.color_delete)
.setRightBackgroundColorResource(R.color.color_delete)
.setDrawableResource(R.drawable.ic_delete_white_24dp)
.setRightDrawableResource(R.drawable.ic_delete_white_24dp)
.setLeftUndoable(true)
.setLeftUndoDescription(R.string.action_deleted)
.setRightUndoable(true)
.setRightUndoDescription(R.string.action_deleted)
.setDescriptionTextColorResource(android.R.color.white)
.setLeftSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.NORMAL_SWIPE)
.setRightSwipeBehaviour(SwipeConfiguration.SwipeBehaviour.NORMAL_SWIPE)
.build();
}
@Override
public void onSwipe(int position, int direction) {
if (direction == SWIPE_LEFT) {
deleteItems(position, direction);
} else {
deleteItems(position, direction);
}
}
@Override
public void onClick(View view) {
// We need to get the parent of the parent to actually have the proper view
int position = mRecyclerView.getChildAdapterPosition((View) view.getParent().getParent());
Toast toast = Toast.makeText(context, "Clicked item at position " + position, Toast.LENGTH_SHORT);
toast.show();
}
@Override
public int getItemCount() {
return shots.size();
}
public void deleteItems (int position, int direction){
try{
final ShotsModel shot = shots.remove(position);
//shots.remove(position);
shot.delete();
notifyItemRemoved(position);
notifyDataSetChanged();
} catch (IndexOutOfBoundsException e){
notifyDataSetChanged();
e.printStackTrace();
}
Toast toast = Toast.makeText(context, "Deleted item at position " + position, Toast.LENGTH_SHORT);
toast.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment