如果项目被扭转,请获取回收站视图中项目的内部详细信息

时间:2017-02-20 20:05:15

标签: android onclick android-recyclerview adapter

我有一个回收站视图,它由列表组成。列表中的项目有自己的详细信息(每个项目都有图像,ID,名称,描述),项目是双重的,而不是按顺序。

示例:recyclerview item position = 0,list item id = 7。

recyclerview item position = 1,list item id = 5。

recyclerview item position = 2,list item id = 12。

所以我需要点击再循环器上的列表项ID。如果我点击recyclerview position = 0,如果我必须得到项目ID为7.这样我就可以根据该ID工作。

我的示例代码是

    recommendedrecyclerView=(RecyclerView)view.findViewById(R.id.recommended_recycler_view);
    recommendedrecyclerView.setNestedScrollingEnabled(false);
    recommendedrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),OrientationHelper.VERTICAL,true));
    recommendedrecyclerView.setItemAnimator(new DefaultItemAnimator());

我从服务器获取数据并将其设置为适配器

  setrecommendedAppsContent(response.body().getData());

    public void setrecommendedAppsContent(List<FeaturedAppsData> data){
    if (data!=null&&data.size()>0) {
        recommendedAdapter = new RecommendedAdapter(mCurrentContext, data);
        recommendedrecyclerView.setAdapter(recommendedAdapter);
        recommendedAdapter.setClickListener(this);
    }
}

这是我的适配器类

public class RecommendedAdapter extends RecyclerView.Adapter<RecommendedAdapter.ItemViewHolder> {

private LayoutInflater inflater = null;
private Context context;
private List<FeaturedAppsData> itemsData;
private ClickListener clicklistener = null;
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription;
public ImageView recommendedAppIcon;
Button recommendedBtn;
String appId;

public void setClickListener(ClickListener clickListener) {
    this.clicklistener = clickListener;
}

public RecommendedAdapter(Context context, List<FeaturedAppsData> itemsData) {
    this.context = context;
    this.itemsData = itemsData;
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    holder.recommendedAppTitle.setText(itemsData.get(position).getName());
    holder.recommendedAppCategory.setText(itemsData.get(position).getApp_description());
    holder.recommendedAppDescription.setText(itemsData.get(position).getOffer_description());
    holder.recommendedAppBtn.setText(itemsData.get(position).getButton_text());
    String imageUrl = String.valueOf(itemsData.get(position).getImage().getUrl());
    Glide.with(context).load(ApiConstant.ApiBaseUrl + imageUrl).into(recommendedAppIcon);
    appId=itemsData.get(position).getId();

}

@Override
public int getItemCount() {
    int size = 0;
    if (itemsData != null && itemsData.size() > 0) {
        size = itemsData.size();

    }
    return size;
}

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

    public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
    public ImageView recommendedAppIcon;
    public Button recommendedAppBtn;
    public ArrayList<FeaturedAppsData> dataItems;
    private Context context;

    public ItemViewHolder(Context context, View itemView, int viewType) {
        super(itemView);
        this.context = context;
        itemView.setOnClickListener(this);

        recommendedAppTitle = (TextView) itemView.findViewById(R.id.recommended_app_title);
        recommendedAppCategory = (TextView) itemView.findViewById(R.id.recommended_app_category);
        recommendedAppDescription = (TextView) itemView.findViewById(R.id.recommended_app_description);
        recommendedAppIcon = (ImageView) itemView.findViewById(R.id.recommended_app_icon);
        recommendedAppBtn=(Button)itemView.findViewById(R.id.recommended_app_card_btn);
    }

    @Override
    public void onClick(View v) {

        if (clicklistener != null) {
            clicklistener.itemClicked(v, getAdapterPosition());
        }
    }
}


@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;
    inflater = LayoutInflater.from(context);
    view = inflater.inflate(R.layout.recommended_item_layout, parent, false);
    recommendedAppIcon=(ImageView)view.findViewById(R.id.recommended_app_icon);

    return new ItemViewHolder(context, view, viewType);
}

}

我不确定Onclick方法。因此,请根据需要和Onclick活动一起建议我。

1 个答案:

答案 0 :(得分:1)

ViewHolder中的setOnClickListener是一个好方法。

考虑到您当前的解决方案,最简单的方法是更改​​ClickListener接口,在onBindViewHolder期间将数据放在Holder中,然后在onClick期间将其传递给侦听器。像这样:

将视图持有者字段更改为:

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

    public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
    public ImageView recommendedAppIcon;
    public Button recommendedAppBtn;
    public FeaturedAppsData data; // <<< ADD THIS 
    // remove that u don't need public ArrayList<FeaturedAppsData> dataItems;
    // remove that u don't need private Context context;
    ... the rest of your holder

然后在onBindViewHolder

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    holder.data = itemsData.get(position);
    ... the rest on bind

然后在onClick内,在持有人内:

@Override
public void onClick(View v) {

    if (clicklistener != null) {
        clicklistener.itemClicked(v, data.getId());
    }
}