Question

As you've probably guessed from the title, I'm a total newbie when it comes to Android development (and coding in general).

I have achieved horizontal view swiping in my app using the ViewPagerIndicator library and code from a tutorial I found. Here's the thing, now I want to set up another horizontal view swiping section (with different content) in a different section of the same app. I'm trying to figure out what code I need to change in the new horizontal view swiping section of my app.

Here's what I've tried so far: I created 2 new classes "ActivityMainSecond.java" and "Fragment1Second.java" and copied and pasted the code from their equivalents (MainActivity & Fragment1). Likewise I did the same for their respective XMLs ("activity_main_second.xml" & "fragment_1_second.xml"). The only code I changed was in the java classes where I linked to the new xml layouts (ex: R.layout), plus I changed the text on the new "fragment_1_second.xml" so it would be different than its equivalent on the first horizontal view swiping section. The problem is, when I run it, it only shows the old fragments from the first horizontal view swiping section!

Do I need to create another "TestFragment" and TestFragmentAdapter" class for the new section? Do I need to create a second pager (ex: @+id/pager2)? Please excuse my ignorance, I'm totally lost here...

Here's the code I used for the first horizontal view swiping section using the ViewPageIndicator.

MainActivity

import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

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

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
}
}

Test Fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestFragment extends Fragment{

private static final String KEY_CONTENT = "TestFragment:Content";

public static TestFragment newInstance(String content){
    TestFragment fragment = new TestFragment();

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 20; i++){
        builder.append(content).append(" ");
    }
    builder.deleteCharAt(builder.length() -1);
    fragment.mContent = builder.toString();

    return fragment;
}

private String mContent = "???";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)){
        mContent = savedInstanceState.getString(KEY_CONTENT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    TextView text = new TextView(getActivity());
    text.setGravity(Gravity.CENTER);
    text.setText(mContent);
    text.setTextSize(20 * getResources().getDisplayMetrics().density);
    text.setPadding(20, 20, 20, 20);

    LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,   LayoutParams.FILL_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.addView(text);
    return layout;
}

public void onSavedInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString(KEY_CONTENT, mContent);
}
}

TestFragmentAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.viewpagerindicator.IconPagerAdapter;

public class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
protected static final String[] CONTENT = new String[] {
    "This", "Is", "A", "Test" , "Too"   
};

private int mCount = CONTENT.length;

public TestFragmentAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getIconResId(int index) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = new Fragment1();
    switch(position){
    case 0:
        fragment = new Fragment1();
        break;
    case 1:
        fragment = new Fragment2();
        break;
    case 2:
        fragment = new Fragment3();
        break;
    case 3:
        fragment = new Fragment4();
        break;
    case 4:
        fragment = new Fragment5();
        break;  
    }
    return fragment;
}

@Override
public int getCount() {
    return mCount;
}

@Override
public CharSequence getPageTitle(int position){
    String title = "";
    switch(position){
    case 0:
        title = "Fragment 1";
        break;
    case 1:
        title = "Fragment 2";
        break;
    case 2:
        title = "Fragment 3";
        break;
    case 3:
        title = "Fragment 4";
        break;
    case 4:
        title = "Fragment 5";
        break;  
    }

    return title;
}

public void setCount(int count){
    if (count > 0 && count < 10){
        mCount = count;
        notifyDataSetChanged();
    }
}


}

Fragment1 (the other fragments are basicly the same)

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_1, null);
}
}

activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
xmlns:app="http://schemas.android.com/apk/res/com.rufflez.viewpagerexample">
<com.viewpagerindicator.TitlePageIndicator 
    android:id="@+id/indicator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:background="#000000"
    android:textColor="#99C224"
    app:footerColor="#99C224"
    app:footerLineHeight="1dp"
    app:footerIndicatorStyle="underline"
    app:footerIndicatorHeight="3dp"
    app:selectedColor="#99C224"
    app:selectedBold="true"/>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>


</LinearLayout>

fragment_1 (other fragment XMLs are basicly the same)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView 
    android:gravity="center"
    android:text="Fragment 1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

Here's the new horizontal view swiping section I'm trying to create:

MainActivitySecond

import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivitySecond extends FragmentActivity {
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_second);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
}
}

Fragment1Second

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1Second extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_1_second, null);
}
}

activity_main_second

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
xmlns:app="http://schemas.android.com/apk/res/com.rufflez.viewpagerexample">
<com.viewpagerindicator.TitlePageIndicator 
    android:id="@+id/indicator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:background="#000000"
    android:textColor="#99C224"
    app:footerColor="#99C224"
    app:footerLineHeight="1dp"
    app:footerIndicatorStyle="underline"
    app:footerIndicatorHeight="3dp"
    app:selectedColor="#99C224"
    app:selectedBold="true"/>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

</LinearLayout>

fragment_1_second

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView 
    android:gravity="center"
    android:text="Something Different"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>
Was it helpful?

Solution

You need to make a new adapter as the adapter specifies where to get the content. Specifically this section is your problem:

@Override
public Fragment getItem(int position) {
    Fragment fragment = new Fragment1();
    switch(position){
    case 0:
        fragment = new Fragment1();// <-- should be a new fragment that you use
        break;
    case 1:
        fragment = new Fragment2(); // <-- should be a new fragment that you use
        break;
    case 2:
        fragment = new Fragment3(); // <-- should be a new fragment that you use
        break;
    case 3:
        fragment = new Fragment4(); // <-- should be a new fragment that you use
        break;
    case 4:
        fragment = new Fragment5(); // <-- should be a new fragment that you use
        break;  
    }
    return fragment;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top