I have been stuck with this for several days and canβt understand what I am doing wrong (I hope this is not something stupid that I missed).
Requirements
I have a FragmentPager that contains fragments with LinearLayout. The layout has a button and a view that can change in accordance with the navigation of the user.
All views, such as GridView and ListView, work great. But I need to make another representation that fits in a fragment that is slightly different in concept. The view should be vertically scrollable, as the height may exceed the screen size. I don't care if the view is created programmatically or dynamically inflated from .xml. I tried both methods and both gave the same results.
Solution 1 (much more preferred)
I tried to create a custom view and clicked it in a ScrollView. This is the corresponding .xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroller" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" > <com.myPackage.MyView android:layout_width="fill_parent" android:layout_height="wrap_content" > </com.myPackage.MyView> </ScrollView>
I made the usual presentation to show a really large rectangle so that it exceeds the screen size. code for MyView:
package com.myPackage.MyView; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView extends View { public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawRect(0, 0, 100, 8000, paint); } }
The problem is that ScorllView does not scroll.
I tried to remove fillViewport (as I saw in another answer), as a result of which nothing was shown, I also tried to add layouts inside and outside ScrollView, but nothing. Trying all the fill_parent / wrap_content combinations also led to ScrollView scrolling.
What am I doing wrong?
Solution 2 (much less preferred)
I can force the (brutally) concept of the view into a ListView that scrolls in order, but I could not get the views inside the list to display my own view. Debugging, I noticed that onDraw is never called. Do I need to implement a drawing elsewhere so that it is drawn in a ListView?