我想使用 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。