当单击“按钮”时,它应该打开浏览器,但没有获得启动活动的代码
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应用或浏览器
答案 0 :(得分:0)
您正在将侦听器注册到parentLayout上。您的RelativeLayout父项布局包装了一个Button。我假设单击时,该事件由Button侦听器注册。 您可以:
尝试删除按钮并插入不可点击的内容,例如TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>
答案 1 :(得分:0)
您的代码是完美的。 这里只有一个问题,您的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>
快乐编码。如果可行,请投票。
谢谢