2017-05-04 108 views
0

我有一個回收視圖和cards.I要像向上輕掃手勢刪除card.My代碼:如何在RecyclerView,GridLayout中實現卡片刷卡?

 recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     data_list = new ArrayList<>(); 
     load_data(); 
     gridLayoutManager = new GridLayoutManager(this, 1,LinearLayoutManager.HORIZONTAL,false); 
     recyclerView.setLayoutManager(gridLayoutManager); 
     recyclerView.setHasFixedSize(true); 
     adapter = new CustomAdapter(this,data_list); 
     recyclerView.setAdapter(adapter); 

CustomAdapterClass:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { 

private Context context; 
private List<MyData> my_data; 

public CustomAdapter(Context context, List<MyData> my_data) { 
    this.context = context; 
    this.my_data = my_data; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false); 
    itemView.setMinimumWidth(parent.getMeasuredWidth()); 
    itemView.setMinimumHeight(parent.getMeasuredHeightAndState()); 
    return new ViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) 
{ 
    holder.userstory.setText(my_data.get(position).getUserstory()); 
    holder.tasks.setText(my_data.get(position).getTask()); 
    holder.actual.setText(my_data.get(position).getActuals()); 
    holder.estimate.setText(my_data.get(position).getTodos()); 

} 

@Override 
public int getItemCount() 
{ 
    return my_data.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder 
{ 
    public TextView userstory,tasks,actual,estimate; 
    public CircularSeekBar seekbar; 

    public ViewHolder(View itemView) 
    { 
     super(itemView); 
     userstory=(TextView) itemView.findViewById(R.id.tvUserStory); 
     tasks=(TextView) itemView.findViewById(R.id.tvTask); 
     actual=(TextView) itemView.findViewById(R.id.tvActual); 
     estimate=(TextView) itemView.findViewById(R.id.tvEstimate); 
    } 
} 
} 

我收到卡水平滾動視圖。我想添加滑動手勢來解僱它 我該如何實現這一目標?

+0

也許這篇文章https://medium.com/@ipaulpro/drag-and-swipe-with-recyclervi ew-b9456d2b1aaf可以幫助你。 – beeb

回答

2

使用ItemTouchHelper爲recyclerview

添加在這些的onCreate線()activty的方法。

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); 
itemTouchHelper.attachToRecyclerView(recyclerView); 

,然後定義回調ItemTouchHelper ..

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { 

     @Override 
     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { 
      Toast.makeText(context, "on Move", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 

     @Override 
     public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir) { 

       Toast.makeText(context, "on Swipe", Toast.LENGTH_SHORT).show(); 
     } 
    }; 

如果你想拿回你的刷卡數據/卡就用這條線..

adapter.notifyItemChanged(viewHolder.getAdapterPosition()); 

,您可以訪問: - https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.html

+0

是的..它工作..只是改變ItemTouchHelper.UP ..但幾秒鐘後它再次顯示 – Akshay

+0

我想完全從列表中刪除...如果我刷第一項第二個應該成爲第一個 – Akshay

+0

代碼這些行'arraylist.remove(viewHolder.getAdapterPosition());'和'adapter.notifyDataSetChanged();' – user392117