水平和垂直滚动的 GridView Android
Horizontal and vertical scrolling GridView Android
我正在使用 GridView 创建一个组件来创建 table 类似 Excel。在这里,我最初创建了一个 2x2 网格,稍后当用户单击一个按钮时,将添加额外的行和列。
这就是我添加新列的方法:-
int numColumns = GVParticipantTable.getNumColumns() + 1;
double numRows = Math.ceil((double) GVParticipantTable.getCount() / (double) GVParticipantTable.getNumColumns());
int totalItems = (int) numRows * numColumns;
if (participantDataList.size() > 4)
{
totalItems = totalItems - participantDataList.size();
}
for (int i = 0; i < totalItems; i++)
{
participantDataList.add("");
}
GVParticipantTable.setNumColumns(numColumns);
mAdapterParticipantTable.notifyDataSetChanged();
这很好用并添加了一个额外的列。问题是,当添加列时,先前的列会收缩以容纳新列,从而使每次添加新列时列看起来更小。我只想一次在屏幕上显示 2 列,然后让用户水平向前滚动以查看更多列。现在网格只能垂直滚动。同样,我希望在添加新行时发生这种情况,用户应该能够垂直滚动以查看更多行。
据我所知,您永远不应将 HorizontalScrollView 与 ListView 一起使用,因为 ListView 负责其自身的滚动。最重要的是,这样做会破坏 ListView 中处理大型列表的所有重要优化,因为它有效地强制 ListView 显示其整个项目列表以填充 HorizontalScrollView 提供的无限容器。
http://developer.android.com/reference/android/widget/HorizontalScrollView.html
由于您可能被迫使用二维滚动视图,您可以考虑使用这个: blog.gorges.us/2010/06/android-two-dimensional-scrollview/
的互联网存档
我没有用过这个,但这可能是一个合理的方法。
也许可以尝试将其添加到您的 XML 文件和 JAVA
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/scrollVertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
//WateverViewYouWant
</ScrollView>
</HorizontalScrollView>
和代码onCreate/onCreateView
final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener
private float mx, my, curX, curY;
private boolean started = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
curX = event.getX();
curY = event.getY();
int dx = (int) (mx - curX);
int dy = (int) (my - curY);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (started) {
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
} else {
started = true;
}
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
started = false;
break;
}
return true;
}
});
我正在使用 GridView 创建一个组件来创建 table 类似 Excel。在这里,我最初创建了一个 2x2 网格,稍后当用户单击一个按钮时,将添加额外的行和列。
这就是我添加新列的方法:-
int numColumns = GVParticipantTable.getNumColumns() + 1;
double numRows = Math.ceil((double) GVParticipantTable.getCount() / (double) GVParticipantTable.getNumColumns());
int totalItems = (int) numRows * numColumns;
if (participantDataList.size() > 4)
{
totalItems = totalItems - participantDataList.size();
}
for (int i = 0; i < totalItems; i++)
{
participantDataList.add("");
}
GVParticipantTable.setNumColumns(numColumns);
mAdapterParticipantTable.notifyDataSetChanged();
这很好用并添加了一个额外的列。问题是,当添加列时,先前的列会收缩以容纳新列,从而使每次添加新列时列看起来更小。我只想一次在屏幕上显示 2 列,然后让用户水平向前滚动以查看更多列。现在网格只能垂直滚动。同样,我希望在添加新行时发生这种情况,用户应该能够垂直滚动以查看更多行。
据我所知,您永远不应将 HorizontalScrollView 与 ListView 一起使用,因为 ListView 负责其自身的滚动。最重要的是,这样做会破坏 ListView 中处理大型列表的所有重要优化,因为它有效地强制 ListView 显示其整个项目列表以填充 HorizontalScrollView 提供的无限容器。
http://developer.android.com/reference/android/widget/HorizontalScrollView.html
由于您可能被迫使用二维滚动视图,您可以考虑使用这个: blog.gorges.us/2010/06/android-two-dimensional-scrollview/
我没有用过这个,但这可能是一个合理的方法。
也许可以尝试将其添加到您的 XML 文件和 JAVA
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/scrollVertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
//WateverViewYouWant
</ScrollView>
</HorizontalScrollView>
和代码onCreate/onCreateView
final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener
private float mx, my, curX, curY;
private boolean started = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
curX = event.getX();
curY = event.getY();
int dx = (int) (mx - curX);
int dy = (int) (my - curY);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (started) {
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
} else {
started = true;
}
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
started = false;
break;
}
return true;
}
});