android-如何从Reyclerview中的项目启动活动?

时间:2016-02-16 07:52:51

标签: android-fragments android-intent android-recyclerview

我正在尝试使用OnClick方法从recyclerview的项目启动活动。我创建了一个接口并在适配器中使用了getAdapterPostion。但活动没有启动。这是我的代码:

我在适配器中创建的界面:

 public interface Clicklistener{
    public void itemClicked(View view,int position);
}

}

ClickListener的Setter方法:

public void setclickListener(Clicklistener clickListener){

this.clickListener = clickListener;
}

查看Holder方法,我在其中设置OnClickListener:

public   class ViewHolderListMovies extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageView movieThumbnail;
    private TextView movieTitle;
    private TextView movieYear;





public ViewHolderListMovies(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            movieThumbnail = (ImageView) itemView.findViewById(R.id.video_poster);
            movieTitle = (TextView) itemView.findViewById(R.id.video_name);
            movieYear = (TextView) itemView.findViewById(R.id.video_year);

        }


        @Override
        public void onClick(View v) {
            if(clickListener!=null){
                clickListener.itemClicked(v,getAdapterPosition());

            }

        }

我初始化片段中的接口:

listMoviesAdapter.setclickListener(this);

最后,我开始在片段中调用所需的Activity:

 @Override
public void itemClicked(View view, int position) {
    startActivity(new Intent(getActivity(),MovieDetailsActivity.class));

}

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

在方法onBindViewHolder中初始化setoncliklistener。例如,可能会有帮助

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

private Context mContext;
private List<String> mDataSet;
private List<String> mIdevent;


public HistoryAdapter(Context context, List<String> dataSet, List<String> idevent, List<String> askPrice, 
        List<String> bidPrice, List<String> Profit) {

    mContext = context;
    mDataSet = dataSet;
    mIdevent = idevent;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      

    View v = LayoutInflater.from(mContext)
            .inflate(R.layout.layout_list_history, parent, false);
    return new ViewHolder(v);

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Picasso.with(mContext).load(R.drawable.add2new).into(holder.image);
    holder.text.setText(mDataSet.get(position));


    holder.setClickListener(new HistoryAdapter.ViewHolder.ClickListener() {
        public void onClick(View v, int pos, boolean isLongClick) {
            if (isLongClick) {

                // View v at position pos is long-clicked.
                String poslx = pos + "";
                String menax = mDataSet.get(pos);
                Toast.makeText(mContext, "longclick pos. " + poslx + " pair " + menax, Toast.LENGTH_SHORT).show();

            } else {
                // View v at position pos is clicked.
                //String possx = pos + "";
                String poslx = pos + "";
                String event2 = mDataSet.get(pos); 
                String id2 = mIdevent.get(pos);
                Toast.makeText(mContext, "shortclick pos. " + poslx + " pair " + event2, Toast.LENGTH_SHORT).show();
                //toggleSelection(pos);

                Intent i = new Intent(mContext, HistoryCandlesActivity.class);
                Bundle extras = new Bundle();
                extras.putString("eventx", event2);
                extras.putString("idx", id2);
                extras.putInt("whatspage", 0);
                i.putExtras(extras);
                v.getContext().startActivity(i);


            }
        }
    });

}



@Override
public int getItemCount() {
    return mDataSet.size();
}

public void remove(int position) {
    mDataSet.remove(position);
    notifyItemRemoved(position);
}

public void add(String text, int position) {
    mDataSet.add(position, text);
    notifyItemInserted(position);
}

static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public ImageView image;
    public TextView text;
    private ClickListener clickListener;

    public ViewHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.image);
        text = (TextView) itemView.findViewById(R.id.text);

        // We set listeners to the whole item view, but we could also
        // specify listeners for the title or the icon.
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);

    }


    /* Interface for handling clicks - both normal and long ones. */
    public interface ClickListener {

        /**
         * Called when the view is clicked.
         *
         * @param v view that is clicked
         * @param position of the clicked item
         * @param isLongClick true if long click, false otherwise
         */
        public void onClick(View v, int position, boolean isLongClick);

    }

    /* Setter for listener. */
    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onClick(View v) {

        // If not long clicked, pass last variable as false.
        clickListener.onClick(v, getPosition(), false);
    }

    @Override
    public boolean onLongClick(View v) {

        // If long clicked, passed last variable as true.
        clickListener.onClick(v, getPosition(), true);
        return true;
    }


  }
}