java - Onclick Recycler View 中的按钮不起作用
问题描述
当单击按钮时,它假设打开浏览器,但没有找到启动活动的代码
public class RecyclerViewAdapterDetailsScreen extends RecyclerView.Adapter<RecyclerViewAdapterDetailsScreen.ViewHolder> {
private Context mContext;
private int numberOfButtons;
private String [] trailers;
public RecyclerViewAdapterDetailsScreen(Context mContext, int numberOfButtons,String [] trailers) {
this.numberOfButtons=numberOfButtons;
this.mContext = mContext;
this.trailers=trailers;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem2, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
/*** Toast.makeText(mContext, trailers[position]+" From OnBindViewHolder", Toast.LENGTH_SHORT).show();
* Toast is working from Here but no sign from on click
*/
holder.parentlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// TOAST IS NOT WORKING SO THE CODE DOESNT gett HERE...............................
Toast.makeText(mContext, trailers[position], Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(trailers[position]));
mContext.startActivity(browserIntent);
}
});
}
@Override
public int getItemCount() {
return trailers.length;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
Button mbutton;
RelativeLayout parentlayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mbutton=itemView.findViewById(R.id.button);
parentlayout = itemView.findViewById(R.id.parent_layout2);
}
}
}
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>
</RelativeLayout>
具有 recyclerview 的 activity_details_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/original_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="@+id/image_thumbnail"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/original_title"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vote average "
android:id="@+id/vote"
android:layout_below="@+id/overview"
android:textStyle="bold"
android:textSize="20sp"/>
<TextView
android:id="@+id/vote_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/vote"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="release date"
android:layout_below="@id/vote_average"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/release"
/>
<TextView
android:id="@+id/release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/release"
/>
<TextView
android:id="@+id/trailers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="@+id/release_date"
android:layout_centerHorizontal="true"
android:text="Trailers"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/trailers"
android:padding="8dp"
/>
</RelativeLayout>
</ScrollView>
它必须进入 onClick 并开始新活动以打开 youtube 应用程序或浏览器,如果您想查看任何缺少的代码告诉我
解决方案
你的代码很完美。这里只有一个问题,您的 parent_layout2 没有获得点击事件,它被孩子(按钮)消耗。根据 Android 定义,孩子优先考虑。
只需将父母宽度更新为 match_parent。它会起作用的。我刚刚测试了你的代码。
这是您更新的 xml
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>
</RelativeLayout>
快乐编码。如果有效,请投票。
谢谢
推荐阅读
- ios - 从主线程 iOS 上的子类创建自定义 UIView
- javascript - 内容可编辑的正则表达式空格不起作用
- c# - 如何从 azure 函数执行我的 docker 容器
- c - 使用字节数组我得到一个“从 char* 到字节的无效转换”
- java - 没有使用 JUnit @Rule 删除 TemporaryFolder
- html - 通过 CSS 替换图像
- jquery - 如何用 jquery html(String) 替换 .innerHTML(String)?
- jenkins - 在 GitHub 拉取请求页面上添加一个按钮以手动触发 Jenkins 作业
- python - 使用 pymongo 从集合中获取 Mongo 字段名称
- python - 关于网页抓取的问题 - 与 Discord