3 ภาพนี้ใช้ LinearLayoutManager
ลบรายการได้ด้วยการเลื่อนซ้าย-ขวา , ย้ายรายการได้ด้วยการเลื่อนขึ้น-ลง
2 ภาพนี้ใช้ GridLayoutManager
ย้ายรายการได้ แต่ลบรายการไม่ได้
build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.github.nisrulz:recyclerviewhelper:25.1.0' } |
drawable/item_normal.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#aaa" /> <solid android:color="#fff" /> </shape> |
drawable/item_ondrag.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#0ff" /> <solid android:color="#f0f" /> </shape> |
layout/custom_layout_grid.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="150dp" android:layout_height="120dp" android:layout_margin="10dp" android:background="@drawable/item_normal" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="-20dp" android:gravity="center" android:textSize="24sp" /> </LinearLayout> |
layout/custom_layout_linear.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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="100dp" android:layout_marginBottom="8dp" android:background="@drawable/item_normal" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:textSize="24sp" /> </LinearLayout> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <!-- <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email"/> --> </android.support.design.widget.CoordinatorLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#dfdfdf" android:gravity="center_horizontal" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> </android.support.v7.widget.RecyclerView> </FrameLayout> |
CustomAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.phaisarn.myapplication; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.Collections; import github.nisrulz.recyclerviewhelper.RVHAdapter; public class CustomAdapter extends RecyclerView.Adapter<CustomHolder> implements RVHAdapter { private ArrayList<CustomItem> mItems; private Context mContext; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } @Override public int getItemCount() { return mItems.size(); } @Override public CustomHolder onCreateViewHolder(ViewGroup vg, int type) { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.custom_layout_linear, vg, false); //View view = inflater.inflate(R.layout.custom_layout_grid, vg, false); CustomHolder holder = new CustomHolder(view); return holder; } @Override public void onBindViewHolder(CustomHolder vHolder, int position) { CustomItem item = mItems.get(position); vHolder.textView.setText(item.text); } @Override public boolean onItemMove(int fromPosition, int toPosition) { Collections.swap(mItems, fromPosition, toPosition); notifyItemMoved(fromPosition, toPosition); return true; } @Override public void onItemDismiss(int position, int direction) { mItems.remove(position); notifyItemRemoved(position); } } |
บรรทัดที่ 31 : ใช้แบบ LinearLayout
ด้วย custom_layout_linear
บรรทัดที่ 32 : ใช้แบบ GridLayout
ด้วย custom_layout_grid
CustomHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import github.nisrulz.recyclerviewhelper.RVHViewHolder; public class CustomHolder extends RecyclerView.ViewHolder implements RVHViewHolder { public TextView textView; private View mView; public CustomHolder(View view) { super(view); textView = (TextView) view.findViewById(R.id.textView); mView = view; } @Override public void onItemSelected(int actionstate) { mView.setBackgroundResource(R.drawable.item_ondrag); } @Override public void onItemClear() { mView.setBackgroundResource(R.drawable.item_normal); } } |
CustomItem.java
1 2 3 4 5 6 7 8 9 10 |
package com.phaisarn.myapplication; public class CustomItem { public String text; public CustomItem(String text) { this.text = text; } } |
CustomItemTouchHelperCallback.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.phaisarn.myapplication; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import github.nisrulz.recyclerviewhelper.RVHItemTouchHelperCallback; public class CustomItemTouchHelperCallback extends RVHItemTouchHelperCallback { public CustomItemTouchHelperCallback(CustomAdapter adapter, boolean a, boolean b, boolean c) { super(adapter, a, b, c); } @Override public int getMovementFlags(RecyclerView rcv, RecyclerView.ViewHolder holder) { RecyclerView.LayoutManager layout = rcv.getLayoutManager(); int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; if (layout instanceof GridLayoutManager) { dragFlags = dragFlags | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; //swipeFlags = 0; } return makeMovementFlags(dragFlags, swipeFlags); } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); /* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); */ final ArrayList<CustomItem> itemArray = new ArrayList<>(); itemArray.addAll(Arrays.asList( new CustomItem("One"), new CustomItem("Two"), new CustomItem("Three"), new CustomItem("Four"), new CustomItem("Five"), new CustomItem("Six"), new CustomItem("Seven") )); RecyclerView rcv = (RecyclerView) findViewById(R.id.recyclerView); CustomAdapter adapter = new CustomAdapter(this, itemArray); rcv.setAdapter(adapter); LinearLayoutManager layout = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); //GridLayoutManager layout = new GridLayoutManager(this, 2); rcv.setLayoutManager(layout); if (layout instanceof GridLayoutManager) { rcv.getLayoutParams().width = RecyclerView.LayoutParams.WRAP_CONTENT; } //RVHItemTouchHelperCallback callback = new RVHItemTouchHelperCallback(adapter, true, true, true); CustomItemTouchHelperCallback callback = new CustomItemTouchHelperCallback(adapter, true, true, true); ItemTouchHelper helper = new ItemTouchHelper(callback); helper.attachToRecyclerView(rcv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
บรรทัดที่ 47 : ใช้ LinearLayoutManager
บรรทัดที่ 48 : ใช้ GridLayoutManager