Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a scrollable fragment based on the default android PageView

Tags:

java

android

I am using the default setup from the PageView setup in Android. I want to stay very simple. The default setup provides a TextView to put text in, and my text goes off the screen. Is it possible to make this scroll vertically?

// edited to make the code appear in the code block
public class DayFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DayFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView tView = new TextView(getActivity());
        if(getArguments().getInt(ARG_SECTION_NUMBER) > 1){
            tView.setText(getAllMeals(getArguments().getInt(
                    ARG_SECTION_NUMBER)-1));}
        else {
            getMeal();
            tView.setText(message);
        }
        return tView;
    }
}
like image 556
sjoshua270 Avatar asked Feb 21 '13 22:02

sjoshua270


2 Answers

Yes it is very simple, in your xml layout file just make the parent a ScrollView of whatever you want to be scrollable.

ScrollView reference doc:http://developer.android.com/reference/android/widget/ScrollView.html

Edit sample: Here is a sample of your XML.

...rest of xml
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
 </ScrollView> ...rest of xml

Then to get the TextView in your Activity just

TextView tView = (TextView)findViewById(R.id.textview);

2nd Edit: Edited above sample for a regular scrollview. Also to add the swiping functionality, you will want to use a a ViewPager(http://developer.android.com/reference/android/support/v4/view/ViewPager.html) and a FragmentPagerAdapter(http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html). The 2 links to official developer documents contain really great examples!

like image 132
Brent Hronik Avatar answered Nov 19 '22 21:11

Brent Hronik


it is also possible to wrap the entire layout in fragment.xml in a ScrollView , see How to add scroll in Fragment. this is also XML based ...

like image 40
ralf htp Avatar answered Nov 19 '22 20:11

ralf htp