Is it possible to do it?
The main point are:
- A Complex header out of the listView but that must scroll
- A ListView whose scroll is handled by the far ScrollView parent.
- The ListView must have recyling, so the linearLayout cannot be a solution
Thanks
to directly answer the question:
Is it possible to do it?
Yes it is. You can see this example here in the Albums and Profiles sections. But it's not simple, nor straight forward.
There're two issues in adding a ListView inside a ScrollView (you can research that on your own) that is TouchEvents get mixed up because it doesn't know which View will consume the touch and the system don't know how to layout a infinite sized View inside another infinite sidez View.
Because it's it's not simple nor straight forward to implement, there several possible implementations and all of them is A LOT of code and A LOT of testing. I'll point you to a open source example and you can go from there:
The problem here is enclosing ListView inside ScrollView. The steps given below describe how to enclose any vertically scrollable view into another vertically scrollable view.
Step 1
Write a method which determines wheather a View can be scrolled vertically and place the method inside a common utility class as follows. This method is taken from ViewPager.java and modified to find whether a View can vertically scrollable.
public static boolean canScroll(View v, boolean checkV, int dy, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
for (int i = count - 1; i >= 0; i--) {
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft()
&& x + scrollX < child.getRight()
&& y + scrollY >= child.getTop()
&& y + scrollY < child.getBottom()
&& canScroll(child, true, dy,
x + scrollX - child.getLeft(), y + scrollY
- child.getTop())) {
return true;
}
}
}
return checkV && ViewCompat.canScrollVertically(v, -dy);
}
Step 2
Subclass the enclosing vertically scrollable view, it may be ScrollView or ListView(In your case it is ScrollView), or the like and override the onInterceptTouchEvent() method as follows.
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
float dy = y - mLastMotionY;
switch (action) {
case MotionEvent.ACTION_DOWN:
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
if (Util.canScroll(this, false, (int) dy, (int) x, (int) y)) {
mLastMotionY = y;
return false;
}
break;
}
return super.onInterceptTouchEvent(event);
}
Step 3
Subclass the enclosed vertically scrollable view, it may be GridView or ListView or the like(In your case ListView) and override the onMeasure() method as follows. No need to override this method in ScrollView. Its default implementation behaves in the right way.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
if (mode == MeasureSpec.UNSPECIFIED) {
int height = getLayoutParams().height;
if (height > 0)
setMeasuredDimension(getMeasuredWidth(), height);
}
}
Step 4
Finally create an xml layout file and use the ScrollView and ListView that you subclassed and you must hard code the layout_height. If you are creating the view hierarchy through java code, then hard code the height by LayoutParams. This hard coding is not necessary if you use your own measuring strategy rather than one specified in step 3.
For further details please view this post ScrollInsideScroll, download the project and examine the code.
you cannot get recycling ListView if you do not write it yourself. If you want to use the solution for nested fragments (as on image) try this.
来源:https://stackoverflow.com/questions/25763569/listview-in-fragment-in-viewpager-in-scrollview-for-android-2-3