可能重複內:
Listview inside ScrollView is not scrolling on Android的ListView滾動型
我有一個ListView
內ScrollView
和問題是ScrollView
正在滾動,但ListView
不會滾動。我認爲這是因爲ScrollView
。有沒有人有解決這個問題?
可能重複內:
Listview inside ScrollView is not scrolling on Android的ListView滾動型
我有一個ListView
內ScrollView
和問題是ScrollView
正在滾動,但ListView
不會滾動。我認爲這是因爲ScrollView
。有沒有人有解決這個問題?
ListView
s具有內置滾動功能。將其封裝在任何其他佈局中,如LinearLayout
或RelativeLayout
。
您不應該在ScrollView中嵌套一個ListView。
如果您想要與ListView一起滾動一些其他視圖,則可以檢出this answer。
使用以下方法,盡情享受吧!
private void setListViewScrollable(final ListView list) {
list.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
listViewTouchAction = event.getAction();
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, 1);
}
return false;
}
});
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, -1);
}
}
});
}
listViewTouchAction是一個全局整數值。 如果你可以更換線
list.scrollBy(0, 1);
與其他東西請與我們分享。
我需要在滾動視圖中有一個listview,以防止整個屏幕過長。這允許您必須設置列表中顯示的項目數量,其餘項目將滾動。
我在我的應用程序中使用以下類,到目前爲止,它似乎工作了不起。
public class NestedListView extends ListView implements OnScrollListener,
OnTouchListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 4;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null,
this);
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
listViewTouchAction = event.getAction();
if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
我對這個解決方案的問題是它花費了巨大的CPU能力。在onMeasure方法中,您要求listadapter重新創建您要求的每個視圖,這將使整個應用程序非常緩慢。我認爲你應該緩存你請求的視圖,並且只有在它們無效時才重新創建視圖。 – 2012-01-23 14:26:58
雖然我沒有注意到我的Atrix上的這個解決方案有任何問題,但我確信它可能會在較老的設備上出現問題。 – Jeff 2012-04-19 02:01:45
停止爭論,它不會解決這個問題,已經有人找到解決方案,這是由莫伊塞斯奧爾梅多了答案:http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is -not-scrolling-on-android?rq = 1 – Bhimbim 2014-03-03 03:59:26