Android RecyclerView OnlistItem单击以创建新的意图

Android RecyclerView OnlistItem单击以创建新的意图,android,android-intent,android-cardview,android-recyclerview,Android,Android Intent,Android Cardview,Android Recyclerview,所以,在最新更新之前,我使用onListItemClick listener,它工作正常,但现在我尝试使用RecyclerView,我不确定如何为每个项目实现onClick,这将打开一个新的活动 这是我以前的东西 public class SermonsFragment extends Fragment { @Override public void onListItemClick(ListView list, View v, int position, long id) {

所以,在最新更新之前,我使用onListItemClick listener,它工作正常,但现在我尝试使用RecyclerView,我不确定如何为每个项目实现onClick,这将打开一个新的活动

这是我以前的东西

public class SermonsFragment extends Fragment {

    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {
        Intent mediaStreamIntent = new Intent(getActivity(), MediaStreamPlayer.class);
        mediaStreamIntent.putExtra("sermon_details", (android.os.Parcelable) list.getItemAtPosition(position));
        startActivity(mediaStreamIntent);
    }
}
但是现在,我没有使用listview,而是创建了一个布道适配器,它如下所示

public class SermonListAdapter extends RecyclerView.Adapter<SermonListAdapter.ViewHolder>{
    private ArrayList<Sermon> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    //Note: need to remove static class no idea why
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        // each data item is just a string in this case
        public View mView;
        public ViewHolder(View v) {
            super(v);
            v.setOnClickListener(this);
            mView = v;
        }

        @Override
        public void onClick(View v) {
            Log.d("SermonsListAdapter.java.debug", "itemClick " + mDataset.get(getPosition()).getName());

        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public SermonListAdapter(ArrayList<Sermon> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public SermonListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sermon_cardview, parent, false);
        // set the view's size, margins, paddings and layout parameters

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        TextView title = (TextView) holder.mView.findViewById(R.id.sermon_title);
        TextView series = (TextView) holder.mView.findViewById(R.id.sermon_series);
        TextView pastor = (TextView) holder.mView.findViewById(R.id.sermon_pastor);
        TextView sermonDate = (TextView) holder.mView.findViewById(R.id.sermon_date);

        title.setText(mDataset.get(position).getName());
        series.setText(mDataset.get(position).getSeries());
        pastor.setText(mDataset.get(position).getPastor());
        sermonDate.setText(mDataset.get(position).getSermonDate());

    }
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    card_view:cardCornerRadius="1dp">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/sermon_title" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_series" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_pastor" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_date" />
    </LinearLayout>
</android.support.v7.widget.CardView>

您可以在adpater中的
onBindViewHolder
方法中的视图上实现
onClick

  • 为保存项单元格的视图指定id
  • 以文本视图的方式获取视图
  • 将onClick设置为方法内部的根,如下所示:

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        viewHolder.relLayout.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                 // perform your operations here 
            }
        });
    }
    
  • 编辑:

    这就是在xml中分配id的方式

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lnrLayout"  ---------->> This is new
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/sermon_title" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_series" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_pastor" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/sermon_date" />
    </LinearLayout>
    
    这是您的自定义viewholder,因此请按照我们声明变量的方式声明TextView。。因此,您的onBindView方法将如下所示:

     @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(mDataset.get(position).getName());
        holder.series.setText(mDataset.get(position).getSeries());
        holder.pastor.setText(mDataset.get(position).getPastor());
        holder.sermonDate.setText(mDataset.get(position).getSermonDate());
        holder.lnrLayout.setOnClickListener(new OnClickListener(){
              public void onClick(View v) {
                 // on click action here
                 //-- use context to start the new Activity
                Intent mediaStreamIntent = new Intent(mContext, MediaStreamPlayer.class);
                mediaStreamIntent.putExtra("sermon_details", (android.os.Parcelable) mDataset.get(position));
                mContext.startActivity(mediaStreamIntent);
              }
        });
    }
    
    我真的不知道为什么这两者之间有区别,可能是因为您在初始化onbind中的视图,而不是viewholder构造函数

    你也可以参考

    编辑2:(第二种方法)

    将适配器更改为以下内容:

    // Provide a suitable constructor (depends on the kind of dataset)
    public SermonListAdapter(ArrayList<Sermon> myDataset, Fragment fragment) {
        mDataset = myDataset;
        mFragment = fragment;
    }
    
    在fragment类中,使用相同的参数定义创建一个名为sendtonextactivity的公共方法,然后调用下一个intent

    第三种方法

    在适配器中创建接口,为接口创建set方法,在片段中实现接口,然后对其进行初始化,然后将其传递给适配器的set方法

    然后用这个:

    if(mListener!= null) {
             mListener.sendToNextActivity(position); -> you can pass any data you wsh to
         }
    

    这里有几个问题:实现点击BindViewHolder和公共类ViewHolder Extendes RecyclerView.ViewHolder之间有什么区别吗?正如你们现在看到的,我在ViewHolder类中有它,当我试图创建意图时,它给出了语法错误。另外,如何为视图分配id?这里真的是初学者,需要一步一步的教程。。感谢you@Harts-如果有帮助,也请参考链接lnrLayoutCardView.setOnClickListener(新视图.OnClickListener(){public void onClick(视图v){//on click action here Log.d(“SermonsListAdapter.java.debug”,“Test”);Intent mediaStreamIntent=new Intent(getActivity(),MediaStreamPlayer.class);mediaStreamIntent.putExtra(“布道详细信息”,(android.os.Parcelable)mDataset.get(position));startActivity(mediaStreamIntent);};我不能使用getactivity和start activity,它说的是无法解析方法,而不是getactivity,您需要在上下文中使用它们。。您可以在适配器的构造函数中获取。。然后将其存储在本地上下文obj中。。然后使用这个。flag_new_任务基本上会将新活动创建为堆栈中的一个新活动。因此,按下后退按钮不会引导您进入上一个屏幕,而是退出应用程序,如果这是唯一的屏幕(不确定)。。另一种方法是,在适配器中传递对片段的引用,然后将位置传递给片段类,然后从片段启动新活动。。等待@Harts检查编辑。。
     @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(mDataset.get(position).getName());
        holder.series.setText(mDataset.get(position).getSeries());
        holder.pastor.setText(mDataset.get(position).getPastor());
        holder.sermonDate.setText(mDataset.get(position).getSermonDate());
        holder.lnrLayout.setOnClickListener(new OnClickListener(){
              public void onClick(View v) {
                 // on click action here
                 //-- use context to start the new Activity
                Intent mediaStreamIntent = new Intent(mContext, MediaStreamPlayer.class);
                mediaStreamIntent.putExtra("sermon_details", (android.os.Parcelable) mDataset.get(position));
                mContext.startActivity(mediaStreamIntent);
              }
        });
    }
    
    // Provide a suitable constructor (depends on the kind of dataset)
    public SermonListAdapter(ArrayList<Sermon> myDataset, Fragment fragment) {
        mDataset = myDataset;
        mFragment = fragment;
    }
    
    if(mFragment != null && mFragment instanceof SermonFragment) {
          ((SermonFragment)mFragment).sendToNextActivity(position); -> you can pass any data you wsh to
     }
    
    if(mListener!= null) {
             mListener.sendToNextActivity(position); -> you can pass any data you wsh to
         }