2

Everything I have seen is overkill. I am simply looking for a way to have some part of my app scroll through several text snippets when swiping left/right. Imagine an image gallery that you scroll through, but just a line of text for each instead of an image. Thanks!

3 Answers3

2

You can have a look at my other posts for more detailed answer. The explanation is very simple in my other posts.

How to properly use fragments with ViewPager?

Multiple ViewPagers in on Activity

How to swipe xml layouts using ViewPager

These are few examples you can find helpful. Hope this helps.. :)

If still having some issues with those answers, take a look at this simple example:

Just follow simple steps here:

Step 1: Create layout for your view pager:(activity_main.xml)

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="#333333">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Step 2: Create another layout to hold your textview items: (items.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/text_view1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="#94999F"
        android:gravity="left|center_vertical"
        android:paddingLeft="10dp"
        android:text="Hello"
        android:textColor="#FFFFFF"
        android:textSize="17sp"
        android:textStyle="bold"
        android:visibility="visible" />

</LinearLayout>

Step 3: In you Main Activity create a class for your adapter or you can create a separate class for your adapter. This is just an adapter inside the same class. Here's the full code:

public class MainActivity extends Activity {

    ViewPager mViewPager;
    String[] items = { "one", "two", "three", "four", "five" };
    ArrayList<String> mArrayList = new ArrayList<String>();
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        /*for (String s : items) {

            mArrayList.add(s);

        }*/

            for(int i = 0;i<100;i++){

        mArrayList.add("Item " + i);

    }

        mViewPager = (ViewPager) findViewById(R.id.pager);
        MyAdapter mAdapter = new MyAdapter(context, mArrayList);
        mViewPager.setAdapter(mAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    /*Here's the adapter*/
    private class MyAdapter extends PagerAdapter {

        ArrayList<String> someList = new ArrayList<String>();
        Context context;

        public MyAdapter(Context context, ArrayList<String> newsomeList) {

            super();
            this.context = context;
            this.someList = newsomeList;

        }

        @SuppressLint("NewApi")
        @Override
        public void finishUpdate(ViewGroup container) {
            // TODO Auto-generated method stub
            super.finishUpdate(container);

        }

        @Override
        public int getCount() {

            return someList.size();

        }

        @Override
        public boolean isViewFromObject(View collection, Object object) {

            return collection == ((View) object);
        }

        @Override
        public Object instantiateItem(View collection, int position) {

            // Inflating layout
            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Setting view you want to display as a row element
            View view = inflater.inflate(R.layout.items, null);

            TextView itemText = (TextView) view.findViewById(R.id.text_view1);
            // Getting reference for text view and inflate the view for Answers

            try {

                itemText.setText(someList.get(position));

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ((ViewPager) collection).addView(view, 0);
            return view;

        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);

        }

    }

}

And that's you need .. :)

Hope this will help you.. Its a very simple example for what you need.. Good Luck .. :)

Community
  • 1
  • 1
mike20132013
  • 5,257
  • 3
  • 28
  • 41
1

take a look at this: Using ViewPager for Screen Slides

ILovemyPoncho
  • 2,622
  • 2
  • 19
  • 33
0

You can use HorizontalScrollView. An example is here. Instead of Buttons, you can use TextView to serve you puroise.

Or else you can use ViewPager also. Already others have given you tutorial just follow that. But I think, to serve your purpose HorizontalScrollView is needed.

Rohit
  • 2,288
  • 6
  • 22
  • 48