简体   繁体   中英

GridView and listview in single screen with root scrolling in android

I have to create the layout like

在此处输入图片说明

my layout would be like-

Gridview - horizontal user filled when finished then add horizontal in next line
Listview - vertically fill the listview with listitem
Listview - vertically fill the listview with listitem

i have to scroll on root view, not the individual scrolling of listview and grid view. As everywhere it is written that we can not put the listview and Grid view inside the scroll view. what are the possible solutions ? please help.

The way i came up with the solution was by overriding the listview or gridview. Though you might have to adjust more then what i have come up with. But to give you a hint Override onTouchEvent method. Below is the solution with listview and 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;
}
}

SampleListView.java:

 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 Layout file:

<?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>

Finally on your Activity's 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)));

I think you have to apply this mechanism for your gridview too. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM