滑动到删除不在列表视图的顶部项目中工作,而是从列表视图的最后一项

时间:2016-04-07 08:06:34

标签: android listview swiperefreshlayout

我使用this作为滑动来删除。但我在实现列表的删除菜单项时遇到问题。

我正在做的只是创建一个删除项目。我真的不想使用任何其他项目,如Open,如演示中所示。

这是我的设计

   <?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>

以下是我如何在片段中创建菜单

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);
        }
    };

这是我如何设置创建者

// set creator
    mListView.setMenuCreator(creator);
  

我的问题我提到了有关删除的几个问题,如上所述

  1. 当我在列表中有太多数据时,假设行数为10并且需要向下滚动时,删除选项的工作方式就像是从列表视图的第一行开始的魅力。
  2. 当列表中的项目较少或者我从底部继续删除项目时我的意思是最后一行,当有较少的项目不需要向下滚动时,不能顺利删除,需要多次刷新列表,或从右到左再次打开和关闭菜单,在删除菜单上多次触摸,然后在尝试太多后删除一些如何。
  3. 我不知道背后有什么问题,但当我做了两个菜单,比如演示中显示的Open和Delete,那么我打开并删除了删除按钮,所以在案例0和案例1中,我写了删除代码,然后我注意到每次触摸打开时删除都有效,但是当我触摸删除它时,有时会删除项目或者需要多次尝试删除。
  4. 可能有什么问题?有什么猜测?有什么工作吗?

    是的,确实如此,当我触摸删除菜单时,它会给我一个日志,向下看,它在日志中显示我,当我触摸删除和删除实际上不起作用,但是当它在日志中开始处理此消息从不显示。

      

    D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN

    编辑一个

      

    这是我的整个适配器代码:

        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];
        }
    }
    

    所以我真的不知道这背后的主要原因是什么,没有任何线索留给我。请告诉我可能是什么问题。

1 个答案:

答案 0 :(得分:0)

试试这个,

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;

        }
    });