A scrollable view with two ListViews inside - android

I have this view which contains a header (not a ListView header) with information in general, and below that should be two ListViews, populated by custom-made CursorAdapters. Now I can't put everything inside a ScrollView since you can't put a ListView inside that (it will break things and won't scroll).
Any idea? The layout is something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="...">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<!-- A lot of things here (the header I was talking about) -->
</RelativeLayout>
<!-- Just a horizontal line (separator) -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/gray_dark" />
<!-- Two ListViews here -->
<ListView
android:id="#+id/first_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/second_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Use Custom class for 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;
}
}
and use it in your xml
like:-
<com.yourPackage.VerticalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context="...">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<!-- A lot of things here (the header I was talking about) -->
</RelativeLayout>
<!-- Just a horizontal line (separator) -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#color/gray_dark" />
<!-- Two ListViews here -->
<ListView
android:id="#+id/first_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/second_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</com.yourPackage.VerticalScrollView>
now your both list views should be scroll able.

Related

LinearLayout not scrolling inside ScrollView [duplicate]

This question already has answers here:
android listview display all available items without scroll with static header
(13 answers)
Closed 2 years ago.
I'm trying to figure out why this LinearLayout is not scrolling:
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:orientation="vertical"
android:weightSum="2"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="TITLE 1"
android:textSize="30sp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
</LinearLayout>
</ScrollView>
I'm adding programmatically a textView and a listView to the linearLayout, and the page is cut in half. The only things scrolling are the listViews.
I don't need the listViews to be scrollable, I only want the entire linearLayout scrollable.
Try this code
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:orientation="vertical"
android:weightSum="2"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="TITLE 1"
android:textSize="30sp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
The listview doesn't scroll because it is nested.
If you use listview inside Scrollview, they might interrupt each other. (both scrolls)
You should customize your Scrollview, so the scrollview will be stop scrolling when you touch listview area.
1) Create new class: CustomScrollView and extend it from ScrollView
2) Change your xml <ScrollView> to your custom scrollview: <com.your_package_name.CustomScrollView>
CustomScrollView.java:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(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("CustomScrollView", "onInterceptTouchEvent: DOWN super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
// Log.i("CustomScrollView", "onInterceptTouchEvent: CANCEL super false" );
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
//Log.i("CustomScrollView", "onInterceptTouchEvent: UP super false" );
return false;
default:
//Log.i("CustomScrollView", "onInterceptTouchEvent: " + action );
break;
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
//Log.i("CustomScrollView", "onTouchEvent. action: " + ev.getAction() );
return true;
}
}

Stop vertical movement when user flings on Horizontal recyclerview

I have a horizontal recycler-view within motion layout. Sometimes when I do a horizontal swipe, the page starts scrolling in the vertical direction. This happens when we do a diagonal fling.
I want something like Google Play where horizontal recycler-views can defend slightly diagonal vertical swipes. BTW setting nestedscrollEnabled doesn't work.
There is quite a nice article regarding all the RecycleView scroll behavior and the issue itself. Checkout RĂºben Sousa article. Solution is quite easy, try to use adopted RecyclerView:
/**
* A RecyclerView that only handles scroll events with the same orientation of its LayoutManager.
* Avoids situations where nested recyclerviews don't receive touch events properly:
*/
public class OrientationAwareRecyclerView extends RecyclerView {
private float lastX = 0.0f;
private float lastY = 0.0f;
private boolean scrolling = false;
public OrientationAwareRecyclerView(#NonNull Context context) {
this(context, null);
}
public OrientationAwareRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public OrientationAwareRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
addOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
scrolling = newState != RecyclerView.SCROLL_STATE_IDLE;
}
});
}
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
final LayoutManager lm = getLayoutManager();
if (lm == null) {
return super.onInterceptTouchEvent(e);
}
boolean allowScroll = true;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
lastX = e.getX();
lastY = e.getY();
// If we were scrolling, stop now by faking a touch release
if (scrolling) {
MotionEvent newEvent = MotionEvent.obtain(e);
newEvent.setAction(MotionEvent.ACTION_UP);
return super.onInterceptTouchEvent(newEvent);
}
break;
}
case MotionEvent.ACTION_MOVE: {
// We're moving, so check if we're trying
// to scroll vertically or horizontally so we don't intercept the wrong event.
float currentX = e.getX();
float currentY = e.getY();
float dx = Math.abs(currentX - lastX);
float dy = Math.abs(currentY - lastY);
allowScroll = dy > dx ? lm.canScrollVertically() : lm.canScrollHorizontally();
break;
}
}
if (!allowScroll) {
return false;
}
return super.onInterceptTouchEvent(e);
}
}
This Work perfect for me.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:id="#+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/btnMore"
android:text="Sample title"
android:textColor="#android:color/black"
android:textSize="18sp" />
<Button
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:theme="#style/MyButton"
android:text="more"
android:textColor="#FFF" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
list_single_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<ImageView
android:id="#+id/itemImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="#drawable/android" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/itemImage"
android:gravity="center"
android:padding="5dp"
android:text="Sample title"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Try this:
RecyclerView rv = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true);
rv.setLayoutManager(layoutManager);
rv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow RecyclerView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow RecyclerView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
v.onTouchEvent(event);
return true;
}
});
That code prevent scroll vertically when you scroll with your horizontal recyclerview.

OnTouchListener on LinearLayout that is behind buttons

