2013-12-18 154 views
5

在滾動視圖中的我的應用程序中,我使用了列表視圖。但列表視圖不滾動。任何人都可以建議我該怎麼做。 我搜索了它,發現列表視圖在滾動視圖內不滾動。
任何解決方案?在滾動視圖中滾動列表視圖的功能

+0

將scrollview添加到已經滾動啓用listview的任何特殊原因? –

回答

8

您可以創建自己的列表視圖並將展開設置爲false。下面是示例類

public class ExpandableHeightListView extends ListView { 

boolean expanded = false; 

public ExpandableHeightListView(Context context) { 
    super(context); 
} 

public ExpandableHeightListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ExpandableHeightListView(Context context, AttributeSet attrs, 
     int defStyle) { 
    super(context, attrs, defStyle); 
} 

public boolean isExpanded() { 
    return expanded; 
} 

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // HACK! TAKE THAT ANDROID! 
    if (isExpanded()) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } else { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

public void setExpanded(boolean expanded) { 
    this.expanded = expanded; 
} 
} 

你可以用你這樣的activity.clas

ExpandableHeightListView listview = (ExpandableHeightListView) findViewById(R.id.list); 
    listview.setExpanded(true); 

在你的佈局文件,你可以在列表視圖的地方滾動視圖中使用ExpandableHeightListView。它將滾動。

+3

如果你不允許物品回收,使用ListView有什麼意義?那麼最好使用垂直LinearLayout。 – BladeCoder

1

不要在滾動視圖中放置一個listview。

listview已經處理滾動,所以它不需要在滾動視圖內。

你應該改變你的佈局,以反映這一點。

5

從ScrollView中排除ListView,因爲ListView中已經有滾動機制。

+2

它就像一個窗體,所以我不能從滾動視圖中排除listView。 – Sandy

+0

我不知道爲什麼遠離問題解決方案獲得最高票數:P – IronManGill

+0

@IronManGill嫉妒? :P –

0

我假設你有一個非常特殊的情況,就像我必須這樣做時那樣。我有一個助手類,它看起來是這樣的:

public class ScrollViewListHelper { 
public static void getListViewSize(ListView myListView) { 
    ListAdapter myListAdapter = myListView.getAdapter(); 
    if (myListAdapter == null) { 
     //do nothing return null 
     return; 
    } 
    //set listAdapter in loop for getting final size 
    int totalHeight = 0; 
    for (int size = 0; size < myListAdapter.getCount(); size++) { 
     View listItem = myListAdapter.getView(size, null, myListView); 
     listItem.measure(0, 0); 
     totalHeight += listItem.getMeasuredHeight(); 
    } 
    //setting listview item in adapter 
    ViewGroup.LayoutParams params = myListView.getLayoutParams(); 
    params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)); 
    myListView.setLayoutParams(params); 
    // print height of adapter on log 
    Log.i("height of listItem:", String.valueOf(totalHeight)); 
} 

}

再經過我初始化適配爲ListView我這樣做:

ScrollViewListHelper.getListViewSize(listViewMeasurements); 
1

的ListView裏面滾動視圖將無法正常工作。把它放在一邊。因爲兩者都具有滾動功能,因此滾動功能在它們聚集時不起作用。

0
import android.content.Context; 
import android.widget.ListView; 

public class MyListView extends ListView { 

    public MyListView(Context context) { 
     super(context); 
    } 

    public MyListView(Context context, android.util.AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 

使用本

1

使用這個類。

public class CustomScrollView extends ScrollView { 
    public CustomScrollView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
    } 

    public boolean onTouchEvent(MotionEvent ev) { 
     return true; 
    } 

    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return false; 
    } 
} 

然後在你的xml佈局中用CustomScrollView的包名和類更改你的scrollview標籤。即更改爲com.test.CustomScrollView

而你內心活動獲取自定義滾動視圖的ID幷包含此代碼。

private int currentX, currentY; 
private CustomScrollView customScrollView; 

customScrollView.setSmoothScrollingEnabled(true); 
customScrollView.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: { 
      currentX = (int) event.getRawX(); 
      currentY = (int) event.getRawY(); 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: { 
       @SuppressWarnings("unused") 
       int x2 = (int) event.getRawX(); 
       int y2 = (int) event.getRawY(); 
       customScrollView.scrollBy(0 , currentY - y2); 
       currentY = y2; 
       break; 
      } 
      case MotionEvent.ACTION_UP: { 
       break; 
      } 
     } 
     return true; 
    } 
}); 
0

