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> 

クリックするとカードビューが動作しますが、リサイクルビューの領域はクリックできません。カードビューのイベントがキャプチャされるように、どのようにクリック可能にすることができますか。カードビュー内でリサイクル可能なビューをクリック可能にする

+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では、リストビューのようにクリック可能なものを使用するようにアダプタの順序を設定する必要があります。あなたが見にここで別の質問取ることができますhttps://developer.android.com/training/material/lists-cards.html : あなたは次のリンクを参照することができ、この //holder.mTextView.setOnClickListener(onClickよう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; 
} 

}

//何かを)。

+0

詳細を教えてください。アダプターの注文はどういう意味ですか? – zooney

+0

こんにちは私はちょうど答えを編集します。見てください。 –

0

recyclerviewアダプタのビュー・ホルダ内でクリック・リスナーを設定する必要があります。 Viewholder内では、すべてのビューのすべての要素、recyclerviewが膨張するLayoytのViewgroupにアクセスできます。私はそれが理にかなったことを願う

また、onBind()メソッドでonclickリスナーを設定することもできます。あなたのcardviewがそれにアクセスするための直接的な方法はありません外のリサイクル業者のビューがある場合

あなたがthis question

を見てみることができます。あなたのリサイクルの観点から、あなたのカードビューを膨張されている活動/フラグメントにコールバックやイベントバスを使用して、あなたのカードビューでは、「InterceptTouchCardView」で新しい価値

+0

リサイクラービューをクリックすると、カードビューのクリックイベントをキャプチャしたいと考えています。 – zooney

+0

@zooney私は私のcomnentsを更新しました。見てみましょう – Akshay

0

soulutionで更新RecyclerViewのアイテムにクリック可能なビュー(ボタン)がいくつかある場合に問題があります。これらのクリック可能なビューは、決してタッチイベントを受け取ることはありません。より良いsoulutionはRecyclerViewを拡張し、それらの両方がちょうど(Kotlin構文)このような偽を返すので、そのonInterceptTouchEventとonTouchEventを上書きすることです:レイアウトファイル内ClickThroughRecyclerViewを使用することにより

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 
    } 

} 

を確保し、そのタッチイベントは、によって消費されませんRecyclerViewのアイテム(ボタンなど)は、RecyclerViewの親ビューに伝播されます。

関連する問題