RecyclerView onListItemClick创建新意图

时间:2014-12-19 02:44:55

标签: android android-intent android-recyclerview android-cardview recycler-adapter

所以,在最新更新之前,我使用onListItemClick监听器并且它工作正常,但现在我尝试使用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());

    }

并且片段或多或少相同,它只是我不能再使用onListItemClick

    public class SermonsFragment extends Fragment {
        private static final int MAX_SERMONS_LIST = 20;
        private ArrayAdapter<Sermon> listAdapter;
        private String imageUrl;
        private static String sermonListJSONUrl = “http://someurl”;

        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);

            //Check if there is internet, if yes call JSONParser
            ConnectionDetector myConnection = new ConnectionDetector(getActivity().getApplicationContext());
            Boolean isInternetOnline = false;
            isInternetOnline = myConnection.isConnectingToInternet();

            if(isInternetOnline) {
                //Call JSONParser Asynchronously to get sermonList in JSON Format
                new callJSONParserAsync().execute(sermonListJSONUrl);
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_sermons, container, false);
            mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            mLayoutManager = new LinearLayoutManager(getActivity());
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());

            //Just an Empty Class
            ArrayList<Sermon> mySermon = new ArrayList<Sermon>();

            //specify an adapter
            mAdapter = new SermonListAdapter(mySermon);
            mRecyclerView.setAdapter(mAdapter);
        }

我的cardview xml看起来像这样

<!-- 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>

我在尝试创建新意图

时遇到此错误
12-18 22:31:48.469  31887-31887/org.ifgfseattle.ifgfseattle E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: org.ifgfseattle.ifgfseattle, PID: 31887
    android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
            at android.app.ContextImpl.startActivity(ContextImpl.java:1232)
            at android.app.ContextImpl.startActivity(ContextImpl.java:1219)
            at android.content.ContextWrapper.startActivity(ContextWrapper.java:322)
            at org.ifgfseattle.ifgfseattle.adapter.SermonListAdapter$1.onClick(SermonListAdapter.java:81)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:13)

您可以在adpater内的onClick方法中对视图实施onBindViewHolder

  1. 将ID分配给包含项目单元格的视图
  2. 以文字视图的方式获取视图
  3. 将onClick设置为方法内的root,如下所示:

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

    这是你在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>
    

    这是您定义视图(或可能实例化它们)的方式

    public ViewHolder(View mView) {
            super(view);
            title = (TextView) holder.mView.findViewById(R.id.sermon_title);
            series = (TextView) holder.mView.findViewById(R.id.sermon_series);
            pastor = (TextView) holder.mView.findViewById(R.id.sermon_pastor);
            sermonDate = (TextView) holder.mView.findViewById(R.id.sermon_date)
            lnrLayout = (LinearLayout)holder.mView.findViewById(R.id.lnrLayout);
        }
    

    这是你的自定义视图,所以声明TextViews就像我们声明变量一样..你的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构造函数。

    您也可以参考this

    编辑2 :(第二种方法)

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

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

    在onClick上执行此操作:

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

    在fragment类中创建一个名为sendToNextAcitivity且具有相同param定义的公共方法,然后调用下一个intent。

    第三种方法

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

    然后使用它:

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