2011-12-23 59 views
1

我希望表格中的行按下時突出顯示。然而,我的這個特性的實現導致了以下幾點:當我滾動表格時(只是滾動,我不打算按表格行),系統將它解釋爲表格行按下並突出顯示行。 如何僅在我不滾動表格時才使系統高亮顯示行?滾動表格時突出顯示錶格行

我做了一個示例項目,它由3個文件組成:類定義,XML佈局和XML文件,它指定按表格行時要執行的操作。 在這裏,他們是:

類定義

public class RowHighlightTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TableLayout mainTable=(TableLayout)findViewById(R.id.table); 
     Resources r=getResources(); 
     // We gonna have 25 rows in our table 
     for (int i = 0; i < 25; i++) { 
      // Set up each row 
      TableRow row=new TableRow(this); 
      row.setMinimumHeight(60); 
      row.setClickable(true); 
      row.setBackgroundDrawable(r.getDrawable(R.drawable.table_row)); 

      // Add a textView to each row to be able to distinguish them 
      TextView tv=new TextView(this); 
      tv.setText("row " + i); 
      row.addView(tv); 
      mainTable.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     } 
    } 
} 

XML佈局:RES /佈局/ main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scrollbars="none" > 

     <TableLayout 
      android:id="@+id/table" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </TableLayout> 
    </ScrollView> 

</LinearLayout> 

第三屆之一,RES /繪製/ table_row.xml,都能在這裏找到指定表格行在默認情況下應爲白色,在按下時應爲黑色:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@android:color/background_light" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/> 
    <item android:drawable="@android:color/background_dark" android:state_pressed="true"/> 

</selector> 

如何使sys當我滾動表格時,不會突出顯示該行?

回答

0

也許你試圖做到這一點的方式並不正確。

爲什麼你不嘗試使用ListView而不是ScrollView中的表?難以滾動/選擇處理已完成,並符合您的需求...

+0

我發現了一種方法來處理它自己,但是,是的,一個列表視圖會是更好的選擇... – fabb 2012-08-24 08:04:44