gpt4 book ai didi

java - Onclick Recycler View 中的按钮不起作用

转载 作者:行者123 更新时间:2023-11-30 04:58:37 25 4
gpt4 key购买 nike

当点击 Button 时,它应该打开浏览器,但它没有到达启动 Activity 的代码

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 并开始新的 Activity 以打开 youtube 应用程序或浏览器,如果您想查看任何缺少的代码告诉我

最佳答案

您的代码是完美的。这里只有一个问题,您的 parent_layout2 没有获得点击事件,它被 child (按钮)使用。根据 Android 定义, child 优先。

只需将父级宽度更新为 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>

快乐编码。如果有效请投票。

谢谢

关于java - Onclick Recycler View 中的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672074/

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