如何在RecyclerView,GridLayout中实现刷卡?

时间:2017-05-04 08:51:36

标签: android

我有一个回收者视图和卡片。我想要一个像刷卡一样的手势来移除卡片。我的代码是:

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

CustomAdapterClass:

print('Please enter lines of text.')
print('Finish with a blank line.')
maxi = 0
text = '.'
while len(text) > 0:
    text = input()
    if len(text) > maxi:
        maxi = len(text)
if maxi == 0:
    print('No text entered.')
else:
    print('The longest line of text was ' + str(maxi) + ' characters long.')

我在水平滚动视图中获取卡片。我想添加滑动手势以解除它 我该如何实现这个目标?

1 个答案:

答案 0 :(得分:2)

使用ItemTouchHelper进行recyclerview

在activty的onCreate()方法中添加这些行。

 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