1

我需要在扩展的主要活动的一部分中添加ViewPager使用。为此,我创建了一个扩展类,从这里它添加了创建包含我要创建的视图的调用。但我面临的问题是我无法让我的主类在我的主要活动上创建查看器。FragmentTabActivityViewPagerActivityFragmentActivityFragmentAdapterFragment

我认为问题是当我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);
        }
}
4

2 回答 2

1

您忘记设置内容视图,因此在初始化 ViewPager 后设置您的 View Pager 是您的内容视图

setContentView(mviewpager);
于 2013-02-15T04:48:42.923 回答
0

代码最终在这里和那里进行了一些更改。我想在这里添加它们以帮助其他与我有同样问题的开发人员。

我需要使用 Fragment 将 ViewPager 添加到扩展 TabActivity 的主要活动的一部分。为此,我创建了一个扩展 FragmentActivity 的 ViewPagerActivity 类,从这里它添加了对 FragmentAdapter 的调用,该调用创建了包含我要创建的视图的 Fragment。但我面临的问题是我无法让我的主类在我的主要活动上创建查看器。

  • 首先我无法扩展多个类,因为我需要两者FragmentTabActivity
  • 我发现在某个地方我们也可以将Tab' 的功能扩展为 Listener。我是这样做的:

    public class Main extends FragmentActivity implements OnClickListener,TabContentFactory{ }
    
  • 然后我把我班上的代码ViewPagerActivity写进Main

  • 我想这是主要问题。无论如何,我改变了代码来创建这样BundleTestFragment

    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");
    }
    
    • 更改为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;
      }
      
    • 在此之后,一切都运行良好。

于 2013-03-05T11:52:15.520 回答