表格中的Android显示不可见视图

时间:2013-09-30 11:38:15

标签: android android-tablelayout

嘿,在我的代码中,我有一个表,它是由我调用的方法动态填充的。添加时的表格行有一个不可见的视图,我想为每个特定的行显示它的点击,而其他所有的行都保持隐藏在这里是我的代码

public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
    final TableRow tr1 = new TableRow(getApplicationContext());
    tr1.setId(id);
    tr1.setClickable(true);

    tr1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        for(int i = 0;i<lytTableData.getChildCount();i++){
        tr1.getChildAt(4).setVisibility(View.INVISIBLE);
        }
        tr1.getChildAt(4).setVisibility(View.VISIBLE);
        }

    });

    tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    EditText textViewItemQuantity;
    TextView textViewItemName,textViewItemPrice,textViewItemAmount;

    textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
    iv2 = new ImageView(this);
    textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
    textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
    textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);  
    textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
    textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
    textViewItemQuantity.setBackgroundColor(android.R.color.transparent);
    iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    iv2.setImageResource(R.drawable.delete);
    iv2.setVisibility(View.INVISIBLE);
    textViewItemQuantity.setOnClickListener(editTextRows);
    tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
    lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    Double intItemAmount = Double.parseDouble(itemAmount);
    intTotalAmount = intTotalAmount + intItemAmount;
    intTotalAmount2 = intTotalAmount2 + intItemAmount;
    txtTotal.setText(String.valueOf(intTotalAmount));
    //txtDiscount.setText("0");
    txtAmount.setText(String.valueOf(intTotalAmount));
    id = id + 1;
        System.out.println(tr1.getId());
}

使用这种方法我每次调用它都能创建一个新行 我可以缺少什么才能使其工作只是为了清楚什么不起作用是显示它显示的隐形视图但不是我想要的方式。我希望它显示在单击的行上,所有其余部分保持不可见,当单击另一行时,它会显示,所有其余行保持不可见

2 个答案:

答案 0 :(得分:0)

您可以通过在列表视图的行布局文件中定义动态布局(您希望在单击时显示),默认情况下使其保持不可见状态并将其标记为onItemClickListener事件上的单击行可见列表显示。 希望这能帮助你。

如果您使用的是基础适配器,那么解决此问题会更好。

答案 1 :(得分:0)

好吧,我终于能够理解这一切,感谢所有尝试过的人,这就是答案

public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
    final TableRow tr1 = new TableRow(getApplicationContext());
    tr1.setId(id);
    tr1.setClickable(true);

    tr1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        TableRow vi = (TableRow) lytTableData.getChildAt(lytTableData.indexOfChild(tr1));
        for(int i = 0;i<lytTableData.getChildCount();i++){
            TableRow vi2 = (TableRow) lytTableData.getChildAt(i);
            vi2.getChildAt(4).setVisibility(View.INVISIBLE);
            }       
        vi.getChildAt(4).setVisibility(View.VISIBLE);
        }

    });

    tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    TextView textViewItemName,textViewItemPrice;

    textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
    iv2 = new ImageView(this);
    textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
    textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
    textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);  
    textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
    textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
    textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
    textViewItemQuantity.setBackgroundColor(0x0000000000);
    iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    iv2.setImageResource(R.drawable.delete);
    iv2.setVisibility(View.INVISIBLE);
    iv2.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            lytTableData.removeView(tr1);
        }

    });
    tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
    lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    Double intItemAmount = Double.parseDouble(itemAmount);
    intTotalAmount = intTotalAmount + intItemAmount;
    //intTotalAmount2 = intTotalAmount2 + intItemAmount;
    txtTotal.setText(String.valueOf(intTotalAmount));
    //txtDiscount.setText("0");
    txtAmount.setText(String.valueOf(intTotalAmount));
    id = id + 1;
        System.out.println(tr1.getId());
}