在android中,GridView和listview在单屏幕上显示,根目录滚动

在android中,GridView和listview在单屏幕上显示,根目录滚动,android,android-layout,android-listview,android-gridview,android-scrollview,Android,Android Layout,Android Listview,Android Gridview,Android Scrollview,我必须创建这样的布局 我的布局是- Gridview-完成后填写水平用户,然后在下一行添加水平 列表视图-用列表项垂直填充列表视图 列表视图-用列表项垂直填充列表视图 我必须在根视图上滚动,而不是单独滚动listview和grid视图。 正如所有地方所写的那样,我们不能将listview和Grid视图放在滚动视图中。可能的解决办法是什么?请帮助。我提出解决方案的方法是覆盖listview或gridview。尽管你可能需要比我想的调整得更多。但是给你一个提示重写onTouchEvent方法。下面

我必须创建这样的布局

我的布局是-

Gridview-完成后填写水平用户,然后在下一行添加水平
列表视图-用列表项垂直填充列表视图
列表视图-用列表项垂直填充列表视图

我必须在根视图上滚动,而不是单独滚动listview和grid视图。
正如所有地方所写的那样,我们不能将listview和Grid视图放在滚动视图中。可能的解决办法是什么?请帮助。

我提出解决方案的方法是覆盖listview或gridview。尽管你可能需要比我想的调整得更多。但是给你一个提示重写onTouchEvent方法。下面是listview和scrollview的解决方案:

TouchFeedbackListener.java:

public interface TouchFeedbackListener {

public void setScorlling(boolean isScrolling);

}
SampleScrollView.java:

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

public class SampleScrollView extends ScrollView implements TouchFeedbackListener{
private boolean mListScrolling;
public SampleScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public SampleScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public SampleScrollView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
  //        Log.e("SampleScrollView","onTouchEvent");
    if(this.mListScrolling){
        Log.e("return false","return false");
        return false;
    }else{
        return super.onTouchEvent(ev);  
    }

}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Don't do anything with intercepted touch events if 
    // we are not scrollable
    if (this.mListScrolling) return false;
    else return super.onInterceptTouchEvent(ev);
}

@Override
public void setScorlling(boolean isScrolling) {
    // TODO Auto-generated method stub
    this.mListScrolling = isScrolling;
}
}
 import android.content.Context;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.widget.ListView;

 public class SampleListView extends ListView {

private TouchFeedbackListener mTouchFeedbackListener = null;
public SampleListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public SampleListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public SampleListView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public void setTouchFeedbackListener(TouchFeedbackListener _mTouchFeedbackListener){
    mTouchFeedbackListener = _mTouchFeedbackListener;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    Log.e("SampleListView","onTouchEvent");
    final int action = ev.getAction();

    int pointerCount = ev.getPointerCount();
    int index = 0;

    if (pointerCount > 1) {
        // If you are using new API (level 8+) use these constants
        // instead as they are much better names
        // index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK);
        // index = index >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;

        // for api 7 and earlier we are stuck with these
        // constants which are poorly named
        // ID refers to INDEX, not the actual ID of the pointer
        index = (action & MotionEvent.ACTION_POINTER_ID_MASK);
        index = index >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_MOVE:
        if(mTouchFeedbackListener!=null){
            mTouchFeedbackListener.setScorlling(true);
        }
        return super.onTouchEvent(ev);
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL:
        if(mTouchFeedbackListener!=null){
            mTouchFeedbackListener.setScorlling(false);
        }
        return super.onTouchEvent(ev);
    }
    return true;
}

  }
SampleListView.java:

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

public class SampleScrollView extends ScrollView implements TouchFeedbackListener{
private boolean mListScrolling;
public SampleScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public SampleScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public SampleScrollView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
  //        Log.e("SampleScrollView","onTouchEvent");
    if(this.mListScrolling){
        Log.e("return false","return false");
        return false;
    }else{
        return super.onTouchEvent(ev);  
    }

}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Don't do anything with intercepted touch events if 
    // we are not scrollable
    if (this.mListScrolling) return false;
    else return super.onInterceptTouchEvent(ev);
}

@Override
public void setScorlling(boolean isScrolling) {
    // TODO Auto-generated method stub
    this.mListScrolling = isScrolling;
}
}
 import android.content.Context;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.widget.ListView;

 public class SampleListView extends ListView {

private TouchFeedbackListener mTouchFeedbackListener = null;
public SampleListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public SampleListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public SampleListView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public void setTouchFeedbackListener(TouchFeedbackListener _mTouchFeedbackListener){
    mTouchFeedbackListener = _mTouchFeedbackListener;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    Log.e("SampleListView","onTouchEvent");
    final int action = ev.getAction();

    int pointerCount = ev.getPointerCount();
    int index = 0;

    if (pointerCount > 1) {
        // If you are using new API (level 8+) use these constants
        // instead as they are much better names
        // index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK);
        // index = index >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;

        // for api 7 and earlier we are stuck with these
        // constants which are poorly named
        // ID refers to INDEX, not the actual ID of the pointer
        index = (action & MotionEvent.ACTION_POINTER_ID_MASK);
        index = index >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_MOVE:
        if(mTouchFeedbackListener!=null){
            mTouchFeedbackListener.setScorlling(true);
        }
        return super.onTouchEvent(ev);
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL:
        if(mTouchFeedbackListener!=null){
            mTouchFeedbackListener.setScorlling(false);
        }
        return super.onTouchEvent(ev);
    }
    return true;
}

  }
XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<com.your.package.SampleScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mSroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:text="Button 1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:text="Button 1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:text="Button 1" />

    <com.your.package.SampleListView
        android:id="@+id/mList"
        android:layout_width="wrap_content"
        android:layout_height="200dp" >
    </com.your.package.SampleListView>
</LinearLayout>
 </com.your.package.SampleScrollView>

最后,关于活动的onCreate:

 /** Called when the activity is first created. */
public String[] country = {"Canada", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA", "Germany", "USA"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    ((SampleListView)findViewById(R.id.mList)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,country));
        ((SampleListView)findViewById(R.id.mList)).setTouchFeedbackListener(((SampleScrollView)findV   iewById(R.id.mSroll)));
/**在首次创建活动时调用*/
公共字符串[]国家={“加拿大”、“德国”、“美国”、“德国”、“美国”、“德国”、“美国”、“德国”、“德国”、“德国”、“美国”、“德国”、“德国”、“德国”、“德国”、“德国”、“德国”、“德国”、“德国”、“德国”、“美国”、“美国”、“德国”、“德国”、“德国”、“德国”、“德国”、“德国”、“美国”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SampleListView)findViewById(R.id.mList)).setAdapter(新的ArrayAdapter(这是android.R.layout.simple_list_item_1,country));
((SampleListView)findViewById(R.id.mList)).setTouchFeedbackListener(((SampleScrollView)findViewById(R.id.mSroll));
我认为您也必须将此机制应用于gridview。 希望这有帮助