2015-07-10 54 views
3

我想刷我的列表视图的项目左右。 为此我使用这个项目。Android:滑动列表项导致clickOnItem

https://github.com/daimajia/AndroidSwipeLayout 我可以刷卡物品,但当我刷完物品后,物品的onclick就会打电话给我。我不想要这个。 我在SwipeListener上做了一些工作,但我并没有完全克服这种情况。我可以向左或向右滑动,而无需调用onClickItem但是当我刷回itemClick在调用

swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() { 
      @Override 
      public void onStartOpen(SwipeLayout swipeLayout) { 
       lineIsClose = false; 
      } 

      @Override 
      public void onOpen(SwipeLayout swipeLayout) { 

       lineIsClose = false; 
      } 

      @Override 
      public void onStartClose(SwipeLayout swipeLayout) { 
       lineIsClose = false; 
      } 

      @Override 
      public void onClose(SwipeLayout swipeLayout) { 
       lineIsClose = true ; 
      } 

      @Override 
      public void onUpdate(SwipeLayout swipeLayout, int i, int i1) { 
      } 

      @Override 
      public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) { 
      } 
     }); 

enter image description here

编辑:一些代码

adapter = new ProductsListAdapter(getActivity(), currentList); 
      adapter.setMode(Attributes.Mode.Multiple); 
      listView.setAdapter(adapter); 

package com.akakce.market.Adapters; 

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Handler; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.daimajia.swipe.SwipeLayout; 
import com.daimajia.swipe.adapters.BaseSwipeAdapter; 
import com.akakce.market.Models.ProductList; 
import com.akakce.market.Managers.SharedPrefManager; 
import com.akakce.market.Models.Product; 
import com.akakce.market.R; 
import com.akakce.market.Utils.GifMovieView; 

import java.util.List; 

/** 
* Created by cuneyt on 1.7.2015. 
*/ 
public class ProductsListAdapter extends BaseSwipeAdapter { 

    Context mContext; 
    List<Product> list; 
    ProductList currentList; 
    boolean lineIsClose = true; 
    public ProductsListAdapter(Context context, ProductList currentList) { 
     this.mContext = context; 
     this.currentList = currentList; 
     this.list = currentList.getProducts(); 

    } 

    @Override 
    public int getSwipeLayoutResourceId(int i) { 
     return R.id.swipe; 
    } 

    @Override 
    public View generateView(final int position, ViewGroup viewGroup) { 
     View v = LayoutInflater.from(mContext).inflate(R.layout.item_products_list, null); 


     return v; 
    } 

    @Override 
    public void fillValues(final int position, View convertView) { 
     Product temp = list.get(position); 
     final TextView name = (TextView) convertView.findViewById(R.id.name); 
     final TextView count = (TextView) convertView.findViewById(R.id.count); 
     final ImageView categoryView = (ImageView) convertView.findViewById(R.id.imageView_category); 
     final GifMovieView gifMovieView = (GifMovieView) convertView.findViewById(R.id.gif_1); 
     final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position)); 
     swipeLayout.getDragEdgeMap().clear(); 
     swipeLayout.addDrag(SwipeLayout.DragEdge.Left, swipeLayout.findViewById(R.id.bottom_wrapper)); 
     swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() { 
      @Override 
      public void onStartOpen(SwipeLayout swipeLayout) { 
       lineIsClose = false; 
      } 

      @Override 
      public void onOpen(SwipeLayout swipeLayout) { 

       lineIsClose = false; 
      } 

      @Override 
      public void onStartClose(SwipeLayout swipeLayout) { 
       lineIsClose = false; 
      } 

      @Override 
      public void onClose(SwipeLayout swipeLayout) { 
       lineIsClose = true ; 
      } 

      @Override 
      public void onUpdate(SwipeLayout swipeLayout, int i, int i1) { 
      } 

      @Override 
      public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) { 
      } 
     }); 

     if (temp.isCompleted()) { 
      name.setTextColor(mContext.getResources().getColor(R.color.gray)); 
      name.setPaintFlags(name.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
      count.setPaintFlags(count.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
     } else { 
      name.setTextColor(mContext.getResources().getColor(R.color.black)); 
      name.setPaintFlags(name.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
      count.setPaintFlags(count.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
     } 
     name.setText(temp.getName()); 
     count.setText("" + temp.getCount()); 


     if (position % 2 == 0)// oylesine var 
      categoryView.setBackgroundColor(Color.BLUE); 
     if (position % 3 == 0) 
      categoryView.setBackgroundColor(Color.RED); 

     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (lineIsClose){ 
        gifMovieView.setVisibility(View.VISIBLE); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         gifMovieView.setVisibility(View.GONE); 
         if (!list.get(position).isCompleted()) {//false ise true yap 

          list.get(position).setCompleted(true); 
          name.setTextColor(mContext.getResources().getColor(R.color.gray)); 
          name.setPaintFlags(name.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
          count.setPaintFlags(count.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 

         } else { // true ise false yap 

          list.get(position).setCompleted(false); 
          name.setTextColor(mContext.getResources().getColor(R.color.black)); 
          name.setPaintFlags(name.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
          count.setPaintFlags(count.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 

         } 

         currentList.setProducts(list); 
         SharedPrefManager.saveList(currentList); 
        } 
       }, 600); 

       //saveList(gifMovieView); 
      } 
     } 
    }); 
     convertView.findViewById(R.id.bottom_wrapper).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       swipeLayout.close(); 
       // 
       // removeFromList(position); 
       list.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 

    } 

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

    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

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

请添加您的代码,你必须设置swipelistview适配器。 – Pavya

回答

0

我有同样的问题,我用这个代码的解决了这个:

viewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() { 
     @Override 
     public void onOpen(SwipeLayout layout) { 

      super.onOpen(layout); 
      ((Fragment)mFragment).setListViewClickable(false); 
     } 

     @Override 
     public void onClose(SwipeLayout layout) { 

      super.onClose(layout); 
      mHandler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        ((Fragment) mFragment).setListViewClickable(true); 
       } 
      } , 100); 

     } 

     @Override 
     public void onStartOpen(SwipeLayout layout) { 
      ((Fragment)mFragment).setListViewClickable(false); 
      super.onStartOpen(layout); 
      } 

     @Override 
     public void onStartClose(SwipeLayout layout) { 

      ((Fragment)mFragment).setListViewClickable(false); 
      super.onStartClose(layout); 
     } 
    }); 

一般当刷卡布局打开我禁用列表视图点击时刷卡布局紧密启用一些时间等之后的列表视图点击100毫秒。

+0

我解决了它之后,忘记了编辑我的问题。谢谢。你的重播可能会帮助某人。 –

+0

哦谢谢接受答案。我有一个问题,当我删除一个列表项,然后我的列表视图得到刷新,但它再次打开该位置的swipelayout。你可以帮我吗 ? –

+0

=)我面临同样的问题。刷新前应关闭swipeLayout。像这样:swipeLayout.close(); –

6

我有一个像你一样的问题。我在这里找到答案:http://android-pratap.blogspot.com/2015/07/swipe-recyclerview-using.html

您可以在onClick上设置SurfaceView()。现在,您可以在不调用onClickItem的情况下轻扫该项目。

viewHolder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(mContext, " onClick : " + item.getName() + " \n" + item.getEmailId(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
2

您应该使用

swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    // delete view or something you want to do 
    } 
}); 
相关问题