2

tablayoutにあるrecyclerviewにcardviewがあります。私はonClickListenerをアダプタのcardviewに配置しますが、動作しません。私は、カードをタップするとEquationsActivityを開始したいだけです。以前はカード用のリスナーインターフェースを使用していましたが、これまでに動作するようになっています。しかし、それは機能していません。唯一の違いは、この場合、私のrecyclerviewはtablayoutにあるということです。それが問題を引き起こしているかどうかはわかりません。tablayoutのcardviewのonClickListenerが機能しない

ご意見ありがとうございます。

マイrecyclerViewAdapter:

public class recylcerAdapter extends RecyclerView.Adapter<recylcerAdapter.ViewHolder> { 
    private ListView listView; 
    private String title; 
    private Cursor cursor; 
    private Context context; 
    private TextView textView; 
    private ImageView imageView; 
    private int[] imageIds; 
    private String[] nameArray; 




    public static class ViewHolder extends RecyclerView.ViewHolder{ 
     //define the viewholder and store card views 
     //constructor 

     private TextView textView; 
     private ImageView imageView; 

     private CardView cardView; 

     public ViewHolder(CardView v){ 
      super(v); 
      cardView = v; 

     } 
    } 


    public recylcerAdapter(Context context, String title, Cursor cursor, int[]imageIds, String[]nameArray){ 
     this.cursor = cursor; 
     this.title = title; 
     this.context = context; 

     this.imageIds = imageIds; 
     this.nameArray = nameArray; 
    } 


    @Override 
    public recylcerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ 
     //create a new view 
     CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_main, parent, false); 
     return new ViewHolder(cardView); 

    } 

    public void onBindViewHolder(ViewHolder holder, final int position){ 
     //set the values inside the given view 

     CardView cardView = holder.cardView; 
     ImageView imageView = (ImageView) cardView.findViewById(R.id.list_icon); 
     Drawable drawable = cardView.getResources().getDrawable(imageIds[position]); 
     imageView.setImageDrawable(drawable); 
     imageView.setContentDescription(nameArray[position]); 
     TextView textView = (TextView) cardView.findViewById(R.id.card_text); 
     textView.setText(nameArray[position]); 

     cardView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Toast toast = Toast.makeText(context, "worked", Toast.LENGTH_SHORT); 
       toast.show(); 
        Intent intent = new Intent(context, EquationsActivity.class); 
context.startActivity(intent); 



      } 
     }); 



    } 

    @Override 
    public int getItemCount(){ 
     //return number of items in the data set 
     return nameArray.length; 
    } 
} 

マイリサイクルレイアウト:

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:id="@+id/recycler" 
android:scrollbars="vertical" 
android:clickable="true" 


android:foreground="?android:attr/selectableItemBackground" 

> 

</android.support.v7.widget.RecyclerView> 

私の主なコーディネーター&タブレイアウト:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <include 
     layout="@layout/tool_bar" 
     android:id="@+id/toolBar" 

     /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill" 
     android:background="@color/colorPrimary" 

     /> 


</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
android:id="@+id/pager" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
android:clickable="true" 
/> 

</android.support.design.widget.CoordinatorLayout> 
+1

xmlレイアウトを投稿してください。 – amitairos

+0

@amitairos ok、私はした –

+1

あなたのカードレイアウトはどこですか? @CodyCoogan – Smit

答えて

2

はViewPagerからandroid:clickable="true"を除去し、それを追加してみてくださいクリック可能にしたいカードのxml。

+1

ありがとう、これはうまくいきました –

関連する問題