在适配器中为RecyclerView获取itemView的特定位置

时间:2016-08-26 08:39:49

标签: android android-recyclerview recycler-adapter recyclerview-layout

我在我的适配器中获取itemView以查找RecyclerView中的preselect元素时遇到问题。 在我的活动中,我有:

    recyclerView = (RecyclerView) findViewById(R.id.activityRecicler);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new CustomRecycleAdapter(array, ReceiptNewActivity.this, arrayJson);
    recyclerView.setAdapter(adapter);
    int adapterLength = adapter.getItemCount();
    int length = arrayJson.length();
    for(int i = 0; i<adapterLength; i++){
        for(int j=0; j<length; j++){
            try {
                if(adapter.getItem(i).getInt("id") == arrayJson.getJSONObject(j).getInt("id")){

                    int value= arrayJson.getJSONObject(j).getInt("value");

                    float valueFloat = (float) value/250;
                    String valueString = String.valueOf(value);

                    //function in adapter for selected
                    adapter.setSelected(i, valueFloat);


                    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
                    View v= viewHolder.itemView;

                    adapter.setRowData(v, valueString);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

我的问题是 RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);返回始终为null。 那么如何才能获得itemView的位置?

0 个答案:

没有答案