如何将列表添加到pageindicator的片段?

时间:2012-10-27 20:22:55

标签: list listview android-viewpager viewpagerindicator

我想将列表视图添加到pageindicator的一页,代码页面指示符是:

public class VpiAbsTestActivity extends SherlockFragmentActivity {

private static final String[] CONTENT = new String[] { "This", "Is", "A", "ViewPager", "Demo" };

TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

public boolean onCreateOptionsMenu(Menu menu) {

    SubMenu subMenu1 = menu.addSubMenu("Action Item");
    subMenu1.add(0,15, 0, "Sample");
    subMenu1.add(0,20, 0,"Menu");
    subMenu1.add("Items");

    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(R.drawable.ic_title_share_default);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenu subMenu2 = menu.addSubMenu("Overflow Item");
    subMenu2.add("These");
    subMenu2.add("Are");
    subMenu2.add("Sample");
    subMenu2.add("Items");

    MenuItem subMenu2Item = subMenu2.getItem();
    subMenu2Item.setIcon(R.drawable.ic_compose);
    subMenu2Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case 10:
        Toast.makeText(VpiAbsTestActivity.this, "Now "+item.getItemId(), Toast.LENGTH_SHORT).show();
        return true;
    case 15:
        Toast.makeText(VpiAbsTestActivity.this, "Now = "+item.getItemId(), Toast.LENGTH_SHORT).show();
        return true;
    case 20:
        Toast.makeText(VpiAbsTestActivity.this, "Now == "+item.getItemId(), Toast.LENGTH_SHORT).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.simple_tabs);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
}

class TestFragmentAdapter extends FragmentPagerAdapter {        
    private int mCount = CONTENT.length;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return TestFragment.newInstance(String.valueOf(position)); 
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position];
    }

 }

}

我想向第一页添加一个列表视图,例如这个列表,但我不能为这段代码写一个列表片段,请告诉我我的工作

public static final String[] list = new String[]{"France", "London", "Sweden"};

我该怎么办?

1 个答案:

答案 0 :(得分:0)

首先,您必须了解什么是片段以及如何实例化它以及如何向其发送数据(您应该查看guide dedicated to Fragments和片段here的文档)。

然后你必须做两件事:

  1. 创建自定义列表片段,建议您查看ListFragment的android文档,并创建一个继承自ListFragment的类。

    public class CustomListFragment extends ListFragment {
        // your code here to add content 
    }
    
  2. 当您在第一页时,修改您的TestFragmentAdapter以实现新的CustomListFragment(参见下面的示例)

    public class TestFragmentAdapter
        extends FragmentPagerAdapter
    {
    
        protected final Class[] CONTENT_CLASSES = new Class[] { CustomListFragment.class, TestFragment.class};
        protected final String[] CONTENT_TITLES = new String[] { "List", "Test"};
    
        public TabAdapter( FragmentManager manager, Activity context )
        {
            super( manager );
            this.context = context;
        }
    
        @Override
        public Fragment getItem( int i )
        {
            Class targetFragmentClass = CONTENT_CLASSES[i]; 
    
            Bundle args = new Bundle();
            // add your args here 
    
            Fragment targetFragment = Fragment.instantiate( context, targetFragmentClass.getName(), args );
            return targetFragment;
        }
    
        @Override
        public int getCount()
        {
            return CONTENT_CLASSES.length;
        }
    
        @Override
        public CharSequence getPageTitle( int position )
        {
            return CONTENT_TITLES[i];
        }
    }