使用json数据填充的recyclerView中的图标弹出菜单

时间:2015-12-26 07:02:08

标签: android android-recyclerview

我用2个textViews和一个imageView创建了一个recyclerview。我还使用JSON数据填充了recyclerView。我想要实现的是,当我点击imageView时,我应该得到一个弹出菜单,然后点击弹出菜单执行某些操作。

我使用的代码如下 recyclerAdapter.java

 private List<FeedItem> feedItemList;
private Context mContext;

public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
    this.feedItemList = feedItemList;
    this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_adapter, null);

    CustomViewHolder viewHolder = new CustomViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    FeedItem feedItem = feedItemList.get(i);
    //Setting text view title
    customViewHolder.textView.setText(Html.fromHtml(feedItem.getHehw()));
    customViewHolder.textView1.setText(Html.fromHtml(feedItem.getHesub()));
    customViewHolder.imageView.setOnClickListener(clickListener);


}

@Override
public int getItemCount() {
    return (null != feedItemList ? feedItemList.size() : 0);
}

public class CustomViewHolder extends RecyclerView.ViewHolder {

    protected TextView textView,textView1;
    protected ImageView imageView;
    public CustomViewHolder(View view) {
        super(view);
        this.imageView= (ImageView) view.findViewById(R.id.thumbnail);
        this.textView = (TextView) view.findViewById(R.id.HeHw);
        this.textView1= (TextView) view.findViewById(R.id.HeSub);
    }
}
View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        CustomViewHolder holder = (CustomViewHolder) view.getTag();
        int position = holder.getPosition();

        FeedItem feedItem = feedItemList.get(position);
        Toast.makeText(mContext, feedItem.getThumbnail(), Toast.LENGTH_SHORT).show();

    }
};

MainActivity.java

ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                @Override
                public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    // retreives the specified value from the recyclerView
                    try {
                        String iID = posts.getJSONObject(position).getString("SERIAL_NUMBER");
                        Toast.makeText(getApplicationContext(), iID, Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

0 个答案:

没有答案