gpt4 book ai didi

Fragment 内的 Android 选项卡布局未加载选项卡内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:06 35 4
gpt4 key购买 nike

我有带有 navigation drawercontainer 的 Main Activity,navigation drawer 有选项列表。抽屉选项替换了 container with fragments.container 加载 tab layout fragment 当我点击 fragtab 选项时,但是当我尝试通过单击相同的选项第二次加载它,它不显示 tab 内容。

fragment xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="2dp">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">


<LinearLayout
android:id="@+id/linearLayout_search"
android:layout_width="match_parent"
android:layout_height="48dp">


<ImageView
android:id="@+id/image_search_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"

android:padding="12dp"
android:src="@mipmap/ic_search_black_24dp" />


<EditText
android:id="@+id/edit_text_search"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center_vertical"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textCapWords"
android:paddingLeft="12dp"
android:paddingRight="8dp"
android:singleLine="true"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent" />

</LinearLayout>

</RelativeLayout>
</android.support.v7.widget.CardView>


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="#F4F1F1"
app:tabTextColor="@android:color/white" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/add_icon_white" />

fragment 类FragTabs.java

public class FragTabs extends Fragment {


public FragTabs() {
// Required empty public constructor
}

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_frag_tabs, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) view.findViewById(R.id.tabs);
//tabLayout.setupWithViewPager(viewPager);

tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});

FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewPager.getCurrentItem() == 0) {
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaymates()).commit();
} else {
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Addplaygroups()).commit();
}
}
});
return view;
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {

CharSequence Titles[] = {"Frag A", "Frag B"};

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
FragA tab1 = new FragA();
f = tab1;
break;
case 1:
FragB tab2 = new FragB();
f = tab2;
break;
}
return f;
}

@Override
public int getCount() {
return 2;
}

/*public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}*/

@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
}}

主要 Activity xml

  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/conta"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />

<FrameLayout
android:id="@+id/profile_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar" />

</RelativeLayout>

<fragment
android:id="@+id/navigation_drawer"
android:name="com.kshediv.playforever.Profile.NaviagtionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_naviagtion"
tools:layout="@layout/fragment_naviagtion" />
</android.support.v4.widget.DrawerLayout>

抽屉导航。

public class NaviagtionFragment extends Fragment {


public NaviagtionFragment() {
// Required empty public constructor
}

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

public void setUp(DrawerLayout dl, Toolbar toolbar) {
mDrawerLayout = dl;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), dl, toolbar, R.string.DrawerOpn, R.string.DrawerClose) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_naviagtion, container, false);

FrameLayout home = (FrameLayout) view.findViewById(R.id.home);
home.setOnClickListener(onClickListener);

FrameLayout fragtabs = (FrameLayout) view.findViewById(R.id.fragtabs);
playmates.setOnClickListener(onClickListener);

FrameLayout settings = (FrameLayout) view.findViewById(R.id.settings);
playmates.setOnClickListener(onClickListener);

return view;
}

public View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.home:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Home()).commit();
mDrawerLayout.closeDrawers();
break;
case R.id.playmates:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new PlayTabs()).commit();
mDrawerLayout.closeDrawers();
break;
case R.id.performance:
getFragmentManager().beginTransaction().replace(R.id.profile_container, new Settings()).commit();
mDrawerLayout.closeDrawers();
break;
}
}
};}

主要 Activity

 public class MainActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NaviagtionFragment nav_frg = (NaviagtionFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
nav_frg.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolBar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_profile, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}}

谢谢,

最佳答案

尝试使用

class ViewPagerAdapter extends FragmentStatePagerAdapter {
...
}

代替

class ViewPagerAdapter extends FragmentPagerAdapter {
...
}

关于Fragment 内的 Android 选项卡布局未加载选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314999/

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