通过用手指在侧面滑动来删除listView的项目

时间:2017-06-21 10:27:38

标签: android listview ontouchlistener custom-adapter

我的目标是通过用手指在侧面滑动来删除listView的项目。我试图在ViewHolder类的布局上放置onTouchListener。我有几个问题:

  1. 当我用手指向下滑动或顶部时,列表滚动,并且项目保持其新位置(它不会返回初始位置)
  2. 如果出现第一个问题,当我向下滚动列表时,几个项目的位置与第一个相同。
  3. 如果我成功将项目滑动到我定义的限制,则删除的项目始终不是好项目。另外我有任何alpha动画。
  4. 使用onTouchListener,我的onItemClick侦听器不再工作了。 (我想为所选列表项的背景着色。)
  5. 以下是我要做的事情的概述:

    list preview

    请参阅下面的代码:

    我的自定义列表适配器

    public class VisiteAdapter extends ArrayAdapter<Visite> {
    
    private Visite visite = null;
    private float _xDelta;
    private float initialXPos;
    private float initialTouchXPos;
    private boolean haveAlreadyCheckXPos = false;
    private List listeVisite = null;
    
    public VisiteAdapter(Context context, List<Visite> items) {
        super(context, R.layout.item_visite, items);
        listeVisite = items;
    }
    
    
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    
        final ViewHolder viewHolder;
        visite = getItem(position);
    
    
        final View result;
    
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater vi = LayoutInflater.from(getContext());
            convertView = vi.inflate(R.layout.item_visite, null);
            viewHolder.nomCopro = (TextView) convertView.findViewById(R.id.listeNomCopro);
            viewHolder.dateVisite = (TextView) convertView.findViewById(R.id.listDateVisite);
            viewHolder.itemLayout = (LinearLayout) convertView.findViewById(R.id.itemLayout);
            viewHolder.globalItem = (FrameLayout) convertView.findViewById(R.id.globalItem);
    
            //OnTouchListener on the first layer of my item
            viewHolder.itemLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    int slideLimit = 150;
    
                    switch (event.getAction()) {
    
                        case MotionEvent.ACTION_DOWN:
    
                            if(!haveAlreadyCheckXPos){
                                //get Initial X position of view.
                                initialXPos = v.getX();
    
                                haveAlreadyCheckXPos=true;
                            }
    
                            //initial X position of my finger when i touch the view
                            initialTouchXPos = event.getRawX();
    
                            //usefull value for translation
                            _xDelta = v.getX() - event.getRawX();
    
                            break;
    
                        case MotionEvent.ACTION_MOVE:
    
                            //Allow the view to slide to the left only. The view is blocked at a certain distance of the initial pos with the "slideLimit".
                            if(initialTouchXPos - event.getRawX() >= 0 && initialTouchXPos - event.getRawX() <= slideLimit){
    
                                //The sliding effect
                                v.animate()
                                        .x(event.getRawX() + _xDelta)
                                        .setDuration(0)
                                        .start();
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            //if the view is near to the slideLimit
                            if(initialXPos - v.getX() >= slideLimit-20){
                                //item is remove from the list with an alpha animation (alpha animation doesn't work)
                                viewHolder.globalItem.animate().alpha(0.0f).setDuration(350).start();
                                Log.e("ListeVisite.adapter"," ITEM DELETE");
                                listeVisite.remove(position);
                                notifyDataSetChanged();
                            }else{
                                //View return to the initial position
                                TranslateAnimation returnToInitialPos = new TranslateAnimation(0, initialXPos - v.getX(), 0, 0);
                                returnToInitialPos.setDuration(500);
                                returnToInitialPos.setFillAfter(true);
                                v.startAnimation(returnToInitialPos);
                            }
    
                            break;
                        default:
                            return false;
                    }
    
                    return true;
                }
            });
    
            result=convertView;
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }
    
    
        if (viewHolder.nomCopro != null) {
            viewHolder.nomCopro.setText("CoproID : "+visite.getCOPRO_REF());
        }
    
        if (viewHolder.dateVisite != null) {
            viewHolder.dateVisite.setText(UsefulMethods.dateToString(visite.getV_DATE_VISITE(),"dd/MM/yyyy"));
        }
    
        //Selection of the item work with a boolean in the model. Look at the MainActivity class
        if(visite.isSelected){
            viewHolder.itemLayout.setBackgroundColor(ResourcesCompat.getColor(getContext().getResources(), R.color.colorPrimaryLight, null));
        }else{
            viewHolder.itemLayout.setBackgroundColor(ResourcesCompat.getColor(getContext().getResources(), R.color.white, null));
        }
    
    
    
    
    
        return convertView;
    }
    private static class ViewHolder {
        TextView nomCopro;
        TextView dateVisite;
        LinearLayout itemLayout;
        FrameLayout globalItem;
    }
    
    }
    

    MainActivity中的listView

    final ListView listViewVisite = (ListView) findViewById(R.id.visiteList);
    
        for(int i =0; i<25;i++ ){
            Visite visite1 = new Visite();
            visite1.setCOPRO_REF(i);
            visite1.setV_DATE_VISITE(UsefulMethods.stringToDate("15/06/2017", "dd/MM/yyyy"));
            listVisite.add(visite1);
        }
    
        visiteAdapter = new VisiteAdapter(MainActivity.this, listVisite);
    
        listViewVisite.setAdapter(visiteAdapter);
    
        listViewVisite.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("ListeVisite","OnClick");
    
    
    
                for(Visite visiteTemp : listVisite){
                    visiteTemp.isSelected=false;
                }
                Visite visite = (Visite) parent.getItemAtPosition(position);
                visite.isSelected = true;
                listVisite.set(position, visite);
                visiteAdapter.notifyDataSetChanged();
    
            }
        });
    

    我的列表项布局

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:id="@+id/globalItem"
        >
    <!-- Red layer below the white layer -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent3"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center_vertical"
        >
    
        <View
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:background="@drawable/ic_delete"
            />
    
    </RelativeLayout>
    <!-- Exposed layer -->
    <LinearLayout
        android:id="@+id/itemLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:padding="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:clickable="true"
        >
    
        <TextView
            android:id="@+id/listeNomCopro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nom de la copropriété"
            android:textSize="20sp"
            />
    
        <TextView
            android:id="@+id/listDateVisite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="15/06/2017"
            android:textSize="20sp"
            android:layout_marginTop="20dp"
            />
    
    </LinearLayout>
    </FrameLayout>
    

    listView

    <ListView
            android:id="@+id/visiteList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/lightGrey3"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:listSelector="@android:color/transparent"
            android:animateLayoutChanges="true"
    
            >
    </ListView>
    

    提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这段代码

$("#start_time").on("change", function(e) {
  alert("helloi");
});

$("#book_meeting_start_date").on("change", function(e) {
  alert("helloi");
});

$("#book_meeting_start_date").on("change.dp", function(e) {
  alert("helloi");
});