1

i want to add list view to one page of pageindicator the code oof page indicator is :

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];
    }

 }

}

i want to add a list view to page one for example this list but i cant write a list fragment for this code please tel me what i do

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

what can I do ?

4

1 回答 1

0

首先,您必须了解什么是 Fragment 以及如何实例化它以及如何向其发送数据(您应该在此处查看 Fragment专用指南和 Fragments 文档)。

然后你必须做两件事:

  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];
        }
    }
    
于 2012-11-01T13:27:07.573 回答