[英]Cannot create fragment using viewpager in an activity.
I need to add a ViewPager
using Fragment
to a portion of my main activity which extends TabActivity
. 我需要将使用
Fragment
的ViewPager
添加到扩展TabActivity
主要活动的一部分中。 For this I have created a ViewPagerActivity
class which extends FragmentActivity
,from here it adds the calls the FragmentAdapter
which creates the Fragment
which contains the view I want to create. 为此,我创建了一个
ViewPagerActivity
类,该类扩展了FragmentActivity
,从此处添加了对FragmentAdapter
的调用,该调用创建了包含要创建的视图的Fragment
。 But the problem that I'm facing is I cannot get my main class to create the viewpager on my main activity. 但是我面临的问题是我无法让主类在主活动上创建viewpager。
I think the problem is when I'm calling the class ViewPagerActivity
on my main class,which cannot create the fragment
to create the viewpager
. 我认为问题是当我在主类上调用类
ViewPagerActivity
,该类无法创建fragment
来创建viewpager
。
Please help I've been stuck in this for almost a month now , each solution I try fails. 请帮助我已经将近一个月了,我尝试的每个解决方案都失败了。 These codes run perfectly when they are implemented separately in a separate project but doesnot seems to execute when implemented in my project.
当这些代码在单独的项目中分别实现时,它们可以完美地运行,但是在我的项目中实现时,它们似乎无法执行。
ViewPagerActivity class 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 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 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后再设置您的内容视图,因为View pager是您的内容视图
setContentView(mviewpager);
The code finally worked with few changes here and there. 该代码终于在这里和那里做了很少的更改。 I'd like to add them here to help other developers with same problem as mine.
我想在这里添加它们,以帮助其他遇到与我相同的问题的开发人员。
I need to add a ViewPager using Fragment to a portion of my main activity which extends TabActivity.
我需要将使用Fragment的ViewPager添加到扩展TabActivity的主要活动的一部分中。 For this I have created a ViewPagerActivity class which extends FragmentActivity ,from here it adds the calls the FragmentAdapter which creates the Fragment which contains the view I want to create.
为此,我创建了一个ViewPagerActivity类,该类扩展了FragmentActivity,从此处添加了对FragmentAdapter的调用,该调用创建了包含要创建的视图的Fragment。 But the problem that I'm facing is I cannot get my main class to create the viewpager on my main activity.
但是我面临的问题是我无法让主类在主活动上创建viewpager。
Fragment
and TabActivity
Fragment
和TabActivity
I found somewhere we could extend Tab
's function also as Listener. 我发现某个地方可以扩展
Tab
的功能,也可以将其用作侦听器。 Here's how I did it: 这是我的操作方式:
public class Main extends FragmentActivity implements OnClickListener,TabContentFactory{ }
Then I wrote the code in my class ViewPagerActivity
into the Main
然后我将类
ViewPagerActivity
的代码写入Main
I guess this was the main problem. 我想这是主要问题。 Anyways, I changed the code to create the
Bundle
in TestFragment
class like this 无论如何,我更改了代码以像这样在
TestFragment
类中创建Bundle
public static TestFragment newInstance(String content) { TestFragment fragment = new TestFragment(); Bundle args = new Bundle(); args.putString("content", content); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // the if clause wasnot working for me mContent = getArguments().getString("content"); }
Changed to onCreate
to return view rather than the layout which I was missing earlier 更改为
onCreate
以返回视图,而不是我之前缺少的布局
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.pagerframe, container,false); LayoutParams textparam = new LayoutParams(LayoutParams.FILL_PARENT,30); TestView text = new TextView(getActivity()); text.setText(mContent); text.setTextColor(Color.WHITE); LinearLayout layout = (LinearLayout) view.findViewById(R.id.pager_ll); layout.setVisibility(View.VISIBLE); layout.setGravity(Gravity.CENTER); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(edtext,textparam); return view; }
After this everything worked well. 此后,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.