gpt4 book ai didi

Android - fragment 内的可扩展列表

转载 作者:行者123 更新时间:2023-11-30 04:09:28 25 4
gpt4 key购买 nike


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

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

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

谢谢。


附件也是我现有代码的一个 fragment 。

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

fragment Activity

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)我没有用过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;
}
}

}

关于Android - fragment 内的可扩展列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11072683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com