我想使用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>
SearchActivity
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);
}
}
MyPagerAdapter
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之外是否还有其他解决办法。
答案 0 :(得分:0)
尝试在initialisePaging方法的末尾添加它:
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mPagerAdapter.getPageTitle(position);
}
});
它对我有用:)
答案 1 :(得分: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);
}
}
希望它可以解决您的问题