The application I am creating has an extensible list view in which I have added a scroll option for editing and deleting. I added this option, but when I launch the application, it does not display the swipe option, even if I have tried with a different kind of list, but it works fine there. I used the swipe option inside the base adapter, can someone tell me why the scroll function does not work in the expanded 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 with 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(); }
}