2012-07-24 91 views
0

我想知道如何在動態表格視圖中獲取單元格單擊(例如:我想單擊第二行的第三個列表,即在2x3單元格中)事件。我知道關於行點擊。但我想要特定的單元格點擊.. 如果列表視圖包含行和列?表格佈局中的單元格單擊事件..

請幫

感謝

回答

0

您可以使用GridView對於這種UI的,而不是TableLayoutListView。在GridView中,您可以設置OnItemClickListener。閱讀關於GridView here的更多信息。

1

動態使用TableLayout可以做到這一點。

TableLayout table = new TableLayout(getApplicationContext()); 
    TableLayout.LayoutParams tab_lp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    table.setLayoutParams(tab_lp); 

    TableRow row = new TableRow(this); 

    final TextView tvProduct = new TextView(getApplicationContext()); 

    LinearLayout.LayoutParams lp_l3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (LayoutParams.WRAP_CONTENT)); 
    tvProduct.setLayoutParams(lp_l3); 
    tvProduct.setText(product); 
    tvProduct.setClickable(true); 

    row.addView(tvProduct, new TableRow.LayoutParams(1)); 
    table.addView(row, new TableLayout.LayoutParams());  

    tvProduct.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      Intent i = new Intent(Beers.this,BeerDetails.class); 
      i.putExtra("Product_Name",tvProduct.getText().toString()); 
      startActivity(i); 
     } 
    }); 

    LinearLayout linearMain = (LinearLayout) findViewById(R.id.linearmainLayout); 
    linearMain.addView(table);  
    linearMain.postInvalidate(); 
相關問題