Android 滚动视图中listview的滚动功能

Android 滚动视图中listview的滚动功能,android,listview,scrollview,Android,Listview,Scrollview,在滚动视图中的应用程序中,我使用的是列表视图。但是列表视图没有滚动。谁能建议我该怎么办。 我搜索了它,发现列表视图没有在滚动视图中滚动 有什么解决方案吗?不要将列表视图放在滚动视图中 import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.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;
}
}
listview已经可以处理滚动,所以它不需要在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;
}
}

您应该更改布局以反映这一点。

将ListView从ScrollView中排除,因为ListView中已经有滚动机制

我假设你有一个非常特殊的情况,就像我不得不这样做时一样。我有一个助手类,看起来像这样:

    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));
}
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);
 }

滚动视图中的ListView将不起作用。把它放到一边去。因为两者都有滚动功能,所以当它们在一起时滚动将不起作用

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);
    }
}
使用此

使用此类

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标记。i、 e.更改为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;
    }
}); 
滚动视图“吃掉”所有触摸。。。 您应该避免将滚动元素(ListView)插入另一个滚动元素(scroll view),否则您的用户可能会发疯

在这些情况下,我使用LinearLayout来代替ListView,并将为表示每个列表元素而创建的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);
}
LinearLayout containerLinearLayout=(LinearLayout)findViewById(R.id.containerLinearLayout);
containerLinearLayout.removeAllViews();

对于(inti=0;i不要将listView放在ScrollView中。 你应该阅读Romain Guy的回答和上面的评论:

您可以从滚动视图中排除ListView。 如果您希望在滚动视图中有一个“列表”,可以使用LinearLayout。如下所示:

    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));
}
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);
 }
公共类MyListLayout扩展了LinearLayout实现
View.OnClickListener{
专用适配器列表;
private View.OnClickListener mListener;
公共MyListLayout(上下文){
超级(上下文);
}
公共MyListLayout(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
公共EvernoteListLayout(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
公共void onClick(视图v){
if(mListener!=null)
mListener.onClick(v);
}
公共无效集合列表(适配器列表){
this.list=列表;
//绝对表
if(this.list!=null){

对于(inti=0;i您可以创建自己的列表视图并将expand设置为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。它将滚动。

最后,我得到了滚动问题的解决方案,我得到了自定义滚动视图类来管理滚动视图

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;
}
}

查看ListView的onMeasure源代码,您会发现,如果未指定height measureSpec,则ListView的高度仅为一个项目的高度加上一些填充,请参见以下内容:

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

因此,我们可以简单地将height SpecMode更改为至多,height size是其父级的左侧高度大小。 以下是解决方案:

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

它就像一个表单,所以我不能从滚动视图中排除listView。我不知道为什么逃避问题解决方案会获得最大的投票:P@IronManGill嫉妒P@ArfanMirza为什么我会是孩子?我已经远远超过了获得代表分数的程度:P…我只是建议你不要逃避问题…我不是建议你逃避问题,而是建议你nt要避免的错误从…开始添加scrollview到已启用滚动功能的listview有什么特殊原因吗?如果不允许项目回收,使用listview有什么意义?最好使用垂直线性布局。因此我们只需将
Mylistlayout
与listview一样工作?而此代码片段可能会解决问题n、 确实有助于提高您的文章质量。请记住,您是在为将来的读者回答这个问题,而这些人可能不知道您的代码建议的原因。