The application which i am creating has an expandable list view in which i have added the option of swipe to edit and delete.I have added those option but when i run the application it doesn't shows the swipe option even i have tried with another list view but there it works fine. I have used the swipe option inside the base adapter can anyone tell me why the swipe functionality doesn't works in expandable list view.
Expandablelistview:
public class ExpandableHeightListView extends ListView {
private static final int TOUCH_STATE_NONE = 0;
private static final int TOUCH_STATE_X = 1;
private static final int TOUCH_STATE_Y = 2;
public static final int DIRECTION_LEFT = 1;
public static final int DIRECTION_RIGHT = -1;
private int mDirection = 1;//swipe from right to left by default
private int MAX_Y = 5;
private int MAX_X = 3;
private float mDownX;
private float mDownY;
private int mTouchState;
private int mTouchPosition;
private DaybookSwipeMenuLayout mTouchView;
private OnSwipeListener mOnSwipeListener;
private DaybookSwipeMenuCreator mMenuCreator;
private OnMenuItemClickListener mOnMenuItemClickListener;
private OnMenuStateChangeListener mOnMenuStateChangeListener;
private Interpolator mCloseInterpolator;
private Interpolator mOpenInterpolator;
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
init();
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ExpandableHeightListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
MAX_X = dp2px(MAX_X);
MAX_Y = dp2px(MAX_Y);
mTouchState = TOUCH_STATE_NONE;
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(new DaybookSwipeMenuAdapter(getContext(), adapter) {
@Override
public void createMenu(DaybookSwipeMenu menu) {
if (mMenuCreator != null) {
mMenuCreator.create(menu);
}
}
@Override
public void onItemClick(DaybookSwipeMenuView view, DaybookSwipeMenu menu,
int index) {
boolean flag = false;
if (mOnMenuItemClickListener != null) {
flag = mOnMenuItemClickListener.onMenuItemClick(
view.getPosition(), menu, index);
}
if (mTouchView != null && !flag) {
mTouchView.smoothCloseMenu();
}
}
});
}
public void setCloseInterpolator(Interpolator interpolator) {
mCloseInterpolator = interpolator;
}
public void setOpenInterpolator(Interpolator interpolator) {
mOpenInterpolator = interpolator;
}
public Interpolator getOpenInterpolator() {
return mOpenInterpolator;
}
public Interpolator getCloseInterpolator() {
return mCloseInterpolator;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//在拦截处处理,在滑动设置了点击事件的地方也能swip,点击时又不能影响原来的点击事件
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mDownX = ev.getX();
mDownY = ev.getY();
boolean handled = super.onInterceptTouchEvent(ev);
mTouchState = TOUCH_STATE_NONE;
mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
View view = getChildAt(mTouchPosition - getFirstVisiblePosition());
//只在空的时候赋值 以免每次触摸都赋值,会有多个open状态
if (view instanceof DaybookSwipeMenuLayout) {
//如果有打开了 就拦截.
if (mTouchView != null && mTouchView.isOpen() && !inRangeOfView(mTouchView.getMenuView(), ev)) {
return true;
}
mTouchView = (DaybookSwipeMenuLayout) view;
mTouchView.setSwipeDirection(mDirection);
}
//如果摸在另外个view
if (mTouchView != null && mTouchView.isOpen() && view != mTouchView) {
handled = true;
}
if (mTouchView != null) {
mTouchView.onSwipe(ev);
}
return handled;
case MotionEvent.ACTION_MOVE:
float dy = Math.abs((ev.getY() - mDownY));
float dx = Math.abs((ev.getX() - mDownX));
if (Math.abs(dy) > MAX_Y || Math.abs(dx) > MAX_X) {
//每次拦截的down都把触摸状态设置成了TOUCH_STATE_NONE 只有返回true才会走onTouchEvent 所以写在这里就够了
if (mTouchState == TOUCH_STATE_NONE) {
if (Math.abs(dy) > MAX_Y) {
mTouchState = TOUCH_STATE_Y;
} else if (dx > MAX_X) {
mTouchState = TOUCH_STATE_X;
if (mOnSwipeListener != null) {
mOnSwipeListener.onSwipeStart(mTouchPosition);
}
}
}
return true;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null)
return super.onTouchEvent(ev);
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
int oldPos = mTouchPosition;
mDownX = ev.getX();
mDownY = ev.getY();
mTouchState = TOUCH_STATE_NONE;
mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
if (mTouchPosition == oldPos && mTouchView != null
&& mTouchView.isOpen()) {
mTouchState = TOUCH_STATE_X;
mTouchView.onSwipe(ev);
return true;
}
View view = getChildAt(mTouchPosition - getFirstVisiblePosition());
if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
mTouchView = null;
// return super.onTouchEvent(ev);
// try to cancel the touch event
MotionEvent cancelEvent = MotionEvent.obtain(ev);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
onTouchEvent(cancelEvent);
if (mOnMenuStateChangeListener != null) {
mOnMenuStateChangeListener.onMenuClose(oldPos);
}
return true;
}
if (view instanceof DaybookSwipeMenuLayout) {
mTouchView = (DaybookSwipeMenuLayout) view;
mTouchView.setSwipeDirection(mDirection);
}
if (mTouchView != null) {
mTouchView.onSwipe(ev);
}
break;
case MotionEvent.ACTION_MOVE:
//有些可能有header,要减去header再判断
mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()) - getHeaderViewsCount();
//如果滑动了一下没完全展现,就收回去,这时候mTouchView已经赋值,再滑动另外一个不可以swip的view
//会导致mTouchView swip 。 所以要用位置判断是否滑动的是一个view
if (!mTouchView.getSwipEnable() || mTouchPosition != mTouchView.getPosition()) {
break;
}
float dy = Math.abs((ev.getY() - mDownY));
float dx = Math.abs((ev.getX() - mDownX));
if (mTouchState == TOUCH_STATE_X) {
if (mTouchView != null) {
mTouchView.onSwipe(ev);
}
getSelector().setState(new int[]{0});
ev.setAction(MotionEvent.ACTION_CANCEL);
super.onTouchEvent(ev);
return true;
} else if (mTouchState == TOUCH_STATE_NONE) {
if (Math.abs(dy) > MAX_Y) {
mTouchState = TOUCH_STATE_Y;
} else if (dx > MAX_X) {
mTouchState = TOUCH_STATE_X;
if (mOnSwipeListener != null) {
mOnSwipeListener.onSwipeStart(mTouchPosition);
}
}
}
break;
case MotionEvent.ACTION_UP:
if (mTouchState == TOUCH_STATE_X) {
if (mTouchView != null) {
boolean isBeforeOpen = mTouchView.isOpen();
mTouchView.onSwipe(ev);
boolean isAfterOpen = mTouchView.isOpen();
if (isBeforeOpen != isAfterOpen && mOnMenuStateChangeListener != null) {
if (isAfterOpen) {
mOnMenuStateChangeListener.onMenuOpen(mTouchPosition);
} else {
mOnMenuStateChangeListener.onMenuClose(mTouchPosition);
}
}
if (!isAfterOpen) {
mTouchPosition = -1;
mTouchView = null;
}
}
if (mOnSwipeListener != null) {
mOnSwipeListener.onSwipeEnd(mTouchPosition);
}
ev.setAction(MotionEvent.ACTION_CANCEL);
super.onTouchEvent(ev);
return true;
}
break;
}
return super.onTouchEvent(ev);
}
public void smoothOpenMenu(int position) {
if (position >= getFirstVisiblePosition()
&& position <= getLastVisiblePosition()) {
View view = getChildAt(position - getFirstVisiblePosition());
if (view instanceof DaybookSwipeMenuLayout) {
mTouchPosition = position;
if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
}
mTouchView = (DaybookSwipeMenuLayout) view;
mTouchView.setSwipeDirection(mDirection);
mTouchView.smoothOpenMenu();
}
}
}
public void smoothCloseMenu(){
if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
}
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getContext().getResources().getDisplayMetrics());
}
public void setMenuCreator(DaybookSwipeMenuCreator menuCreator) {
this.mMenuCreator = menuCreator;
}
public void setOnMenuItemClickListener(
OnMenuItemClickListener onMenuItemClickListener) {
this.mOnMenuItemClickListener = onMenuItemClickListener;
}
public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
this.mOnSwipeListener = onSwipeListener;
}
public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeListener) {
mOnMenuStateChangeListener = onMenuStateChangeListener;
}
public static interface OnMenuItemClickListener {
boolean onMenuItemClick(int position, DaybookSwipeMenu menu, int index);
}
public static interface OnSwipeListener {
void onSwipeStart(int position);
void onSwipeEnd(int position);
}
public static interface OnMenuStateChangeListener {
void onMenuOpen(int position);
void onMenuClose(int position);
}
public void setSwipeDirection(int direction) {
mDirection = direction;
}
/**
* 判断点击事件是否在某个view内
*
* @param view
* @param ev
* @return
*/
public static boolean inRangeOfView(View view, MotionEvent ev) {
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
if (ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())) {
return false;
}
return true;
}
BaseAdapter class having the swipe list option:
public class Daybook_adapter extends BaseAdapter {
Context context;
ArrayList<Daybook> entriesdaybook;
ArrayList<Daybooklist> daybooklists;
Daybooklist_adapter adapter;
DatabaseHandler databaseHandler;
LinearLayout emptyy;
double totalamountin=0.0;
ExpandableHeightListView daybookdetailviewlist;
public Daybook_adapter(Context context, ArrayList<Daybook> list) {
this.context = context;
entriesdaybook = list;
}
@Override
public int getCount() {
return entriesdaybook.size();
}
@Override
public Object getItem(int position) {
return entriesdaybook.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.model_daybook, null);
}
Daybook m = entriesdaybook.get(position);
final TextView tv_date = (TextView) convertView.findViewById(R.id.tv_daybook_date);
final TextView tv_cashin = (TextView) convertView.findViewById(R.id.tv_daybook_cashin);
final TextView tv_cashout = (TextView) convertView.findViewById(R.id.tv_daybook_cashout);
final TextView tv_totalamt = (TextView)convertView.findViewById(R.id.daybook_total_amt);
//final String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String s = m.getDate();
emptyy = (LinearLayout) convertView.findViewById(R.id.empty);
daybookdetailviewlist = (ExpandableHeightListView) convertView.findViewById(R.id.detaillist_daybook);
String[] spiliter = s.split("-");
String year = spiliter[0];
String month = spiliter[1];
String date = spiliter[2];
if (month.startsWith("01")) {
tv_date.setText(date + "Jan" + year);
} else if (month.startsWith("02")) {
tv_date.setText(date + "Feb" + year);
} else if (month.startsWith("03")) {
tv_date.setText(date + "Mar" + year);
} else if (month.startsWith("04")) {
tv_date.setText(date + "Apr" + year);
} else if (month.startsWith("05")) {
tv_date.setText(date + "May" + year);
} else if (month.startsWith("06")) {
tv_date.setText(date + "Jun" + year);
} else if (month.startsWith("07")) {
tv_date.setText(date + "Jul" + year);
} else if (month.startsWith("08")) {
tv_date.setText(date + "Aug" + year);
} else if (month.startsWith("09")) {
tv_date.setText(date + "Sep" + year);
} else if (month.startsWith("10")) {
tv_date.setText(date + "Oct" + year);
} else if (month.startsWith("11")) {
tv_date.setText(date + "Nov" + year);
} else if (month.startsWith("12")) {
tv_date.setText(date + "Dec" + year);
}
/* if (m.getUsertype().startsWith("singleworker")) {
tv_cashin.setText("\u20B9" + "0");
} else if (m.getUsertype().startsWith("groupworker")) {
tv_cashin.setText("\u20B9" + "0");
}*/
tv_cashin.setText("\u20B9" + m.getCashin());
tv_cashout.setText("\u20B9" + m.getCashout());
double one = Double.parseDouble(m.getCashin());
double two = Double.parseDouble(m.getCashout());
double three = one+two;
tv_totalamt.setText("\u20B9" +String.valueOf(three) );
databaseHandler = new DatabaseHandler(context);
daybooklists = databaseHandler.getAllDaywisedaybookdetails(s);
adapter = new Daybooklist_adapter((Activity) context, daybooklists);
if (adapter != null) {
if (adapter.getCount() > 0) {
emptyy.setVisibility(View.GONE);
daybookdetailviewlist.setAdapter(adapter);
DaybookSwipeMenuCreator creator = new DaybookSwipeMenuCreator() {
@Override
public void create(DaybookSwipeMenu menu) {
// create "open" item
DaybookSwipeMenuItem openItem = new DaybookSwipeMenuItem(
(Activity) context);
// set item background
openItem.setBackground(R.color.colorPrimary);
// set item width
openItem.setWidth(dp2px(90));
// set item title
openItem.setIcon(context.getResources().getDrawable(R.drawable.pencil));
openItem.setTitle("Edit");
// set item title fontsize
openItem.setTitleSize(15);
// set item title font color
openItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
DaybookSwipeMenuItem deleteItem = new DaybookSwipeMenuItem(
(Activity) context);
// set item background
deleteItem.setBackground(R.color.colorPrimaryDark);
// set item width
deleteItem.setWidth(dp2px(90));
// set a icon
deleteItem.setIcon(context.getResources().getDrawable(R.drawable.delete));
deleteItem.setTitle("Delete");
deleteItem.setTitleSize(15);
deleteItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(deleteItem);
}
};
daybookdetailviewlist.setMenuCreator(creator);
final ArrayList<Daybooklist> finalListet = daybooklists;
daybookdetailviewlist.setOnMenuItemClickListener(new ExpandableHeightListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final int position, DaybookSwipeMenu menu, int index) {
// Coconuttype item = finalListet.get(position);
// final String farmername = finalListet.get(position).getFarmername();
// final String farmermobno = finalListet.get(position).getFarmermobno();
// final String farmerlocation = finalListet.get(position).getFarmerlocation();
switch (index) {
case 0:
// open
// open(item);
// String farmername = finalListet.get(position).getFarmername();
// String farmermobno = finalListet.get(position).getFarmermobno();
// String farmerlocation =finalListet.get(position).getFarmerlocation();
Log.e("Editclicked", "edt");
// Log.e("farmername", farmername);
// Log.e("farmermobno", farmermobno);
// Log.e("farmerlocation", farmerlocation);
/* Intent k = new Intent(context, FarmerEdit_Activity.class);
k.putExtra("farmername", farmername);
k.putExtra("farmermobno", farmermobno);
k.putExtra("farmerlocation", farmerlocation);
context.startActivity(k);*/
break;
case 1:
// delete
//delete(item);
Log.e("Deleteclicked", "del");
AlertDialog.Builder alert = new AlertDialog.Builder(
context);
alert.setTitle("Delete");
alert.setMessage("Are you sure to delete ");
alert.setPositiveButton(context.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do your work here
finalListet.remove(position);
//databaseHandlerOtherChgs.deleteCoconut(item.getCoconuttype());
// Log.e("farmername", farmername);
// Log.e("farmermobno", farmermobno);
// Log.e("farmerlocation", farmerlocation);
/* databasehandler.deletefarmercontacts(farmername, farmermobno);
databasehandler.deletedaybookdetails(farmername, farmermobno);
databasehandler.deletefarmeradvance(farmername, farmermobno);
databasehandler.deletefarmerbillbookdetails(farmername, farmermobno);
databasehandler.deletefarmertradedetails(farmername, farmermobno);
fadapter.notifyDataSetChanged();
Intent k = getIntent();
finish();
startActivity(k);*/
}
});
alert.setNegativeButton(context.getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
/* finalListet.remove(position);
databaseHandlerOtherChgs.deleteCoconut(item.getCoconuttype());
adapter.notifyDataSetChanged();
Intent k = getIntent();
finish();
startActivity(k);*/
break;
}
return false;
}
});
// set SwipeListener
daybookdetailviewlist.setOnSwipeListener(new ExpandableHeightListView.OnSwipeListener() {
@Override
public void onSwipeStart(int position) {
// swipe start
}
@Override
public void onSwipeEnd(int position) {
// swipe end
}
});
// set MenuStateChangeListener
daybookdetailviewlist.setOnMenuStateChangeListener(new ExpandableHeightListView.OnMenuStateChangeListener() {
@Override
public void onMenuOpen(int position) {
}
@Override
public void onMenuClose(int position) {
}
});
}
}else {
daybookdetailviewlist.setEmptyView(emptyy);
}
daybookdetailviewlist.setExpanded(true);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//todo disable the comments for farmer individual transaction
}
});
return convertView;
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
}
public void setTransactionList(ArrayList<Daybook> newList) {
entriesdaybook = newList;
notifyDataSetChanged();
}
}
I m posting code here , i hope it helps u
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:focusable="false"
android:text="Click this:notify data set changed and re-open the opened menu" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn">
<com.allen.expandablelistview.SwipeMenuExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_below="@id/btn"
android:groupIndicator="@null" />
</FrameLayout>
</RelativeLayout>
MainActivity.java :
public class MainActivity extends AppCompatActivity {
private List<ApplicationInfo> mAppList;
private AppAdapter mAdapter;
private SwipeMenuExpandableListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppList = getPackageManager().getInstalledApplications(0);
findViewById(R.id.btn).setOnClickListener(l);
// 1. Create View
listView = (SwipeMenuExpandableListView) findViewById(R.id.listView);
if (getIntent().getBooleanExtra("stick_mode", false)) {
// 2. Set where does the menu item stick with
/**
* STICK_TO_ITEM_RIGHT_SIDE: Stick with item right side, when swipe,
* it moves from outside of screen .
**/
/**
* STICK_TO_SCREEN: stick with the screen, it was covered and don't
* move ,item moves then menu show.
**/
listView.setmMenuStickTo(SwipeMenuListView.STICK_TO_SCREEN);
}
// 3. Create Adapter and set. It controls data,
// item view,clickable ,swipable ...
mAdapter = new AppAdapter();
listView.setAdapter(mAdapter);
// 4. Set OnItemLongClickListener
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
int itemType = ExpandableListView.getPackedPositionType(id);
int childPosition, groupPosition;
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
childPosition = ExpandableListView.getPackedPositionChild(id);
groupPosition = ExpandableListView.getPackedPositionGroup(id);
// do your per-item callback here
Toast.makeText(MainActivity.this,
"long click on group " + groupPosition + " child " + childPosition, Toast.LENGTH_SHORT)
.show();
return true; // true if we consumed the click, false if not
} else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
groupPosition = ExpandableListView.getPackedPositionGroup(id);
// do your per-group callback here
Toast.makeText(MainActivity.this, "long click on group " + groupPosition,
Toast.LENGTH_SHORT).show();
return true; // true if we consumed the click, false if not
} else {
// null item; we don't consume the click
return false;
}
}
});
// 5.Set OnGroupClickListener
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(MainActivity.this, "group " + groupPosition + " clicked", Toast.LENGTH_SHORT)
.show();
return false;
}
});
// 6.Set OnChildClickListener
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this,
"group " + groupPosition + " child " + childPosition + " clicked", Toast.LENGTH_SHORT).show();
return false;
}
});
// 7. Create and set a SwipeMenuExpandableCreator
// define the group and child creator function
SwipeMenuExpandableCreator creator = new SwipeMenuExpandableCreator() {
@Override
public void createGroup(SwipeMenu menu) {
// Create different menus depending on the view type
switch (menu.getViewType()) {
case 0:
createMenu1(menu);
break;
case 1:
createMenu2(menu);
break;
case 2:
createMenu3(menu);
break;
}
}
@Override
public void createChild(SwipeMenu menu) {
// Create different menus depending on the view type
switch (menu.getViewType()) {
case 0:
createMenu1(menu);
break;
case 1:
createMenu2(menu);
break;
case 2:
createMenu3(menu);
break;
}
}
private void createMenu1(SwipeMenu menu) {
SwipeMenuItem item1 = new SwipeMenuItem(getApplicationContext());
item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0x18, 0x5E)));
item1.setWidth(dp2px(90));
item1.setIcon(R.drawable.ic_action_favorite);
menu.addMenuItem(item1);
SwipeMenuItem item2 = new SwipeMenuItem(getApplicationContext());
item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
item2.setWidth(dp2px(90));
item2.setIcon(R.drawable.ic_action_good);
menu.addMenuItem(item2);
}
private void createMenu2(SwipeMenu menu) {
SwipeMenuItem item1 = new SwipeMenuItem(getApplicationContext());
item1.setBackground(new ColorDrawable(Color.rgb(0xE5, 0xE0, 0x3F)));
item1.setWidth(dp2px(90));
item1.setIcon(R.drawable.ic_action_important);
menu.addMenuItem(item1);
SwipeMenuItem item2 = new SwipeMenuItem(getApplicationContext());
item2.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
item2.setWidth(dp2px(90));
item2.setIcon(R.drawable.ic_action_discard);
menu.addMenuItem(item2);
}
private void createMenu3(SwipeMenu menu) {
SwipeMenuItem item1 = new SwipeMenuItem(getApplicationContext());
item1.setBackground(new ColorDrawable(Color.rgb(0x30, 0xB1, 0xF5)));
item1.setWidth(dp2px(90));
item1.setIcon(R.drawable.ic_action_about);
menu.addMenuItem(item1);
SwipeMenuItem item2 = new SwipeMenuItem(getApplicationContext());
item2.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
item2.setWidth(dp2px(90));
item2.setIcon(R.drawable.ic_action_share);
menu.addMenuItem(item2);
}
};
listView.setMenuCreator(creator);
// 8. Set OnMenuItemClickListener
listView.setOnMenuItemClickListener(new SwipeMenuExpandableListView.OnMenuItemClickListenerForExpandable() {
@Override
public boolean onMenuItemClick(int groupPostion, int childPostion, SwipeMenu menu, int index) {
ApplicationInfo item = mAppList.get(groupPostion);
// childPostion == -1991 means it was the group's swipe menu was
// clicked
String s = "Group " + groupPostion;
if (childPostion != SwipeMenuExpandableListAdapter.GROUP_INDEX) {
s += " , child " + childPostion;
}
s += " , menu index:" + index + " was clicked";
switch (index) {
case 0:
// open
break;
case 1:
// delete
// delete(item);
mAppList.remove(groupPostion);
mAdapter.notifyDataSetChanged();
break;
}
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
return false;
}
});
}
public void btnClick(View view) {
}
/**
* Basically, it's the same as BaseExpandableListAdapter. But added controls
* to every item's swipability
*
* @author yuchentang
* @see android.widget.BaseExpandableListAdapter
*/
class AppAdapter extends BaseSwipeMenuExpandableListAdapter {
/**
* Whether this group item swipable
*
* @param groupPosition
* @return
* @see com.allen.expandablelistview.BaseSwipeMenuExpandableListAdapter#isGroupSwipable(int)
*/
@Override
public boolean isGroupSwipable(int groupPosition) {
return true;
}
/**
* Whether this child item swipable
*
* @param groupPosition
* @param childPosition
* @return
* @see com.allen.expandablelistview.BaseSwipeMenuExpandableListAdapter#isChildSwipable(int,
* int)
*/
@Override
public boolean isChildSwipable(int groupPosition, int childPosition) {
if (childPosition == 0)
return false;
return true;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return childPosition % 3;
}
@Override
public int getChildTypeCount() {
return 3;
}
@Override
public int getGroupType(int groupPosition) {
return groupPosition % 3;
}
@Override
public int getGroupTypeCount() {
return 3;
}
class ViewHolder {
ImageView iv_icon;
TextView tv_name;
public ViewHolder(View view) {
iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
tv_name = (TextView) view.findViewById(R.id.tv_name);
view.setTag(this);
}
}
@Override
public int getGroupCount() {
return mAppList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 3;
}
@Override
public Object getGroup(int groupPosition) {
return mAppList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return "The " + childPosition + "'th child in " + groupPosition + "'th group.";
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public ContentViewWrapper getGroupViewAndReUsable(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
boolean reUseable = true;
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.item_list_app, null);
convertView.setTag(new ViewHolder(convertView));
reUseable = false;
}
ViewHolder holder = (ViewHolder) convertView.getTag();
ApplicationInfo item = (ApplicationInfo) getGroup(groupPosition);
holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));
holder.tv_name.setText(item.loadLabel(getPackageManager()));
return new ContentViewWrapper(convertView, reUseable);
}
@Override
public ContentViewWrapper getChildViewAndReUsable(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
boolean reUseable = true;
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.item_list_app, null);
convertView.setTag(new ViewHolder(convertView));
reUseable = false;
}
ViewHolder holder = (ViewHolder) convertView.getTag();
if (null == holder) {
holder = new ViewHolder(convertView);
}
ApplicationInfo item = (ApplicationInfo) getGroup(groupPosition);
convertView.setBackgroundColor(Color.GRAY);
holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager()));
holder.tv_name.setText("this is child");
return new ContentViewWrapper(convertView, reUseable);
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mAdapter.notifyDataSetChanged(true);
}
};
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
}
Hereby, i am attaching screenshot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With