Last active
January 9, 2017 16:16
-
-
Save shihabmi7/0c9104683b940ecd01a06f48598c7ead to your computer and use it in GitHub Desktop.
Recylar View with Divider
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
recyler_history = (RecyclerView) findViewById(R.id.recyler_history); | |
// recyler_history.setAdapter(myAdapter); | |
recyler_history.setHasFixedSize(true); | |
RecyclerView.LayoutManager mLayoutManager = | |
new LinearLayoutManager(getApplicationContext()); | |
recyler_history.setLayoutManager(mLayoutManager); | |
recyler_history.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); | |
recyler_history.setItemAnimator(new DefaultItemAnimator()); | |
recyler_history.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyler_history, | |
new RecyclerTouchListener.ClickListener() { | |
@Override | |
public void onClick(View view, int position) { | |
// TripHistory movie = tripHistories.get(position); | |
// Toast.makeText(getApplicationContext(), | |
// movie.getTrip_date() + " is selected!", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onLongClick(View view, int position) { | |
/*Toast.makeText(getApplicationContext(), | |
" Long Clicked!", Toast.LENGTH_SHORT).show();*/ | |
} | |
})); | |
myAdapter = new MoviesAdapter(tripHistories); | |
recyler_history.setAdapter(myAdapter); | |
myAdapter.notifyDataSetChanged(); |
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.passenger.ucabs.adapter; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
/** | |
* Created by Shihab on 1/28/2016. | |
*/ | |
public class DividerItemDecoration extends RecyclerView.ItemDecoration { | |
private static final int[] ATTRS = new int[]{ | |
android.R.attr.listDivider | |
}; | |
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; | |
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; | |
private Drawable mDivider; | |
private int mOrientation; | |
public DividerItemDecoration(Context context, int orientation) { | |
final TypedArray a = context.obtainStyledAttributes(ATTRS); | |
mDivider = a.getDrawable(0); | |
a.recycle(); | |
setOrientation(orientation); | |
} | |
public void setOrientation(int orientation) { | |
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { | |
throw new IllegalArgumentException("invalid orientation"); | |
} | |
mOrientation = orientation; | |
} | |
@Override | |
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
if (mOrientation == VERTICAL_LIST) { | |
drawVertical(c, parent); | |
} else { | |
drawHorizontal(c, parent); | |
} | |
} | |
public void drawVertical(Canvas c, RecyclerView parent) { | |
final int left = parent.getPaddingLeft(); | |
final int right = parent.getWidth() - parent.getPaddingRight(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int top = child.getBottom() + params.bottomMargin; | |
final int bottom = top + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
public void drawHorizontal(Canvas c, RecyclerView parent) { | |
final int top = parent.getPaddingTop(); | |
final int bottom = parent.getHeight() - parent.getPaddingBottom(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int left = child.getRight() + params.rightMargin; | |
final int right = left + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
if (mOrientation == VERTICAL_LIST) { | |
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); | |
} else { | |
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 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
package com.passenger.ucabs.adapter; | |
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.TextView; | |
import com.passenger.ucabs.R; | |
import com.passenger.ucabs.models.Movie; | |
import java.util.List; | |
/** | |
* Created by Shihab on 1/28/2016. | |
*/ | |
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> { | |
private List<Movie> moviesList; | |
public class MyViewHolder extends RecyclerView.ViewHolder { | |
public TextView title, year, genre; | |
ImageView cardimage; | |
public MyViewHolder(View view) { | |
super(view); | |
title = (TextView) view.findViewById(R.id.title); | |
genre = (TextView) view.findViewById(R.id.genre); | |
year = (TextView) view.findViewById(R.id.year); | |
} | |
} | |
public MoviesAdapter(List<Movie> moviesList) { | |
this.moviesList = moviesList; | |
} | |
@Override | |
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View itemView = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.movie_list_row, parent, false); | |
return new MyViewHolder(itemView); | |
} | |
@Override | |
public void onBindViewHolder(MyViewHolder holder, int position) { | |
Movie movie = moviesList.get(position); | |
holder.title.setText(movie.getTitle()); | |
// holder.genre.setText(movie.getGenre()); | |
//holder.year.setText(movie.getYear()); | |
} | |
@Override | |
public int getItemCount() { | |
return moviesList.size(); | |
} | |
} |
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.passenger.ucabs.adapter; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
/** | |
* Created by User on 1/9/2017. | |
*/ | |
// | |
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { | |
public interface ClickListener { | |
void onClick(View view, int position); | |
void onLongClick(View view, int position); | |
} | |
private GestureDetector gestureDetector; | |
private ClickListener clickListener; | |
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) { | |
this.clickListener = clickListener; | |
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && clickListener != null) { | |
clickListener.onLongClick(child, recyclerView.getChildPosition(child)); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { | |
View child = rv.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { | |
clickListener.onClick(child, rv.getChildPosition(child)); | |
} | |
return false; | |
} | |
@Override | |
public void onTouchEvent(RecyclerView rv, MotionEvent e) { | |
} | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment