Scrolling view in fragment

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?

+4
source share
2 answers

Iain_b is correct, when the image is drawn, it will crop everything that does not match its layout boundaries. For a custom view, this is usually done by overriding the OnMeasure method. Currently, you have a custom view of the wrap_content view at its height, which in your case will result in a view with a height of 0 (it has no content). Unfortunately, overriding OnMeasure means you cannot use the height and width properties for your custom view, but you will have much finer control in calling OnMeasure when Android executes the layout.

All of this is described in the documentation for Android here: Custom components

+1
source
 <ScrollView> //your textview goes here </ScrollView> 

For example, if you have a TextView and you want it to be scrollable, you can use ScrollView as described above.

make sure you have only one TextView in your ScrollView

+1
source

All Articles