setOnClickListener - 如何在onClick中传入AdapterView <! - ? - >

时间:2016-04-17 11:07:41

标签: android android-fragments android-recyclerview android-cursoradapter android-cursor

更新:我修改了原来的问题,因为它太模糊了。

我刚刚实施了RecyclerView,现在我想让每个项目都可以点击,这样当我点击一个项目时,它会带我去DetailsActivity(手机和平板电脑),在那里我可以找到有关该项目的更多信息。

在实现recyclerview之前我能够这样做,因为我所拥有的只是一个gridview,我调用了mGridView.setOnItemClickListener。

以下是recyclerview之前的代码:

public class BookFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

   ....
   .....

    public interface CallbackInterface {
        /**
         * DetailFragmentCallback for when an item has been selected.
         */
        public void onItemSelected(Uri bookUri);
    }


    ....
    .....


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        // initialize our BookAdapter
        mBookAdapter = new BookAdapter(getActivity(), null, 0, CURSOR_LOADER_ID);
        // initialize mGridView to the GridView in fragment_main.xml
        mGridView = (GridView) rootView.findViewById(R.id.Book_grid);
        // set mGridView adapter to our CursorAdapter
        mGridView.setAdapter(mBookAdapter);

        // make each item clickable
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                int uriId = position + 1;

                Uri uri = ContentUris.withAppendedId(BookContentProvider.Book.CONTENT_URI,
                        uriId);

                Cursor cursor = (Cursor) parent.getItemAtPosition(position);
                if (cursor != null) {

                    ((CallbackInterface) getActivity())
                            .onItemSelected(BookContentProvider.Book.withId(cursor.getLong(0))); // getlong(0) gets the row ID
                }
            }
        });


        return rootView;
    }
.....

}

现在我有了recyclerview,看起来我不能再调用mGridView.setOnItemClickListener了,因为Recyclerview没有这个方法。看起来我可以调用setOnClickListener,但我不确定如何在BookCursorAdapter中实现它:

这是我的代码BookCursorAdapter:

public class BookCursorAdapter extends CursorRecyclerViewAdapter<BookCursorAdapter.ViewHolder> {

    private static final String LOG_TAG = BookCursorAdapter.class.getSimpleName();
    private static Context mContext;
    private ViewHolder mVh;


    public BookCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor);
        mContext = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final ImageView imageView;

        public ViewHolder(View view) {
            super(view);

            mView = view;
            imageView = (ImageView) view.findViewById(R.id.list_item_book_imageview);

        }

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_Book, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        mVh = vh;
        return vh;
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {

        DatabaseUtils.dumpCursor(cursor);

        int imageIndex = cursor.getColumnIndex(BookColumns.COLUMN_IMAGEPATH);
        String image = cursor.getString(imageIndex);
        if (image != null) {
            Picasso.with(viewHolder.imageView.getContext())
                    .load("http://image.tmdb.org/t/p/w400/" + image)
                    .resize(600, 800)  
                    .centerCrop()
                    .into(viewHolder.imageView);
        }


    }

}

1 个答案:

答案 0 :(得分:1)

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH> {

private Cursor cursor;

public MyAdapter(Cursor c) {
    cursor = c;
}

// View Holder
public static class VH extends RecyclerView.ViewHolder {

    public TextView _textView;
    public ImageView _icon;

    public VH(View v) {
        super(v);
        _textView = (TextView) v.findViewById(android.R.id.text1);
        _icon = (ImageView) v.findViewById(android.R.id.icon);
    }

}


@Override
public MyAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflate the layout and create a new ViewHolder with it.
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    final View v = inflater.inflate(R.layout.baseitem, parent, false);
    return new VH(v);
}

@Override
public void onBindViewHolder(final VH holder, int position) {
    // This is called whenever the RecyclerView needs to bind data to the viewholders.
    // This will happen continuously whilst scrolling.
    // Viewholders scrolled off screen are likely to be reused.
    // so make sure you reset any values in it to the defaults.

    cursor.moveToPosition(position);

    holder._textView.setText(cursor.getString(cursor.getColumnIndex("MyStringData")));
    holder._icon.setImageResource(R.drawable.icv_arrow_up);

    holder._icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("TAG", holder._textView.getText().toString() + " was pressed");
        }
    });
}

@Override
public int getItemCount() {
    return cursor.getCount();
}

}