Onclick回收器视图中的按钮不起作用

问题描述 投票:0回答:2

当单击按钮时,它应该打开浏览器,但没有获得启动活动的代码

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应用或浏览器

java android recycler-adapter
2个回答
0
投票
直接在RecyclerView中的按钮上的setOnClickListener

    尝试移除按钮并插入不可点击的内容,例如TextView:

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.