【问题标题】:android onclick for swipe layout in recyclerviewandroid onclick 用于在 recyclerview 中滑动布局
【发布时间】:2016-09-19 14:17:09
【问题描述】:

我将simpleItemTouchCallback 附加到recyclerView 用于滑动列表中的项目。

              @Override
              public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
                        View itemView = viewHolder.itemView;
                        float height = (float) itemView.getBottom() - (float) itemView.getTop();
                        float width = height / 3;
                        if (dX / 3 > 0) {
                            RectF rect = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX / 3, (float) itemView.getBottom());
                            LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View v = li.inflate(R.layout.complete_layout, null);
                            v.measure(View.MeasureSpec.getSize(v.getMeasuredWidth()), View.MeasureSpec.getSize(v.getMeasuredHeight()));
                            v.layout(0, 0, Math.round(rect.width()), Math.round(rect.height()));
                            c.save();
                            c.translate(rect.left, rect.top);
                            v.draw(c);
                        } else {
                            RectF rect1 = new RectF((float) itemView.getRight() + dX / 3, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
                            LayoutInflater li1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View v = li1.inflate(R.layout.delete_layout, null);
                            v.measure(View.MeasureSpec.getSize(v.getMeasuredWidth()), View.MeasureSpec.getSize(v.getMeasuredHeight()));
                            v.layout(0, 0, Math.round(rect1.width()), Math.round(rect1.height()));
                            c.save();
                            c.translate(rect1.left, rect1.top);
                            v.draw(c);
                        }
                        c.restore();
                    }
 super.onChildDraw(c, recyclerView, viewHolder, dX / 3, dY, actionState, isCurrentlyActive);

滑动和绘制新布局正常工作。现在想在新布局中添加一个OnClickListener。我找不到这样做的方法。

【问题讨论】:

    标签: android android-recyclerview android-canvas onclicklistener


    【解决方案1】:

    应该工作。但在我的代码中,视图根本不可点击。也许它对你有用。

    RectF rect1 = new RectF((float) itemView.getRight() + dX / 3, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
    LayoutInflater li1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li1.inflate(R.layout.delete_layout, null);
    v.measure(View.MeasureSpec.getSize(v.getMeasuredWidth()), View.MeasureSpec.getSize(v.getMeasuredHeight()));
    v.layout(0, 0, Math.round(rect1.width()), Math.round(rect1.height()));
    c.save();
    c.translate(rect1.left, rect1.top); 
    
    v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // implement here your code for the click
                    }
    });
    

    【讨论】:

      【解决方案2】:
      recyclerView.addOnItemTouchListener(
          new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
          @Override
          public void onItemClick(View view, int position) {
             //Magic happens here
          }
      }
      

      顺便说一句:我不知道你在你发布的方法中做了什么^^

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-13
        相关资源
        最近更新 更多