我需要在扩展的主要活动的一部分中添加ViewPager
使用。为此,我创建了一个扩展类,从这里它添加了创建包含我要创建的视图的调用。但我面临的问题是我无法让我的主类在我的主要活动上创建查看器。Fragment
TabActivity
ViewPagerActivity
FragmentActivity
FragmentAdapter
Fragment
我认为问题是当我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();
}
}
测试片段.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);
}
}