感谢您的关注
我在导航抽屉片段中实现viewpager时遇到了一些问题。例如,我有一个导航抽屉,其中有两个片段fragment1和fragment2,如图所示。当活动开始时,fragment1显示其viewpager片段。但当我转到fragment2并返回fragment1时,viewpager中的片段就消失并显示为空白。如果您有任何问题,请提出一些答案。
屏幕截图
先谢了
MainActivity.java
package com.ansoft.shutterbox;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends FragmentActivity {
public static DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
ArrayList<ListItemData> data;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
insertData();
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(MainActivity.this,navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
int width=getResources().getDisplayMetrics().widthPixels/3;
DrawerLayout.LayoutParams params=(android.support.v4.widget.DrawerLayout.LayoutParams)mDrawerList.getLayoutParams();
params.width=width;
mDrawerList.setLayoutParams(params);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
protected void onResume() {
Toast.makeText(getApplicationContext(), "Resumed", Toast.LENGTH_LONG).show();
super.onResume();
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new MyProfileFragment();
break;
case 2:
fragment = new EventsFragment();
break;
case 3:
fragment = new TopUpFragment();
break;
case 4:
fragment = new InboxFragment();
break;
case 5:
fragment = new ContactUsFragment();
break;
case 6:
fragment = new SettingsFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
Fragment1.java
package com.ansoft.shutterbox.Fragments;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class EventsFragment extends Fragment {
public static ViewPager mViewPager;
public EventsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View cv = inflater.inflate(R.layout.fragment_events, container, false);
InitializeView(cv);
ImageView icDrawer = (ImageView)cv.findViewById(R.id.ic_drawer);
icDrawer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
adapter = new TabPagerAdapter(getFragmentManager());
mViewPager.setAdapter(adapter);
tvBacktoList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowNormal();
}
});
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
tab1.setBackgroundResource(R.drawable.bg_event_selected);
tab2.setBackgroundColor(getResources().getColor(android.R.color.transparent));
imgtab1.setImageResource(R.drawable.ic_list_selected);
imgtab2.setImageResource(R.drawable.ic_calender_default);
tvtab1.setTextColor(getResources().getColor(R.color.colorPrimary));
tvtab2.setTextColor(getResources().getColor(R.color.white));
break;
case 1:
tab2.setBackgroundResource(R.drawable.bg_event_selected);
tab1.setBackgroundColor(getResources().getColor(android.R.color.transparent));
imgtab1.setImageResource(R.drawable.ic_list_default);
imgtab2.setImageResource(R.drawable.ic_calender_selected);
tvtab1.setTextColor(getResources().getColor(R.color.white));
tvtab2.setTextColor(getResources().getColor(R.color.colorPrimary));
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return cv;
}
class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new EventListFragment();
case 1:
return new EventCalenderFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
2条答案
按热度按时间aiazj4mn1#
我只是用一个简单的技巧解决了这个问题。在我的
TabPagerAdapter
类中,我用了getChildFragmentManager()
而不是getFragmentManager()
。我不知道这有什么帮助,但它确实有帮助。v6ylcynt2#
一个真正好的做法是始终检查viewpager不是空的,或者null。有了这个,看到导航的深度,在应用程序内,片段内的一个关键是上下文管理,除了传统的“这个”活动,另一个也是很好地管理示例和示例键,以便当它们被检索时,导航对用户来说是舒适的,由于我在片段级别上离开它或先前导航的状态或位置,所以它可能会被删除。
在主要或片段中
"如果你用视频传呼机"
"而不是"
在viewpager的内部或外部,我们添加了一个抽屉菜单导航,然后是示例的处理,调用的深度在片段中。如果进程的周期管理得不好,理解一个做得很好的代码是如何发生故障的,这甚至是更有趣的。检索示例是隐式操作的确切步骤。
快乐代码!
有用链接: