テーブルの行を押したときに強調表示したい。ただし、この機能を実装すると、次のようになります。テーブルをスクロールすると(スクロールするだけで、テーブルの行を押すことは意図されていません)、システムはそれをtable-row-pressとして解釈し、行を強調表示します。テーブルをスクロールしない場合にのみシステムが行を強調表示するようにするにはどうすればよいですか?
サンプルプロジェクトを作成しました。これは、クラス定義、XMLレイアウト、およびテーブル行が押されたときに何をするかを指定するXMLファイルの3つのファイルで構成されています。はい、どうぞ:
クラス定義
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 / layout / 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>
3番目のres/drawable / 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>
テーブルをスクロールするときにシステムが行を強調表示しないようにするにはどうすればよいですか?