我想实现一个类似以下图片的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));
}
}