Sunday, May 15, 2016
Saturday, May 7, 2016
Wednesday, May 4, 2016
RecyclerView Loadmore
activity_layout
<?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="match_parent"
android:fitsSystemWindows="true"
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: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>
list_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tvEmailId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvName"
android:layout_margin="5dp"
android:text="Email Id"
android:textColor="@android:color/black"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
progressbar_item
<?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="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" />
</LinearLayout>
OnLoadMoreListener
public interface OnLoadMoreListener {
void onLoadMore();
}
DataAdapter
public class DataAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<Student> studentList;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public DataAdapter(List<Student> students, RecyclerView recyclerView) {
studentList = students;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}
@Override
public int getItemViewType(int position) {
return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_row, parent, false);
vh = new StudentViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.progress_item, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof StudentViewHolder) {
Student singleStudent = (Student) studentList.get(position);
((StudentViewHolder) holder).tvName.setText(singleStudent.getName());
((StudentViewHolder) holder).tvEmailId.setText(singleStudent.getEmailId());
((StudentViewHolder) holder).student = singleStudent;
} else {
if (((ProgressViewHolder) holder).progressBar != null)
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return studentList.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvEmailId;
public Student student;
public StudentViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.tvName);
tvEmailId = (TextView) v.findViewById(R.id.tvEmailId);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"OnClick :" + student.getName() + " \n " + student.getEmailId(),
Toast.LENGTH_SHORT).show();
}
});
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity { private TextView tvEmptyView; private RecyclerView mRecyclerView; private DataAdapter mAdapter; private LinearLayoutManager mLayoutManager; private List<Student> studentList; protected Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvEmptyView = (TextView) findViewById(R.id.empty_view); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); studentList = new ArrayList<Student>(); handler = new Handler(); loadData(); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter = new DataAdapter(studentList, mRecyclerView); mRecyclerView.setAdapter(mAdapter); if (studentList.isEmpty()) { mRecyclerView.setVisibility(View.GONE); tvEmptyView.setVisibility(View.VISIBLE); } else { mRecyclerView.setVisibility(View.VISIBLE); tvEmptyView.setVisibility(View.GONE); } mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { studentList.add(null); mAdapter.notifyItemInserted(studentList.size() - 1); handler.postDelayed(new Runnable() { @Override public void run() { studentList.remove(studentList.size() - 1); mAdapter.notifyItemRemoved(studentList.size()); int start = studentList.size(); int end = start + 20; for (int i = start + 1; i <= end; i++) { studentList.add(new Student("Student " + i, "AndroidStudent" + i + "@gmail.com")); mAdapter.notifyItemInserted(studentList.size()); } mAdapter.setLoaded(); } }, 2000); } }); } private void loadData() { for (int i = 1; i <= 20; i++) { studentList.add(new Student("Student " + i, "androidstudent" + i + "@gmail.com")); } } }
RecyclerView Swipe Item
RecyclerView Swipe to Dismiss
activity_layout
activity_layout
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movie_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_item_movie layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="2dp"> <TextView
android:id="@+id/movie_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="8dp"/> </android.support.v7.widget.CardView>
build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 23
buildToolsVersion "23.0.2" defaultConfig { applicationId "map.conghuy.com.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0" } buildTypes { release { minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:cardview-v7:23.0.0'}
MovieTouchHelper
public class MovieTouchHelper extends ItemTouchHelper.SimpleCallback {
private MovieAdapter mMovieAdapter;
public MovieTouchHelper(MovieAdapter movieAdapter){
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.mMovieAdapter = movieAdapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mMovieAdapter.swap(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mMovieAdapter.remove(viewHolder.getAdapterPosition());
}
}
MovieAdapter
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private Context mContext;
private List<Movie> mMovies;
public MovieAdapter(Context context, List<Movie> movies){
this.mContext = context;
this.mMovies = movies;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_item_movie, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindMovie(mMovies.get(position));
}
@Override
public int getItemCount() {
return mMovies.size();
}
public void remove(int position) {
mMovies.remove(position);
notifyItemRemoved(position);
}
public void swap(int firstPosition, int secondPosition){
Collections.swap(mMovies, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
}
public class ViewHolder extends RecyclerView.ViewHolder{
public final TextView movieNameTextView;
public ViewHolder(View view){
super(view);
movieNameTextView = (TextView) view.findViewById(R.id.movie_name);
}
public void bindMovie(Movie movie){
this.movieNameTextView.setText(movie.getName());
}
}
}
Movie
public class Movie {
private String name;
public Movie(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup RecyclerView
RecyclerView movieRecyclerView = (RecyclerView) findViewById(R.id.movie_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
movieRecyclerView.setLayoutManager(linearLayoutManager);
// Setup Adapter
MovieAdapter movieAdapter = new MovieAdapter(this, getMovies());
movieRecyclerView.setAdapter(movieAdapter);
// Setup ItemTouchHelper
ItemTouchHelper.Callback callback = new MovieTouchHelper(movieAdapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(movieRecyclerView);
}
private List<Movie> getMovies(){
List<Movie> movieList = new ArrayList<>();
movieList.add(new Movie("Harry Potter"));
movieList.add(new Movie("Twilight"));
movieList.add(new Movie("Star Wars"));
movieList.add(new Movie("Star Trek"));
movieList.add(new Movie("Galaxy Quest"));
return movieList;
}
}