I am working on an Android application using C# in Xamarin. In my axml file, I have a LinearLayout with a certain ID that I have assigned a onTouchListener to. The problem is that as long as the buttons I have in my design are on top of the linearLayout, the onTouchListener is never triggered. If I set one of the buttons to invisible, then it works.
Is it possible to still have the onTouchListener react even though there are Controls in the way?
This is my axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:layout_marginBottom="0.0dp"
android:background="#android:color/holo_green_light"
android:soundEffectsEnabled="false" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:background="#android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:background="#android:color/holo_red_light" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
</LinearLayout>
This is how I set the onTouchlistener for the linearLayout:
LinearLayout linLay = FindViewById<LinearLayout>(Resource.Id.LinearLayout1);
linLay.SetOnTouchListener(new MyTouchListener(this));
This is a custom touch listener class:
public class MyTouchListener : Java.Lang.Object, View.IOnTouchListener
{
Context activityContext;
public MyTouchListener(Context ac)
{
activityContext = ac;
}
public bool OnTouch(View v, MotionEvent e)
{
float x = 0.0f;
float y = 0.0f;
if (e.Action == MotionEventActions.Down)
{
// do stuff
x = e.GetX();
y = e.GetY();
Toast.MakeText(activityContext, x.ToString(), ToastLength.Short).Show();
return true;
}
//if (e.Action == MotionEventActions.Up)
//{
// // do other stuff
// return true;
//}
return true;
}
}
And finally, this is how I set up the button click listener:
Button button = FindViewById<Button>(Resource.Id.button1);
button.Click += (s,e) =>
{
//int i = count % s.Count;
//if(i > 28)
// Toast.MakeText(this, button.GetX().ToString(), ToastLength.Short).Show();
button.Text = string.Format("{0}", count++);
};
You have to intercept the touch event. In order to do that you have to subclass LinearLayout and override onInterceptTouchEvent():
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(getContext(), "onTouch", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
Your xml:
<?xml version="1.0" encoding="utf-8"?>
<com.your.package.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:layout_marginBottom="0.0dp"
android:background="#android:color/holo_green_light"
android:soundEffectsEnabled="false" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:visibility="invisible"
android:background="#android:color/holo_blue_light" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:background="#android:color/holo_red_light" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
</com.your.package.MyLinearLayout>
Now any touch event on the layout will be handled by your custom MyLinearLayout.
You need to extend (inherent) the parent linear layout so that you can intercept touch events from the nested children layouts. Override these two methods to receive touches from the children. Then in your xml you will add "myCustomLinearLayout" or whatever you name the your custom linear layout class. It should be pretty similar in C# / Xamarin.
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; // With this i tell my layout to consume all the touch events from its childs
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s",
break;
case MotionEvent.ACTION_MOVE:
//Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s",
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}

ScrollView doesn't start at top

I have created a scrollview and inside I placed a LinearLayout containing an image a text and a button. Unfortunately when the app displays this layout the scrollview's starting position is in the middle, displaying the text instead of the top, displaying the image.
I have tried programatically the scrollto and it didnt work. I have also tried the requestfocus in the XML and it didn't work.
Any ideas?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sv"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/rregeneratingcream" />
<EditText
android:id="#+id/etDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:enabled="false"
android:background="#00000000" />
<Button
android:id="#+id/buttonRegeneration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
</ScrollView>
Try this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sv"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"/>
<EditText
android:id="#+id/etDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="#+id/imageView1"
android:inputType="textMultiLine"
android:focusable="false"
android:background="#00000000" />
<Button
android:id="#+id/buttonRegeneration"
android:layout_width="fill_parent"
android:layout_below="#+id/etDescription"
android:layout_height="wrap_content"
android:text="Next" />
</RelativeLayout>
</ScrollView>
And let me know if you need any further help.
You can use the Custom Vertical ScrollView like this :
package com.xyz.utils;
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;
}
}

scrolling ListView within ScrollView

I have a ScrollView. One of its children is a ListView. Both scrolling in the same direction. How do I get both of them to respond to scroll events? And then when the end of the ListView is reached for the event to go to the parent ScrollView so the ScrollView may scroll?
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
...
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
...
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
...
</LinearLayout>
<android.support.v4.view.ViewPager
.../>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/pad_half"
>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
</LinearLayout>
</LinearLayout>
</ScrollView>
My ListView actually goes inside the ViewPager. The design is such that about three items in the ListView is visible, and user is able to scroll to see other items. In the meantime, the views above and below the ViewPager are visible. Again, it's a ViewPager: it has other pages than the ListView in question.
You Should not have a listview within a ScrollView.
But you can have a custom class and use that to accomplish A Listview with in a ScrollView.
Try the following code
first make a custom ScrollView Class.
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:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself
case MotionEvent.ACTION_CANCEL:
super.onTouchEvent(ev);
break;
case MotionEvent.ACTION_UP:
return false;
default: break;
}
return false;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return true;
}
}
Then use this class for you layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<com.gui.today.VerticalScrollview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/progressBar3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />
<TextView
android:id="#+id/empty_calender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="#string/empty_calender"
android:textColor="#ffffff"
android:visibility="gone" />
<ListView
android:id="#+id/listView2"
android:layout_width="fill_parent"
android:layout_height="150dp" >
</ListView>
</FrameLayout>
</LinearLayout>
</com.gui.today.VerticalScrollview>
I have a ScrollView
Get rid of it.
One of its children is a ListView
Put everything in the ListView, either using addHeaderView()/addFooterView(), my MergeAdapter, or something else along those lines.
You cant scroll a listview same direction as the scrollview. But if you have a horizontal scrollview you can scroll vertical with a listview.
you cannot add a ListView in a scroll View ,as list view also scolls and there would be a synchonization problem between listview scroll and scroll view scoll. You can make a CustomList View and add this method into it.
#Override public boolean onInterceptTouchEvent(MotionEvent ev)
{
/*
* Prevent parent controls from stealing our events once we've gotten a touch down
*/
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
ViewParent p = getParent();
if (p != null) {
p.requestDisallowInterceptTouchEvent(true);
}
}
return false;
}

Categories

Resources