设置并获取回收站视图的项目ID

时间:2016-12-06 07:19:03

标签: android android-recyclerview

我是RecyclerView的新手。我们可以在RecyclerView中获得点击项目位置,但是我们可以设置并获取RecyclerView的项目ID吗?因为在我的RecyclerView中,该项目具有服务器提供的自定义ID。我试过这段代码:

FragmentListProduct.java

productData = new JSONArray();
linearLayoutManager = new LinearLayoutManager(fragmentActivity);
lpAdapter = new ListProductAdapter(fragmentActivity, R.layout.list_product_grid, productData, null,
        new ListProductHolder.OnProductClickListener() {
            @Override
            public void onProductClick(View v, long id) {
                Toast.makeText(fragmentActivity, "id:"+id, Toast.LENGTH_SHORT).show();
                MainActivity.setIdToRead((int) id);
                MainActivity.replaceFragment(FragmentListProduct.this, new FragmentViewProduct());
            }
        });
rvProductList = (RecyclerView) mainView.findViewById(R.id.rvProductList);
rvProductList.setLayoutManager(linearLayoutManager);
rvProductList.setAdapter(lpAdapter);
rvProductList.setHasFixedSize(true);

ListProductHolder.java

public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView tvTitle;
    TextView tvPrice;
    ImageView ivImage;
    private OnProductClickListener onProductClickListener;

    ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
        super(itemView);
        this.onProductClickListener = onProductClickListener;

        tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        onProductClickListener.onProductClick(view, getItemId());
    }

    public static interface OnProductClickListener {
        public void onProductClick(View v, long id);
    }
}

ListProdutAdapter.java

public class ListProductAdapter extends RecyclerView.Adapter<ListProductHolder> {
    private Activity activity;
    private int layoutResource;
    private JSONArray productDataArray;
    private StringSignature downloadSignature;
    private ListProductHolder.OnProductClickListener onProductClickListener;

    public ListProductAdapter(Activity activity, int layoutResource, JSONArray productDataArray, StringSignature downloadSignature, ListProductHolder.OnProductClickListener onProductClickListener) {
        this.activity = activity;
        this.layoutResource = layoutResource;
        this.productDataArray = productDataArray;
        this.downloadSignature = downloadSignature;
        this.onProductClickListener = onProductClickListener;
    }

    @Override
    public ListProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(activity).inflate(layoutResource, parent, false);
        return new ListProductHolder(v, onProductClickListener);
    }

    @Override
    public void onBindViewHolder(ListProductHolder holder, int position) {...}

    @Override
    public long getItemId(int i) {
        long itemId = -1;
        try {
            itemId = productDataArray.getJSONObject(i).getInt("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return itemId;
    }
}

但是当我尝试它时,ID总是-1。怎么解决这个?或者我可以做另一种解决方案吗?

3 个答案:

答案 0 :(得分:4)

在on bind视图持有者中添加click侦听器,因为视图将被重用,您必须为每个重用的视图设置click。将单击侦听器添加到根布局

@Override
public void onBindViewHolder(ListProductHolder holder, int position) {
    final long itemId = productDataArray.getJSONObject(position).getInt("id");

    holder.rootLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onProductClickListener.onProductClick(view, itemId);
        }
    });
}

答案 1 :(得分:0)

您可以使用

itemView.setTag(your_id);

和onClick你需要使用

view.getTag();

检查以下示例

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.LogHolder>
{
    public RecyclerAdapter()
    {
    }

    @UiThread
    protected  void dataSetChanged()
    {
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        //you can pass ur server response array here
        //int count = (csMainJsonArr != null) ? csMainJsonArr.length() : 0;
        int count = 20;
        return count;
    }


    public class LogHolder extends RecyclerView.ViewHolder
    {
        TextView    nameTxt             = null;
        TextView    contactTxt          = null;
        TextView    statusTxt           = null;
        ImageView   listImg             = null;

        public LogHolder(View itemView)
        {
            super(itemView);

                nameTxt     = (TextView)itemView.findViewById(R.id.custom_NameTxt);
                contactTxt  = (TextView)itemView.findViewById(R.id.custom_ContactTxt);
                statusTxt   = (TextView)itemView.findViewById(R.id.custom_StatusTxt);
                listImg     = (ImageView)itemView.findViewById(R.id.custom_listImg);
        }
    }

    @Override
    public LogHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LogHolder holder = new LogHolder(inflater.inflate(R.layout.custom_design, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(LogHolder holder, int position)
    {
        //just get data from your json arrahy and add here
        holder.nameTxt.setText("name "+position);
        holder.contactTxt.setText("contact "+position);
        holder.statusTxt.setText("status "+position);

        //you need to set tag for holder image and holder item to get position of item
        holder.listImg.setTag(position);
        holder.itemView.setTag(position);


        holder.listImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //create your own action
                // using view.getTag() you can get the position of item clicked

                int position = (int) view.getTag();
                Log.d("=======", String.valueOf(position));
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //create your own action
                //using view.getTag() you can get the position of item clicked

                int position = (int) view.getTag();
                Log.d("=======", String.valueOf(position));
            }
        });

    }

}

并且不要忘记宣布这一点。

 csRecyclerView  = (RecyclerView) findViewById(R.id.recyclerView);
    csAdapter = new RecyclerAdapter();
    LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
    //manager.setReverseLayout(true);
    //manager.setStackFromEnd(true);
    csRecyclerView.setLayoutManager(manager);
    csRecyclerView.setAdapter(csAdapter);

答案 2 :(得分:0)

方法getItemId() int ListProductAdapter是否有其他代码?与getItemId(int i)中的ListProductAdapter相同。在我看来,您需要做的只是将getItemId()替换为productDataArray.getJSONObject(getLayoutPosition()).getInt("id")

public class ListProductHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvTitle;
TextView tvPrice;
ImageView ivImage;
private OnProductClickListener onProductClickListener;

ListProductHolder(View itemView, OnProductClickListener onProductClickListener) {
    super(itemView);
    this.onProductClickListener = onProductClickListener;

    tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
    ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
    tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    onProductClickListener.onProductClick(view, productDataArray.getJSONObject(getLayoutPosition()).getInt("id"));
}

public static interface OnProductClickListener {
    public void onProductClick(View v, long id);
}

}