滚动表格时表格行突出显示
我希望当我按下表中的行时突出显示它们。但是,我对此功能的实现导致以下结果:当我滚动表格(只是滚动,我不打算按表格行)时,系统将其解释为表格行按下并突出显示该行。 如何让系统仅在不滚动表格时突出显示行?
我制作了示例项目,它由 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/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>
第三个,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>
如何使系统不突出显示当我滚动表格时行?
I want rows in my table to be highlighted when I press them. However, my implementation of this feature leads to the following: when I scroll the table (just scroll, I'm not intended to press table rows), system interprets it as table-row-press and highlights the row.
How to make system highlight row only when I don't scroll the table?
I've made example project, it consists of 3 files: Class definition, XML layout and XML file where it is specified what to do when table row is pressed.
Here they are:
Class Definition
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 layout: 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>
3rd one, res/drawable/table_row.xml, it's specified here that table rows should be white by default and dark when pressed:
<?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>
How to make system not highlight the row when I scroll the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您尝试执行此操作的方式不是正确的方式。
为什么不尝试使用 ListView 而不是 ScrollView 中的 Table ?困难的滚动/选择处理已经完成并且满足您的需求......
Maybe the way you're trying to do this is not the right way.
Why didn't you try to use ListView instead of a Table inside a ScrollView ? The difficult scrolling/selection handling is already done and fits your needs...