0

我正在listview使用fragment. bottomnavigation每当listview行超出屏幕时,它的值就会重置。

我试图Recyclerview.Viewholder在我的代码中实现它显示一些错误,但我不知道如何viewholder在我的代码中实现。

public class AtcAdapter  extends ArrayAdapter<AtcObjects> {
    public AtcAdapter(@NonNull Context context, int resource, List<AtcObjects> objects) {
        super(context, resource, objects);
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
    View ListItemView=convertView;

    if(ListItemView==null) {
        ListItemView = LayoutInflater.from(getContext()).inflate(R.layout.atc_list, parent, false);

        }
    AtcObjects currentFood = getItem(position);

    TextView foodName=(TextView) ListItemView.findViewById(R.id.atcFname);
    foodName.setText(currentFood.getpName());
    TextView fPrice=(TextView) ListItemView.findViewById(R.id.atcFprice);
    fPrice.setText("Price   :"+currentFood.getpPrice()+" RS");
    TextView tvRes=(TextView) ListItemView.findViewById(R.id.tvResName);
    tvRes.setText(""+currentFood.getResName()+" ResName");
    String url=currentFood.getpIng();
    ImageView imgV=(ImageView) ListItemView.findViewById(R.id.ivAtc);
    Picasso.get().load(url).into(imgV);
    final TextView textView=(TextView) ListItemView.findViewById(R.id.atcQty);
    textView.setText(currentFood.qty+"");
    Button addBtn=(Button) ListItemView.findViewById(R.id.atc_addBtn);
  Button btnRemove=(Button) ListItemView.findViewById(R.id.atc_delete);
    btnRemove.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View view) {
                                         ((ListView) parent).performItemClick(view, position, 66);
                                     }
                                 });
           addBtn.setTag(position);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           updateQuantity(position,textView,1);
            ((ListView) parent).performItemClick(view, position, 56);

        }
    });
    Button subbtn=(Button) ListItemView.findViewById(R.id.atc_subBtn);
    subbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         updateQuantity(position,textView,-1);
            ((ListView) parent).performItemClick(view, position, 90);

        }
    });
    return ListItemView;
}


private void updateQuantity(int position, TextView edTextQuantity, int value) {

   AtcObjects products = getItem(position);
    if(value > 0)
    {
        products.qty = products.qty + 1;
    }
    else
    {
        if(products.qty > 0)
        {
            products.qty = products.qty - 1;
        }

    }
    edTextQuantity.setText(products.qty+"");
}
@Override
public int getViewTypeCount() {

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

}

4

1 回答 1

0
        You can create ViewHolder class as below:
         static class ViewHolder {
                ImageView imageView;
                TextView textView;
                int position;
            }

        and inside getView() method use below code :-
        @Override
        public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
         ViewHolder holder;

                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.view, null);
                    holder = new ViewHolder();
                    holder.textView= (TextView) convertView.findViewById(R.id.textView);
                    convertView.setTag(holder);
                }
                else {
                    holder = (ViewHolder) convertView.getTag();
                }
}
于 2019-07-24T11:13:44.557 回答