2013-12-18 152 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