1

我想使用 ViewPagerIndicator 在我的应用程序中的片段中导航。我已成功导入库并实现了 TitlePageIndicator。到目前为止,当我启动应用程序时我没有任何异常,我也看到了指示器,但没有任何页面名称。这是我的代码

viewpager_layout.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="fill_parent"
android:orientation="vertical" >

 <com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/titles"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

<android.support.v4.view.ViewPager
    android:id="@+android:id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

搜索活动

public class SearchActivity extends FragmentActivity 
implements Tab1Fragment.OnHeadlineSelectedListener, Tab2Fragment.OnHeadlineSelectedListener 
{

private MyPagerAdapter mPagerAdapter; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    //getApplicationContext().deleteDatabase("db");

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewpager_layout);

    UserID.getInstance().createUserID(this);

    this.initialisePaging(); //initialsie the pager
    invalidateOptionsMenu();

}

private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
    fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
    fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName()));
    this.mPagerAdapter  = new MyPagerAdapter(getSupportFragmentManager(), fragments);


    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    pager.setAdapter(mPagerAdapter);

    TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
    titleIndicator.setViewPager(pager);
}
}

我的页面适配器

package com.easyimmoandroid.myPager;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPagerAdapter extends FragmentPagerAdapter {

private static final String[] TITLES = new String[] { "Title 1", "Title 2",  "Title 3" };

private List<Fragment> fragments;

public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int pos) {
    return this.fragments.get(pos);
}

@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

}

分段

package com.easyimmoandroid.fragments;

public class Tab1Fragment extends Fragment {

private ScrollView mScrollView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (container == null) {
        return null;
    }

    // FRAGMENT LAYOUT
    mScrollView = (ScrollView) inflater.inflate(R.layout.tab_frag1_layout,
            container, false);
...

return mScrollView;
}

谢谢您的帮助

由于这仍然不起作用,我会请你建议我是否有任何其他解决方案可以做到这一点,除了 WiewPagerIndicator。

4

2 回答 2

0

尝试在您的 initialisePaging 方法的末尾添加:

        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            mPagerAdapter.getPageTitle(position);
        }
    });

它对我有用:)

于 2013-06-18T10:26:23.187 回答
0

像这样改变你的getItem()方法

 @Override
    public Fragment getItem(int pos) {
        return TestFragment.newInstance(fragments.get(position % fragments.size));
    }

也改变你getPageTitle的方法

    @Override
public CharSequence getPageTitle(int position) {
    return MyPagerAdapter.fragments.get(position % fragments.size);
}

TestFragment班级看起来像

        public final class TestFragment extends Fragment{


            static Typeface typeface, type2;
            private static final String KEY_CONTENT = "TestFragment:Content";



            public static TestFragment newInstance(String content)
            {

                TestFragment fragment = new TestFragment();

                StringBuilder builder = new StringBuilder();

                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) {


                LinearLayout layout = new LinearLayout(getActivity());

                layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));



     TextView text = new TextView(getActivity());
                text.setText(mContent);
                text.setTextSize(20f);
                layout.addView(text);
                layout.setGravity(Gravity.CENTER);

                return layout;
            }

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

希望它可以解决您的问题

于 2013-06-18T10:35:21.650 回答