gpt4 book ai didi

android - RecyclerView 项目 View 隐藏/显示

转载 作者:太空狗 更新时间:2023-10-29 14:03:42 24 4
gpt4 key购买 nike

我想实现 reclyerview 的行为如下默认情况下,项目将为灰色,另一个按钮隐藏,直到按钮添加点击:

如果展开任何其他项目,则每个列表都会展开一个项目单击下一个项目将首先关闭任何打开的项目,然后打开新项目:

我已尝试实现它,但每次单击一个项目并发生扩展时,即使单击加号按钮会增加其数量,列表中的其他项目也不会扩展。

此外,扩展可以出现在所有项目上,而在引用上,每次点击只会扩展一个项目。

这是我的代码

public class SalesProductsAdapter extends  

RecyclerView.Adapter<SalesProductsAdapter.Vh>
implements
TextWatcher,Filterable,View.OnClickListener,AdapterView.OnItemClickListener
{

public List<SalesProductsItems> mItems = new ArrayList<SalesProductsItems>();
public static List<SalesProductsItems> filteredIt = new ArrayList<SalesProductsItems>();

@Override
public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.selling_screen_sellitems, parent, false);

Vh vh = new Vh(v);
return vh;
}

@Override
public void onBindViewHolder(final Vh holder, final int position) {
final SalesProductsItems cl = filteredIt.get(position);
if(pitems != null && pitems.size() > 0){
for(int j = 0; j < pitems.size(); j++){
Pending_Items _pitems = pitems.get(j);
long proid = cl.getProdid();
long _proid = _pitems.getProdloc();
if(proid == _proid){
prodname = cl.getProduct();
qty[0] = new BigDecimal(_pitems.getQty()).intValue();
unit[0] = _pitems.getPrice();
} else {
prodname = cl.getProduct();
qty[0] = new BigDecimal(cl.getQuantity()).intValue();
unit[0] = cl.getUnit();
}
}
} else {
prodname = cl.getProduct();
qty[0] = new BigDecimal(cl.getQuantity()).intValue();
unit[0] = cl.getUnit();
}
items = pitems.size();
if(items > 0)
imgnext.setVisibility(View.VISIBLE);
else
imgnext.setVisibility(View.GONE);

holder.txtproduct.setText(prodname);
updateViews(qty[0], holder);

if(qty[0] > 0){
System.out.println(" qty is " + s_qty);
holder.imgminus.setVisibility(View.GONE);
holder.imgadd.setVisibility(View.GONE);
holder.txtqty.setText(s_qty[0]);
holder.txtunit.setText(s_unit);
holder.txtsubtotal.setText(s_subtotal[0]);
} else {
holder.imgminus.setVisibility(View.GONE);
holder.txtqty.setVisibility(View.GONE);
holder.txtunit.setText(s_unit);
holder.txtsubtotal.setVisibility(View.GONE);
}

holder.imgadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
qty[0] += 1;
if(Products.checkQty(context,cl.getProdid(),qty[0])) {
if (qty[0] == 1) {
s_qty[0] = String.valueOf(qty[0]);

holder.txtqty.setText(s_qty[0]);
holder.txtqty.setVisibility(View.VISIBLE);
holder.imgminus.setVisibility(View.VISIBLE);

process_item(cl, holder,position);
changeBg(holder,qty[0]);

holder.txtsubtotal.setVisibility(View.VISIBLE);
} else {
s_qty[0] = String.valueOf(qty[0]);
holder.txtqty.setText(s_qty[0]);
process_item(cl, holder,position);
holder.txtsubtotal.setVisibility(View.VISIBLE);
}
} else {
Toast.makeText(context,cl.getProduct() + " " + context.getResources().getString(R.string
.strsalesquantityerror1) +
" " + Products.getItemOnHand(context,String.valueOf(cl.getProdid())) + " " + context.getResources()
.getString(R.string.strsalesquantityerror2), Toast.LENGTH_LONG).show();
}
}
});

holder.layview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.imgminus.getVisibility() == View.VISIBLE){
holder.imgminus.setVisibility(View.GONE);
if(qty[0] == 0) {
holder.imgadd.setVisibility(View.VISIBLE);
holder.txtqty.setVisibility(View.GONE);
}
else {
holder.txtqty.setVisibility(View.VISIBLE);
holder.imgadd.setVisibility(View.GONE);
holder.txtqty.setText(s_qty[0]);
}
} else {
Intent intent = new Intent(context,SingeItem.class);
intent.putExtra(Constants.SOURCE,Constants.SALETYPE_SALE);
intent.putExtra(Products.PRODUCTNAME,cl.getProdid());
intent.putExtra(Products.ID, position);
context.startActivity(intent);
}
}
});

holder.txtqty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.imgadd.getVisibility() == View.GONE){
holder.txtqty.setText(s_qty[0]);
holder.imgadd.setVisibility(View.VISIBLE);
holder.imgminus.setVisibility(View.VISIBLE);
}
}
});

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return filteredIt.size();
}

@Override
public long getItemId(int arg0){
return 0;
}


private void updateViews(int qt, Vh holder){
double un = unit[0];
double st = new BigDecimal(qt).multiply(new BigDecimal(un)).doubleValue();
s_qty[0] = String.valueOf(qt);
s_subtotal[0] = new BigDecimal(st).setScale(2, RoundingMode.HALF_UP).toString();

s_subtotal[0] = currency + " " + new BigDecimal(subtotal[0]).setScale(2, RoundingMode.HALF_UP).toString();
s_unit = currency+" "+new BigDecimal(unit[0]).setScale(2, RoundingMode.HALF_UP).toString();

holder.txtsubtotal.setText(s_subtotal[0]);
holder.txtunit.setText(s_unit);
holder.txtsubtotal.setVisibility(View.VISIBLE);
if(qt > 0)
imgnext.setVisibility(View.VISIBLE);
else
imgnext.setVisibility(View.GONE);

gtotal += subtotal[0];
txtt.setText(currency + " " + new BigDecimal(gtotal).setScale(2, RoundingMode.HALF_UP).toString());
txti.setText("/ " + items + " " + context.getString(R.string.stritems));
}


}

最佳答案

1) 一次如果你只想显示/隐藏一个元素,这个可以正常工作

步骤 1) 创建一个变量并将其值设置为=-1; int currentPosition=-1;

第二步) 在绑定(bind) viewHolder 时,在 itemClick 上将 currentPosition 更新为 holder 位置

onBindViewHolder(ViewHolder v,int position)
{
view.Onclick(...{currentPosition=position;});
if(currentPosition==position)
view.setVisiblity(Visible);
else
view.setVisibility(Gone);
}

2) 如果您想在任何按钮上显示更多项目,请单击

  1. 创建2个arrayList,第一个arrayList存储总值,第二个arrayListarrayList 只显示 holder 中的少数元素;
  2. 现在单击按钮将所有元素添加到其他 arrayList 并通知。

关于android - RecyclerView 项目 View 隐藏/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com