Swipe to Delete Not working from the Top item of the listview but from the last item of list view

只愿长相守 提交于 2019-12-10 11:50:58

问题


I am Using the this as swipe to delete. but I am having problem in implementing only the delete menu item of the list.

What I am doing is creating just one single delete Item. I really do not want to use any other item such like Open as shown in demo.

here is my design

   <?xml version="1.0" encoding="utf-8"?>

<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"
    android:background="#dadada">


    <RelativeLayout                 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/segmented_buttons">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="false"
                android:orientation="vertical">


                <!--<ScrollView-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent"-->
                <!--android:layout_gravity="center"-->
                <!--android:gravity="center"-->
                <!--android:clickable="false">-->
                <TextView
                    android:id="@+id/swipeRefreshLayout_emptyView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center|center_vertical"
                    android:enabled="false"
                    android:gravity="center"
                    android:visibility="gone" />


                <com.baoyz.swipemenulistview.SwipeMenuListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:clipChildren="false"
                    android:divider="@android:color/transparent"

                    android:dividerHeight="5.0sp" />

                <!--</ScrollView>-->
            </FrameLayout>


        </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>


</RelativeLayout>

and here is How I Am creating my menu in my Fragment

SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
              // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(
                    getActivity());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                    0x3F, 0x25)));
            // set item width
            deleteItem.setWidth(dp2px(90));
            // set a icon
            deleteItem.setIcon(R.drawable.ic_delete);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };

here is how I am setting creator

// set creator
    mListView.setMenuCreator(creator);

My Problem I have couple of problems regarding to the delete, as mentioned

  1. When I have too many data in the list , say that the number of rows are 10 and need to scroll down , the delete option work like a charm even from the first row of the listview.
  2. When there are lesser item in the list or if I continue to delete items from the bottom I mean the last row , when there are less item which do not need to scroll down, does not delete smoothly , it takes many times to refresh list , or open and close again the menu right to left , takes many touch down on delete menu and then it delete some how after too many tries.
  3. I do not know what is problem behind , but When I made the two menu , like Open and Delete as shown in the demo , then I made both open and delete as delete button , so in Case 0 , and Case 1 , I wrote code of delete , then I noticed that the deleting is working every time when I touch on open , but again when I touch on delete it some times deletes the item or some time it takes to many tries.

What could be problem ? Any guess ? Is there any work around ?

And Yeah On thing for sure , When I touch the delete menu it gives me a Log , look down below, It show me this in log , when I Touch to delete and delete is in fact not working , but when it does start working this message in log never shows up.

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

Edit One

Here is my whole code of adapter :

    public class SwipeListOfferAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private ArrayList<OfferStatusData> offerList;
    OfferStatusData offerStatusData;


    public SwipeListOfferAdapter(Activity activity, List<OfferStatusData> offerList) {
        this.activity = activity;
        this.offerList = (ArrayList)offerList;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
    }

    @Override
    public int getCount() {
        return offerList.size();
    }

    @Override
    public Object getItem(int location) {
        return offerList.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_offer_status, null);

            holder = new ViewHolder();
         //   holder.tvCustomerName = (TextView) convertView.findViewById(R.id.tv_row_customer_name);
            holder.tvOfferName = (TextView) convertView.findViewById(R.id.row_offer_name);
           // holder.tvEmail = (TextView) convertView.findViewById(R.id.row_email);
            holder.tvPhoneNumber = (TextView) convertView.findViewById(R.id.row_phone_number);
            holder.tvAddress = (TextView) convertView.findViewById(R.id.row_address);
            holder.tvTmcBrand = (TextView) convertView.findViewById(R.id.row_tmc_brand);
            holder.tvStartEndTime = (TextView) convertView.findViewById(R.id.row_offer_time);
            holder.tvNoOfPerson = (TextView) convertView.findViewById(R.id.tv_row_total_person);
            holder.tvNoOfAcceptors = (TextView) convertView.findViewById(R.id.tv_row_total_acceptors);
            holder.tvMonth = (TextView) convertView.findViewById(R.id.tv_row_month);
            holder.tvDay = (TextView) convertView.findViewById(R.id.tv_row_day);
           // holder.tvCustomerName = (TextView) convertView.findViewById(R.id.tv_row_customer_name);

            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(offerList.size()>0){

            offerStatusData = (OfferStatusData)offerList.get(position);
           // holder.tvCustomerName.setText(offerStatusData.getName());
            holder.tvOfferName.setText(offerStatusData.getOffer_name());
           // holder.tvEmail.setText(offerStatusData.getEmail());
            holder.tvPhoneNumber.setText(offerStatusData.getPhone_number());
            holder.tvAddress.setText(offerStatusData.getAddress());
            holder.tvTmcBrand.setText(offerStatusData.getTmc_brand());
            holder.tvStartEndTime.setText(offerStatusData.getStart_time()+"-"+offerStatusData.getEnd_time());
            holder.tvNoOfPerson.setText(offerStatusData.getNo_of_person());
            holder.tvNoOfPerson.setTextColor(Color.parseColor("#F66D0D"));
            holder.tvNoOfAcceptors.setText(offerStatusData.getCurrent_acceptors()+"/"+offerStatusData.getNo_of_person());
            holder.tvNoOfAcceptors.setTextColor(Color.parseColor("#2B5BD2"));
// holder.tvMonth.setText("july");
//                holder.tvDay.setText("29");
            String dateFromService = offerStatusData.getDate();
            String[] monthFromService = dateFromService.split("-");
            Log.d("date split", dateFromService.toString());
            holder.tvDay.setText(monthFromService[2].toString());
            holder.tvDay.setTextColor(Color.parseColor("#57902B"));
            holder.tvMonth.setText(getMonth(Integer.parseInt(monthFromService[1])));
            holder.tvMonth.setTextColor(Color.parseColor("#57902B"));
        }


        return convertView;



//        String color = bgColors[position % bgColors.length];
//        serial.setBackgroundColor(Color.parseColor(color));


    }
    void  delete (int post){


        offerList.remove(post);
    }
    class ViewHolder{

        TextView tvOfferName,tvPhoneNumber,tvAddress,
                tvTmcBrand,tvStartEndTime,tvNoOfPerson,tvNoOfAcceptors,tvMonth,tvDay;
    }
    public String getMonth(int month) {
        return  new DateFormatSymbols().getShortMonths()[month-1];
        // return new DateFormatSymbols().getMonths()[month-1];
    }
}

So I really do not know what is the main reason behind this , No clue left for me. Please tell me what could be the problem.


回答1:


Try this,

listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow View pager to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow View pager to intercept touch events.
                // v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle Listview touch events.
            v.onTouchEvent(event);
            return true;

        }
    });


来源:https://stackoverflow.com/questions/36470236/swipe-to-delete-not-working-from-the-top-item-of-the-listview-but-from-the-last

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!