1


嘿,我正在尝试将使用 Tabhost 和活动的现有应用程序转换为带有片段的基于操作栏的应用程序。在此过程中遇到了实施可扩展列表的问题。

我在互联网上没有找到任何关于如何实现这个可扩展列表的简单示例。由于 ExpandableListActivity 没有片段的对应物,我认为我需要使用和扩展 ListFragment 但由于我对 android 很陌生,所以我不知道如何做。:)

如果有人能阐明如何使用带有片段的可扩展列表,我会很高兴。

谢谢。


附件也是我现有代码的片段。

已经有两个微调器的片段 1

public class Configurator_Fragment extends SherlockListFragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
    }
    final View v = inflater.inflate(R.layout.configurator_layout,
            container, false);

    final Spinner spinner = (Spinner) v
            .findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity(), R.array.array1,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    final Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            getActivity(), R.array.array2,
            android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);

    return v;
}
}

片段活动

public class TabActivity extends SherlockFragmentActivity  {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_navigation);


    if (savedInstanceState == null) {

        Fragment newFragment = new Configurator_Fragment();
        Fragment newFragment2 = new SecondFragment();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.pager, newFragment, "First");
        ft.add(R.id.pager, newFragment2, "Second");
        ft.commit();

   }

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Fragment 1");
    ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Fragment 2");

    mViewPager = (ViewPager)findViewById(R.id.pager);
    mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), mViewPager);

        mTabsAdapter.addTab(tab1, Configurator_Fragment.class, null);
        mTabsAdapter.addTab(tab2, SecondFragment.class, null);


    if (savedInstanceState != null) {
        getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("index"));
    }

}
@Override
protected void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
    outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());


}
public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final Context mContext;
    private final ActionBar mBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    private FragmentTransaction mCurTransaction = null;


    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mBar = bar;
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
     TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }


    @Override
    public Fragment getItem(int position) {
     TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Object tag = tab.getTag();
                for (int i=0; i<mTabs.size(); i++) {
                    if (mTabs.get(i) == tag) {

                            mViewPager.setCurrentItem(i);
                    }
                }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
 }

}
4

1 回答 1

1

先声明几句...

1)我没有使用过 ABS,所以我不确定会做出什么改变。
2) 我的 ExpandableListView 由数据库支持,而不是数组。我这么说是因为看起来你正在使用数组。无论如何,我还是发布了这个,希望它仍然可以为您提供一些方向。

也就是说,我只是通过扩展 Fragment 来完成它。

public class EventListFragment extends Fragment {

    private AttendanceDB mDbHelper;
    public static Cursor mGroupsCursor;
    private MyExpandableListAdapter mAdapter;
    private String mGroup;
    private String mChild;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.explistfragment, container, false);
        header = (TextView) v.findViewById(R.id.header1);
        header.setText(R.string.event_header);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        mGroup = extras.getString("Group");
        mChild = extras.getString("Child");
        mDbHelper = new AttendanceDB(getActivity());
        mDbHelper.open();
        fillData(mGroup, mChild);
    }

    private void fillData(String group, String child) {
        ExpandableListView lv;
        mGroupsCursor = mDbHelper.fetchGroup(group);
        getActivity().startManagingCursor(mGroupsCursor);
        mGroupsCursor.moveToFirst();
        lv = (ExpandableListView) getActivity().findViewById(R.id.explist);

        mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
                R.layout.explistlayout,
                R.layout.explistlayout1,
                new String[] { "_id" },
                new int[] { android.R.id.text1 },
                new String[] { child },
                new int[] { android.R.id.text1 });
        lv.setAdapter(mAdapter);
    }

    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

        public MyExpandableListAdapter(Cursor cursor, Context context,
                int groupLayout, int childLayout, String[] groupFrom,
                int[] groupTo, String[] childrenFrom, int[] childrenTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo,
                    childLayout, childrenFrom, childrenTo);
        }
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            Cursor childCursor = mDbHelper.fetchChildren(mGroup, groupCursor
                    .getString(groupCursor
                            .getColumnIndex(AttendanceDB.EVENT_ROWID)));
            getActivity().startManagingCursor(childCursor);
            childCursor.moveToFirst();
            return childCursor;
        }
    }

}
于 2012-06-17T16:12:34.323 回答