Android - 片段中的可扩展列表

时间:2012-06-17 15:50:54

标签: android listview android-fragments expandablelistview fragment


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

我还没有在互联网上找到关于如何实现此可扩展列表的任何简单示例。由于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;
}
}

FragmentActivity

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) {

    }
 }

}

1 个答案:

答案 0 :(得分:1)

首先是几个免责声明......

1)我没有使用ABS,所以我不确定会有什么变化。
2)我的ExpandableListView由数据库支持,而不是数组。我这说是因为看起来你正在使用数组。无论如何我都希望它可以为你提供一些指导。

那就是说,我只是通过扩展片段来完成它。

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

}