gpt4 book ai didi

java - tablayout 中 cardview 的 onClickListener 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:23 24 4
gpt4 key购买 nike

我在 tablayout 中的 recyclerview 中有一个 cardview。我在适配器的卡片 View 上放置了一个 onClickListener,但它不起作用。我只想在点击卡片时启动 EquationsActivity。在使用卡片的 Listener 界面之前,我已经让这个工作了。但这不起作用,唯一的区别是在这种情况下,我的 recyclerview 在 tablayout 中。我不确定这是否会导致任何问题。

感谢您的任何建议!

我的 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>

最佳答案

尝试从 ViewPager 中删除 android:clickable="true",并将其添加到您希望可点击的卡片的 xml 中。

关于java - tablayout 中 cardview 的 onClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38904304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com