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;
}
}
No comments:
Post a Comment