2011-12-23 98 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