0
  <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_subscribe_card"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:onClick="@{vm.onGoalPress}"
    card_view:cardCornerRadius="4dp">

<android.support.v7.widget.RecyclerView
                android:id="@+id/feeds_list"
                android:layout_width="200dp"
                android:layout_height="30dp"
                android:layout_marginTop="6dp" />

</CardView>

Click上的卡片视图正在工作。但回收站视图的区域不可点击。如何使其可点击以便捕获卡片视图事件。

4

4 回答 4

0
public class InterceptTouchCardView extends CardView {

public InterceptTouchCardView(Context context) {
    super(context);
}

public InterceptTouchCardView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

/**
 * Intercept touch event so that inner views cannot receive it.
 * 
 * If a ViewGroup contains a RecyclerView and has an OnTouchListener or something like that,
 * touch events will be directly delivered to inner RecyclerView and handled by it. As a result, 
 * parent ViewGroup won't receive the touch event any longer.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}

}

于 2016-06-21T13:16:45.243 回答
0

对于 recylerview,您需要设置适配器顺序以使用可点击的列表视图。您可以参考以下链接:https ://developer.android.com/training/material/lists-cards.html 您可以在这里查看另一个问题:为什么 RecyclerView 没有 onItemClickListener()?RecyclerView 与 Listview 有何不同?

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] mDataset;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ViewHolder(TextView v) {
        super(v);
        mTextView = v;
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.my_text_view, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ...
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.mTextView.setText(mDataset[position]);

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}

}

//类似这样的 //holder.mTextView.setOnClickListener(onClick);

于 2016-06-21T06:14:44.633 回答
0

InterceptTouchCardView 当项目RecyclerView本身有一些可点击的视图(即按钮)时,解决方案会出现问题。然后那些可点击的视图永远不会收到任何触摸事件。更好的解决方案是扩展 RecyclerView 并覆盖它的onInterceptTouchEventand onTouchEvent,所以它们都像这样返回 false (Kotlin 语法):

class ClickThroughRecyclerView : RecyclerView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attr: AttributeSet?) : super(context, attr)

    constructor(context: Context, attr: AttributeSet?, defStyle: Int) : super(context, attr, defStyle)

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        return false
    }

    override fun onTouchEvent(e: MotionEvent): Boolean {
        return false
    }

}

通过ClickThroughRecyclerView在布局文件中使用可确保RecyclerView' 的项目(如按钮,...)未使用的触摸事件被传播到RecyclerView' 的父视图。

于 2017-01-10T18:19:07.050 回答
0

您需要在 recyclerview Adapter 的 viewholder 中设置点击监听器。在 Viewholder 中,您可以访问每个视图的所有元素,即 recyclerview 膨胀的layoyt 的 Viewgroup。我希望这是有道理的。

或者,您可以在 onBind() 方法中设置 onclick 侦听器。

你可以看看这个问题

如果您的 cardview 在回收站视图之外,则无法直接访问它。使用从您的回收站视图中的回调或事件总线到您正在扩充卡片视图的活动/片段,然后使用新值更新您的卡片视图

于 2016-06-21T06:34:37.667 回答