build.gradle (Project: MyApplication)
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 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir } |
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 |
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' implementation 'com.baoyz.swipemenulistview:library:1.3.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' } |
ViewHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.phaisarn.myapplication; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class ViewHolder { public ImageView imageView; public TextView textViewName; public TextView textViewNumber; public ViewHolder(View view) { imageView = (ImageView) view.findViewById(R.id.imageView); textViewName = (TextView) view.findViewById(R.id.textView_name); textViewNumber = (TextView) view.findViewById(R.id.textView_num); } } |
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 |
package com.phaisarn.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import java.util.ArrayList; public class CustomAdapter extends ArrayAdapter { private Context mContext; private ArrayList<CustomItem> mItems; public CustomAdapter(Context context, ArrayList<CustomItem> items) { super(context, 0, items); mContext = context; mItems = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vHolder; if(convertView == null) { LayoutInflater inflater = LayoutInflater.from(mContext); convertView = inflater.inflate(R.layout.item_layout, parent, false); vHolder = new ViewHolder(convertView); convertView.setTag(vHolder); } else { vHolder = (ViewHolder)convertView.getTag(); } CustomItem item = mItems.get(position); vHolder.imageView.setImageResource(item.imageId); vHolder.textViewName.setText(item.name); vHolder.textViewNumber.setText(item.num); return convertView; } } |
CustomItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.phaisarn.myapplication; public class CustomItem { public int imageId; public String name; public String num; public CustomItem(int imageId, String name, String num) { this.imageId = imageId; this.name = name; this.num = num; } } |
layout/item_layout.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"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:orientation="horizontal" android:padding="5dp" android:rowCount="2"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_rowSpan="2" /> <TextView android:id="@+id/textView_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_weight="1" android:textColor="#00b" android:textSize="20sp" /> <TextView android:id="@+id/textView_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_weight="1" android:textSize="18sp" /> </GridLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <com.baoyz.swipemenulistview.SwipeMenuListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.widget.AdapterView; import android.widget.Toast; import com.baoyz.swipemenulistview.SwipeMenu; import com.baoyz.swipemenulistview.SwipeMenuCreator; import com.baoyz.swipemenulistview.SwipeMenuItem; import com.baoyz.swipemenulistview.SwipeMenuListView; import java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private SwipeMenuListView mSwipeListView; @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(); } }); fab.hide(); final ArrayList<CustomItem> itemArray = new ArrayList<>(); itemArray.addAll(Arrays.asList( new CustomItem(R.drawable.female1, "มานี", "099888xxxx"), new CustomItem(R.drawable.male1, "มานะ", "089444xxxx"), new CustomItem(R.drawable.male2, "ปิติ", "0881234xxxx"), new CustomItem(R.drawable.female2, "ชูใจ", "086000xxxx"), new CustomItem(R.drawable.male3, "วีระ", "081234xxxx"), new CustomItem(R.drawable.male4, "สมคิด", "087111xxxx"), new CustomItem(R.drawable.female3, "ดวงแก้ว", "090990xxxx"))); CustomAdapter adapter = new CustomAdapter(this, itemArray); mSwipeListView = (SwipeMenuListView) findViewById(R.id.listView); mSwipeListView.setAdapter(adapter); // DIRECTION_LEFT เป็นค่า default อยู่แล้ว (ไม่กำหนดเมธอดนี้ก็ได้) // ถ้าอยากให้ Swipe ไปทางขวา กำหนดเป็น DIRECTION_RIGHT (เมนูจะอยู่ทางซ้าย) mSwipeListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT); createSwipeMenu(); mSwipeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { mSwipeListView.smoothOpenMenu(i); } }); mSwipeListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { String str = "Item: " + itemArray.get(position).num + "\n"; String t = ""; switch(index) { //menu.getMenuItem(index).getTitle(); case 0: t = "Call"; break; case 1: t = "Edit"; break; case 2: t = "Delete"; break; } str += "Menu: " + t; Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show(); return true; } }); } private void createSwipeMenu() { SwipeMenuCreator creator = new SwipeMenuCreator() { @Override public void create(SwipeMenu menu) { SwipeMenuItem menuCall = new SwipeMenuItem(getBaseContext()); menuCall.setBackground(new ColorDrawable(Color.GREEN)); menuCall.setWidth(dp2px(80)); menuCall.setIcon(R.drawable.ic_call); menu.addMenuItem(menuCall); SwipeMenuItem menuEdit = new SwipeMenuItem(getBaseContext()); menuEdit.setBackground(new ColorDrawable(Color.BLUE)); menuEdit.setWidth(dp2px(80)); menuEdit.setIcon(R.drawable.ic_edit); menu.addMenuItem(menuEdit); SwipeMenuItem menuDelete = new SwipeMenuItem(getBaseContext()); menuDelete.setBackground(new ColorDrawable(Color.RED)); menuDelete.setWidth(dp2px(80)); menuDelete.setIcon(R.drawable.ic_delete); menu.addMenuItem(menuDelete); /* create text menu SwipeMenuItem menuText = new SwipeMenuItem(getBaseContext()); menuText.setBackground(new ColorDrawable(Color.DKGRAY)); menuText.setWidth(200); menuText.setTitle("Text"); menuText.setTitleSize(24); menuText.setTitleColor(Color.WHITE); menu.addMenuItem(menuText); */ } }; mSwipeListView.setMenuCreator(creator); } private int dp2px(float dpValue) { final float scale = this.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @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); } } |
รูปที่ใช้