ScrollView中的值无法从RecyclerAdapter访问Fragment

时间:2018-05-07 06:28:31

标签: android android-recyclerview recycler-adapter

我在Recyclerview的每个子行中有两个buttons和一个textview。这两个按钮用作触发器来计算数字,然后在textview中写入数字。

问题:滚动时未更新文本视图值,而item-count仅显示列表中可见的项目数。

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

private LayoutInflater inflater;
private Context ctx;

public RecyclerAdapterItem(Context ctx) {

    inflater = LayoutInflater.from(ctx);
    this.ctx = ctx;
}

@Override
public RecyclerAdapterItem.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.recyclerviewitem_item_tab, parent, false);
    ItemViewHolder holder = new ItemViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(final RecyclerAdapterItem.ItemViewHolder holder, int position) {
    DecimalFormat df = new DecimalFormat("#.###");
    df.setRoundingMode(RoundingMode.CEILING);
    holder.Itemname.setText(ItemFragment.itemArrayList.get(position).getItem_name().toString());
    Float  rt=Float.valueOf(ItemFragment.itemArrayList.get(position).getRate());
    holder.Rate.setText(String.valueOf(df.format(rt)));
    holder.qty.setText(String.valueOf(ItemFragment.itemArrayList.get(position).getQty()));


}

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

class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    protected Button btn_plus, btn_minus;
    private TextView Itemname,Rate, qty;

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

        Itemname = (TextView) itemView.findViewById(R.id.Itemname);
        Rate=(TextView) itemView.findViewById(R.id.Rate);
        qty = (TextView) itemView.findViewById(R.id.Qty);
        btn_plus = (Button) itemView.findViewById(R.id.plus);
        btn_minus = (Button) itemView.findViewById(R.id.minus);

        btn_plus.setTag(R.integer.btn_plus_view, itemView);
        btn_minus.setTag(R.integer.btn_minus_view, itemView);
        btn_plus.setOnClickListener(this);
        btn_minus.setOnClickListener(this);

    }

    // onClick Listener for view
    @Override
    public void onClick(View v) {

        if (v.getId() == btn_plus.getId()){

            View tempview = (View) btn_plus.getTag(R.integer.btn_plus_view);
            TextView tv = (TextView) tempview.findViewById(R.id.Qty);
            int number = Integer.parseInt(tv.getText().toString()) + 1;
            tv.setText(String.valueOf(number));
            ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));

        } else if(v.getId() == btn_minus.getId()) {

            View tempview = (View) btn_minus.getTag( R.integer.btn_minus_view);
            TextView tv = (TextView) tempview.findViewById(R.id.Qty);
            int number = Integer.parseInt(tv.getText().toString()) - 1;
            if(number<0)
                number =0;
            tv.setText(String.valueOf(number));
            ItemFragment.itemArrayList.get(getAdapterPosition()).setQty(String.valueOf(number));
        }
    }

}
}

这是recycleradapter代码。提到的textviewqty

2 个答案:

答案 0 :(得分:0)

如果您的代码正常运行,则只尝试此方法并尝试检查更新..

notifyDataSetChanged();

其他方面使得脉冲和分钟的接口值更新如下代码..

    onItemClickListner onItemClickListner;

public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(Item str);//pass your object types.
}
    @Override
    public void onBindViewHolder(final DataObjectHolder holder, final int position)
    {
        Item item=mDataset.get(position);
        holder.setIsRecyclable(false);
        holder.itemid.setText(mDataset.get(position).getItem_id());
        holder.itemname.setText(mDataset.get(position).getItem_name());
        holder.qty.setText(mDataset.get(position).getQty());
        int  rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
        holder.rate.setText(formatter.format(rt)+".00");
        ItemDetails listItem = filterList.get(position);
        holder.itemname.setText(listItem.getItem_name());
        holder.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.setPlus(true);
                onItemClickListner.onClick(data);
            }
        });
        holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.setMinus(true);
                onItemClickListner.onClick(data);
            }
        });

    }

然后在定义适配器对象并绑定到recylcer视图后,调用下面的代码..

       recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
       @Override
       public void onClick(Item str) {
           if (str.getPlus()){
               str.setQty();// set your logic
               recyclerViewAdpater.notifyDataSetChanged();
           }
           if (str.getMinus()){
               str.setQty();// set your logic
               recyclerViewAdpater.notifyDataSetChanged();
           }
       }
   });

答案 1 :(得分:0)

将按钮的单击侦听器移动到BindView方法

import UIKit

protocol ASubCellDelegate {
    // CollectionView Cell Pressed
    func onSubCellAtcolectionBPressed(_item : YourData)
}
class ASubCell: UICollectionViewCell {

    @IBOutlet weak var collectionViewB : UICollectionView!
     var delegate : ASubCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        // setup your Collection View B
    }

}


// set up your horizontal Collection View
extension UICollectionViewCell:UICollectionViewDelegate,UICollectionViewDataSource {
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell  =  collectionView.dequeueReusableCell(withReuseIdentifier: "YourBCellID", for: indexPath) as! YourBCellID
        return cell
    }
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if delegate != nil {
            self.onSubCellAtcolectionBPressed(YourData)
        }
    }

}