我需要使用Fragment
添加ViewPager
到我的主要活動的一部分,該活動延伸到TabActivity
。 爲此,我創建了一個ViewPagerActivity
類,它擴展了FragmentActivity
,從這裏添加了調用FragmentAdapter
,創建了包含我想創建的視圖的Fragment
。 但是我面臨的問題是我無法讓我的主類在我的主要活動上創建viewpager。無法在活動中使用viewpager創建片段。
我認爲問題出在我打電話給我的主班級ViewPagerActivity
,這不能創建fragment
來創建viewpager
。
請幫助我在這裏呆了差不多一個月了,我嘗試的每個解決方案都失敗了。 這些代碼在單獨項目中分開實現時運行完美,但在我的項目中實現時似乎不會執行。
ViewPagerActivity類
class ViewPagerClass extends FragmentActivity implements OnClickListener
{
TestFragmentAdapter mAdapter;
ViewPager mviewpager;
CirclePageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mviewpager = new ViewPager(getApplicationContext());
mviewpager.setId(R.id.viewPager);
mviewpager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mviewpager);
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
TestFragmentAdapter.java
class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final String[] CONTENT = new String[] { "This", "Is", "A", "Test"};
protected static final int[] ICONS = new int[] {};
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}
@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
TestFragment.java
public final class TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
static TestFragment fragment = new TestFragment();
public static TestFragment newInstance(String content) {
StringBuilder builder = new StringBuilder();
builder.append(content).append("\n");
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) {
View view = inflater.inflate(R.layout.pagerframe, container);
LayoutParams textparam = new LayoutParams(130,40);
TestView text = new TextView(getActivity());
text.setText(mContent);
text.setTextColor(Color.WHITE);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.pager_ll);
layout.setBackgroundColor(Color.BLUE);
layout.setVisibility(View.VISIBLE);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(text,textparam);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
感謝您的幫助,但viewpager僅僅是一個主視圖的一部分,甚至當我使用addView(mviewpager)它因此未工作。 – 2013-02-15 06:31:03
如果視圖分頁器只是它的一部分,它很容易在xml文件中定義。 Bdw你的代碼應該返回包含TextView的視圖,該視圖應該顯示你的mContent(檢查你的mcontent天氣是空的還是空的,如果你的視圖不顯示的話);我建議你先設置一個靜態文本 – 2013-02-15 06:39:38
是的,我錯過了,謝謝。 – 2013-03-05 12:16:51