1

ページインジケーターの1ページにリストビューを追加したいのですが、ページインジケーターのコードは次のとおりです。

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

 }

}

このリストなどのページ1にリストビューを追加したいのですが、このコードのリストフラグメントを記述できません。何をするか教えてください

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

私に何ができる ?

4

1 に答える 1

0

First of all you have to understand what is a Fragment and how to instantiate it and how to send to it data (you should look the guide dedicated to Fragments and the documentation for Fragments here).

Then you have to do 2 things :

  1. Create your custom list fragment, I suggest you to look at the android documentation of ListFragment and create a class that inherits from ListFragment.

    public class CustomListFragment extends ListFragment {
        // your code here to add content 
    }
    
  2. Modify your TestFragmentAdapter to instanciate your new CustomListFragment when you are in the first page (see example below)

    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 に答える