Reputation: 2561
I've been stuck on this for a few days now and can't figure out what I'm doing wrong (hope it's not something stupid I missed).
The requirements
I have a FragmentPager which contains Fragments with LinearLayout. The layout has a button and a view that can change according to user navigation.
All views such as GridView and ListView work perfectly. But I need to make another view that would fit in the Fragment that is a little different in concept. The view must be vertically scrollable since it's height can get bigger than the screen size. I don't care if the view is created programatically or inflated from .xml dynamically. I tried both methods and both gave the same results.
Solution 1 (much more preferable)
I tried to make a custom view and pushed it inside a ScrollView. This is the relevant .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 custom view to show a really large rectangle so it exceeds 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 with this is that the ScorllView doesn't scroll.
I tried removing the fillViewport (as I saw in a different answer) which resulted in nothing shown at all, I also tried to add layouts inside and outside the ScrollView but nothing. Trying all the "fill_parent"/"wrap_content" combinations also resulted with non-scrolling ScrollView.
What am I doing wrong?
Solution 2 (much less preferable)
I can force (brutally) the concept of the view into a ListView, which does scroll fine but I couldn't get the views inside the list to display my custom view. Debugging, I noticed that onDraw is never called. Do I need to implement the drawing of the view somewhere else so it will be drawn in a ListView?
Upvotes: 4
Views: 5025
Reputation: 11
<ScrollView>
//your textview goes here
</ScrollView>
For instance if you have a TextView and you want it to be scrollable then you can use ScrollView like given above.
make sure you are nesting a only one TextView inside your ScrollView
Upvotes: 1
Reputation: 743
Iain_b is correct, when a view is drawn it will clip anything that is not within its layout bounds. For a custom view, this is usually done by overriding the OnMeasure method. Currently you have the custom view set to wrap_content on its height, which in your case will result in a view that has a height of 0 (it has no content). Overriding OnMeasure unfortunately means that you cannot use the height and width properties for your custom view, but you will have much finer control in the OnMeasure call when Android does the layout pass.
This is all described in the Android documentation here: Custom Components
Upvotes: 1