滾動視圖「吃」的所有接觸... 你應該避免將滾動元件(ListView控件)到另一個滾動元件(滾動視圖),您的用戶可以去瘋狂。

在這種情況下,我在地方的ListView使用的LinearLayout,我膨脹到它的customview創建表示每個列表元素,這裏有一個例子,下跌免費問我更多:

LinearLayout containerLinearLayout= (LinearLayout)findViewById(R.id.containerLinearLayout); 
containerLinearLayout.removeAllViews(); 
for(int i=0;i<array.length();i++){ 
JSONObject convocazione=array.getJSONObject(i); 
View child = getLayoutInflater().inflate(R.layout.list_element_layout,null); 
TextView txtDettaglioLuogo=(TextView)child.findViewById(R.id.txtDettaglioLuogo); 
TextView txtOra=(TextView)child.findViewById(R.id.txtOra); 


txtOra.setText("text"); 
txtData.setText("more text"); 


containerLinearLayout.addView(child); 
} 
1

唐不要在ScrollView中放置一個listView。 你應該閱讀羅曼蓋伊答案及以上的評論:

How can I put a ListView into a ScrollView without it collapsing?

您可以從滾動視圖中排除的ListView。 如果你想在scrollView裏有一個「list」,你可以使用LinearLayout。類似這樣的:

public class MyListLayout extends LinearLayout implements 
     View.OnClickListener { 

    private Adapter list; 
    private View.OnClickListener mListener; 

    public MyListLayout(Context context) { 
     super(context); 
    } 

    public MyListLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

    } 

    public EvernoteListLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void onClick(View v) { 
     if (mListener!=null) 
      mListener.onClick(v); 
    } 

    public void setList(Adapter list) { 
     this.list = list; 

     //Popolute list 
     if (this.list!=null){ 
      for (int i=0;i<this.list.getCount();i++){ 
       View item= list.getView(i, null,null); 
       this.addView(item); 
      } 
     } 

    } 

    public void setmListener(View.OnClickListener mListener) { 
     this.mListener = mListener; 
    } 
} 



// Create ArrayAdapter 
MyListAdapter mListAdapter = new MyListAdapter(); 
MyListLayout mLay = (MyListLayout) findViewById(R.id.box_list_ev); 
if (mLay != null) { 
     mLay.setList(mListAdapter); 
} 
+0

所以我們只需要把'Mylistlayout'將像listview一樣工作? –

0

最後,我得到了滾動問題的解決方案,我得到了自定義scrollview類來管理滾動視圖。

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.widget.ScrollView; 

public class VerticalScrollview extends ScrollView{ 

public VerticalScrollview(Context context) { 
    super(context); 
} 

public VerticalScrollview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    final int action = ev.getAction(); 
    switch (action) 
    { 
     case MotionEvent.ACTION_DOWN: 
       Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false"); 
       super.onTouchEvent(ev); 
       break; 

     case MotionEvent.ACTION_MOVE: 
       return false; // redirect MotionEvents to ourself 

     case MotionEvent.ACTION_CANCEL: 
       Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false"); 
       super.onTouchEvent(ev); 
       break; 

     case MotionEvent.ACTION_UP: 
       Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false"); 
       return false; 

     default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action); break; 
    } 

    return false; 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    super.onTouchEvent(ev); 
    Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction()); 
    return true; 
} 
} 
0

檢查的ListView的onMeasure源代碼,你會發現,如果高度measureSpec是不確定的,則列表視圖的高度只有一個項目加上一些填充的高度,見下圖:

if (heightMode == MeasureSpec.UNSPECIFIED) { 
     heightSize = mListPadding.top + mListPadding.bottom + childHeight + 
       getVerticalFadingEdgeLength() * 2; 
    } 

因此,我們可以簡單地將高度SpecMode更改爲AT_MOST,並且高度尺寸是其父項的左側高度尺寸。 下面是解決方案:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), 
      MeasureSpec.AT_MOST); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+1

雖然此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan