无法访问RecyclerView中的元素

时间:2017-11-23 14:49:56

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

我已经实施了recyclerview来显示来自webservice的项目列表。为了显示我使用了一行的元素,使用适配器将其输入到recyclerview中。现在我想引用单行中的按钮,但我无法做到。我尝试过使用getAdapterPosition(),但这会返回我尝试访问的行的位置,而不是行中的按钮。

适配器:

public class FruitsandVegAdapter extends RecyclerView.Adapter<FruitsandVegAdapter.Holder>{

private List<FruitsandVEGmodel> mFF;

LinearLayout buttons;
CustomItemClickListener listener;


public FruitsandVegAdapter(CustomItemClickListener listener){
    this.listener=listener;
    mFF=new ArrayList<>();
}

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

    View row= LayoutInflater.from(parent.getContext()).inflate(R.layout.ffsingle_item,parent,false);
    final Holder mViewholder=new Holder(row);

   /* row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(v,mViewholder.getLayoutPosition());
        }
    });*/
    //subtract.setVisibility(View.GONE);
    //buttons=row.findViewById(R.id.buttons);
 //  buttons.setOnItemClickListener(){};


    /*addButton.setOnItemClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addButton.setVisibility(SurfaceView.GONE);
            quantity.setVisibility(SurfaceView.VISIBLE);


        }
    });*/



   return mViewholder;

}

@Override
public void onBindViewHolder(Holder holder, final int position) {

    FruitsandVEGmodel currentFF=mFF.get(position);

    holder.FFname.setText(currentFF.getProd_name());
    holder.FFmrp.setText("MRP:"+Double.toString(currentFF.getV_Mrp()));
    holder.FFrate.setText("RATE:"+Double.toString(currentFF.getV_Rate()));
    holder.FFmrp.setPaintFlags(holder.FFmrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    String IMG_URl="http://www.myfiscus.in/images/"+currentFF.getDept()+currentFF.getV_Smallimage();
    Picasso.with(holder.itemView.getContext()).load(IMG_URl).into(holder.FFphoto);
   /* holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("position",position+"");

        }
    });*/

}

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

public void addFF(FruitsandVEGmodel fruitsandVEGmodel) {
    mFF.add(fruitsandVEGmodel);
    notifyDataSetChanged();

}

public class Holder extends RecyclerView.ViewHolder {

    private ImageView FFphoto;
    private TextView FFname;
    private TextView FFmrp;
    private TextView FFrate;
    Button addButton;

    public Holder(final View itemView) {
        super(itemView);
        FFphoto=itemView.findViewById(R.id.FFphoto);
        FFname=itemView.findViewById(R.id.FFname);
        FFmrp=itemView.findViewById(R.id.FFmrp);
        FFrate=itemView.findViewById(R.id.FFrate);
        addButton= itemView.findViewById(R.id.addButton);
       itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getAdapterPosition()!=RecyclerView.NO_POSITION) {
                    listener.onItemClick(v, getLayoutPosition());
                }
                Log.d("Position : ",getAdapterPosition()+"");
            }
        });

    }


}

}

的活动:

public class FruitsVeg extends AppCompatActivity {

private RecyclerView recyclerView;
private RestManager mRestManager;
private FruitsandVegAdapter mfruitsandVegAdapter;
int minteger;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fruits_veg);

    configview();

    mRestManager=new RestManager();
    Call<List<FruitsandVEGmodel>> listCall =mRestManager.getmFFapi().getAllFruitsandvegs();
    // Set up progress before call
    final ProgressDialog progressDoalog;
    progressDoalog = new ProgressDialog(FruitsVeg.this);
    progressDoalog.setMax(100);
    progressDoalog.setMessage("Its loading....");
    progressDoalog.setTitle("ProgressDialog bar example");
    progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    // show it
    progressDoalog.show();

    listCall.enqueue(new Callback<List<FruitsandVEGmodel>>() {
        @Override
        public void onResponse(Call<List<FruitsandVEGmodel>> call, Response<List<FruitsandVEGmodel>> response) {
            if(response.isSuccessful()){
                List<FruitsandVEGmodel> ffList =response.body();
                for(int i=0;i< ffList.size();i++)
                {
                    FruitsandVEGmodel fruitsandVEGmodel=ffList.get(i);
                    mfruitsandVegAdapter.addFF(fruitsandVEGmodel);
                }

            }
            progressDoalog.dismiss();
        }

        @Override
        public void onFailure(Call<List<FruitsandVEGmodel>> call, Throwable t) {
            progressDoalog.dismiss();

        }
    });
}

private void configview() {
    recyclerView=findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));

    mfruitsandVegAdapter=new FruitsandVegAdapter(new CustomItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            Log.d("TAG", "clicked position:" + position);
           final Button addBUtton=v.findViewById(R.id.addButton);
            final LinearLayout quantity=v.findViewById(R.id.quantity);
           //addBUtton.setText("haha");
          // final Button subtract=v.findViewById(R.id.subtract);
          //quantity.setVisibility(View.GONE);
          /* addBUtton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addBUtton.setVisibility(View.GONE);
                    quantity.setVisibility(View.VISIBLE);
                }
            });*/


        }
    });
    recyclerView.setAdapter(mfruitsandVegAdapter);
}

1 个答案:

答案 0 :(得分:0)

使用以下代码作为适配器,它现在可以使用

public class FruitsandVegAdapter extends RecyclerView.Adapter<FruitsandVegAdapter.Holder>{

private List<FruitsandVEGmodel> mFF;

LinearLayout buttons;
CustomItemClickListener listener;


public FruitsandVegAdapter(CustomItemClickListener listener){
    this.listener=listener;
    mFF=new ArrayList<>();
}

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

    View row= LayoutInflater.from(parent.getContext()).inflate(R.layout.ffsingle_item,parent,false);
    final Holder mViewholder=new Holder(row);

   /* row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(v,mViewholder.getLayoutPosition());
        }
    });*/
    //subtract.setVisibility(View.GONE);
    //buttons=row.findViewById(R.id.buttons);
 //  buttons.setOnItemClickListener(){};


    /*addButton.setOnItemClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addButton.setVisibility(SurfaceView.GONE);
            quantity.setVisibility(SurfaceView.VISIBLE);


        }
    });*/



   return mViewholder;

}

@Override
public void onBindViewHolder(Holder holder, final int position) {

    FruitsandVEGmodel currentFF=mFF.get(position);

    holder.FFname.setText(currentFF.getProd_name());
    holder.FFmrp.setText("MRP:"+Double.toString(currentFF.getV_Mrp()));
    holder.FFrate.setText("RATE:"+Double.toString(currentFF.getV_Rate()));
    holder.FFmrp.setPaintFlags(holder.FFmrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    String IMG_URl="http://www.myfiscus.in/images/"+currentFF.getDept()+currentFF.getV_Smallimage();
    Picasso.with(holder.itemView.getContext()).load(IMG_URl).into(holder.FFphoto);
  holder.addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("Position : ",position+"");
            }
        });


}

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

public void addFF(FruitsandVEGmodel fruitsandVEGmodel) {
    mFF.add(fruitsandVEGmodel);
    notifyDataSetChanged();

}

public class Holder extends RecyclerView.ViewHolder {

    private ImageView FFphoto;
    private TextView FFname;
    private TextView FFmrp;
    private TextView FFrate;
    Button addButton;

    public Holder(final View itemView) {
        super(itemView);
        FFphoto=itemView.findViewById(R.id.FFphoto);
        FFname=itemView.findViewById(R.id.FFname);
        FFmrp=itemView.findViewById(R.id.FFmrp);
        FFrate=itemView.findViewById(R.id.FFrate);
        addButton= itemView.findViewById(R.id.addButton);


    }


}

}