gpt4 book ai didi

android - 内部 fragment 中的 ViewPager,不显示,不可见

转载 作者:太空狗 更新时间:2023-10-29 15:53:31 26 4
gpt4 key购买 nike

我使用的是 ActionBar,它加载不同的 Fragment。这是在 FragmentActivity 类中完成的。其中一个 Fragent 应该有一个包含不同 Fragment 的 ViewPager。我现在不再收到任何错误,但是 ViewPager 和 PagerTitleStrip 以及 ViewPager 内的所有 Fragents 根本没有显示出来。 ActionBar 中的其他 fragment 正常工作。我正在使用支持包中的所有类。我在这里看了很多帖子,但我还没找到。

在派生自 FragmentAcivity 的 ActivityClass 中,我使用了 Listener:

SettingsActiviy.java

protected class SettingsTabsListener implements ActionBar.TabListener {

private Fragment fragment;

public SettingsTabsListener(Fragment fragment) {
this.fragment = fragment;
Log.e("SettingsTabListener","Contructor Fragment is " + fragment);
}

@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xaction=fragMgr.beginTransaction();

// Dont know if we really need that...
if (fragment instanceof SettingsActivityOverviewFragment) {
if (fragment == null) {
fragment = SettingsActivityOverviewFragment.newInstance(appWidgetId);
}
}
xaction.replace(R.id.FragmentPlaceholder, (Fragment)fragment);
xaction.commit();
}

@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xaction=fragMgr.beginTransaction();

xaction.remove(fragment);
xaction.commit();
}

SettingsActivityOverviewFragment.java

public class SettingsActivityOverviewFragment extends Fragment {
private static final Field sChildFragmentManagerField;
ViewPager viewPager;
PagerAdapter pagerAdapter;

public static final SettingsActivityOverviewFragment newInstance(int appWidgetId){
SettingsActivityOverviewFragment f = new SettingsActivityOverviewFragment();
Bundle bdl = new Bundle(1);
bdl.putInt("appWidgetId", appWidgetId);
f.setArguments(bdl);
return f;
}

/* 这是由 googles 空指针引起的修复,直到分离方法 */

static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
}
sChildFragmentManagerField = f;
}

@Override
public void onDetach() {
super.onDetach();

if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
}
}
}

/*修复结束*/

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.settings_frag_overview, container, false);
viewPager=(ViewPager)result.findViewById(R.id.overview_pager);
Log.e("SettingsActivityOverviewFragment","viewPager content is " + viewPager + ", " + viewPager.getHeight());

pagerAdapter = buildAdapter();
new SetAdapterTask().execute();

return(result);
}

private PagerAdapter buildAdapter() {
return(new SettingsOverviewPagerAdapter(getActivity(), getChildFragmentManager()));
}

public void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {

}

private class SetAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}

@Override
protected void onPostExecute(Void result) {

if(pagerAdapter != null) viewPager.setAdapter(pagerAdapter);
}
}

}

SettingsOverviewPagerAdapter.java

public class SettingsOverviewPagerAdapter extends FragmentPagerAdapter {
Context ctxt=null;

public SettingsOverviewPagerAdapter(Context ctxt, FragmentManager mgr) {
super(mgr);
this.ctxt=ctxt;
}

@Override
public int getCount() {
Log.i("SettingsOverviewPagerAdapter","getCount returns 10");
return(10);
}

@Override
public Fragment getItem(int position) {
return(EditorFragment.newInstance(position));
}

@Override
public String getPageTitle(int position) {
return(EditorFragment.getTitle(ctxt, position));
}
}

EditorFragment.java

public class EditorFragment extends Fragment {
private static final String KEY_POSITION = "position";

static EditorFragment newInstance(int position) {
EditorFragment frag = new EditorFragment();
Bundle args = new Bundle();

args.putInt(KEY_POSITION, position);
frag.setArguments(args);

return (frag);
}

static String getTitle(Context ctxt, int position) {
return (String.format("ctest", position + 1));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.editor, container, false);
EditText editor = (EditText) result.findViewById(R.id.editor);
int position = getArguments().getInt(KEY_POSITION, -1);

editor.setHint(getTitle(getActivity(), position));

return (result);
}
}

SettingsFragOverview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overview_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"/>
</android.support.v4.view.ViewPager>

EditorFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:gravity="left|top"
/>

最佳答案

这通常发生在:

  • 使用内部 fragment 作为 ViewPager 的父级
  • 父容器中有一个 ScrollView 。

关于android - 内部 fragment 中的 ViewPager,不显示,不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21476831/

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