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上的卡片視圖正在工作。但回收站視圖的區域不可點擊。它如何被點擊以捕獲卡片視圖事件。在卡片視圖內製作可回收點擊的視圖

+0

您的問題並不十分清楚..您希望將cradview作爲回收站視圖的一部分添加,但您將其作爲回收站視圖聲明在相同的視圖組中。您應該將卡視圖作爲回收視圖適配器的一部分。 – Akshay

回答

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

}

0

對於recylerview,您需要設置適配器順序以使用可點擊的類似listview。 您可以參考以下鏈接:https://developer.android.com/training/material/lists-cards.html 你可以到這裏看看另一個問題:Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from 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);

+0

您能否詳細說明一下,適配器的順序是什麼意思? – zooney

+0

嗨,我只是編輯答案。請看一下。 –

0

您需要在recyclerview適配器的視圖中設置點擊偵聽器。在Viewholder中,您可以訪問每個視圖的所有元素,即回收站查看擴展的layoyt的Viewgroup。我希望這是有道理的。

或者,您可以在onBind()方法中設置onclick監聽器。

如果您cardview在外面回收觀點再有就是訪問它沒有直接的方法你可以看看this question

。使用回調或事件總線從回收以活動/片段你在哪裏充氣您的卡片視圖,然後用新值

+0

點擊回收站視圖時,我想捕獲卡片視圖的點擊事件。 – zooney

+0

@zooney我已經更新了我的部分。看一看 – Akshay

0

的soulution與「InterceptTouchCardView」更新您的卡片視圖當RecyclerView的項目本身具有一些可點擊的視圖(即按鈕)時有問題。那些可點擊的視圖從不會收到任何觸摸事件。更好soulution是延長RecyclerView並覆蓋其onInterceptTouchEvent及的onTouchEvent,所以他們都只是返回false像這樣(科特林語法):

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的父視圖。

相關問題