片段之间的切换显示与第一个片段相同的位置



我在这个问题上挣扎了几天。我的项目有 4 个片段,它们共享相同的浮动按钮。我已经设法引用了这些片段,以便允许浮动按钮访问每个片段,但是尽管我在每个片段之间切换,但它只显示第一个片段的位置。这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNavigationViewListener();
//set the drawer layout and navigation view
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//set item selected persist highlight
menuItem.setChecked(true);
//close drawer
mDrawerLayout.closeDrawers();
//set action as per the item selected
return true;
}
});
//create an adapter that knows where each fragment is
final LocationFragmentAdapter adapter = new LocationFragmentAdapter(this, getSupportFragmentManager());
mViewPager = findViewById(R.id.viewPager);
mViewPager.setAdapter(adapter);
adapter.getItem(position);
//set the function for the floating button to add a new item
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(LOG_TAG, "the current fragment " + adapter.getPageTitle(position));
if(position == 0) {
Intent intent = new Intent(MainActivity.this, EditorActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(MainActivity.this, RecipeEditor.class);
startActivity(intent);
}
}
});
}

这是我的片段适配器

public class LocationFragmentAdapter extends FragmentPagerAdapter {
final int count = 4;
private int [] tabTitle = {R.string.inventory_platform, R.string.product_platform, R.string.customer_platform, R.string.order_platform};
private Context mContext;
public LocationFragmentAdapter(Context context, FragmentManager fm){
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
if(position == 0) {
return new InventoryFragment();
}else if(position == 1) {
return new RecipeFragment();
}else if(position == 2) {
return new CustomerFragment();
}else {
return new OrderFragment();
}
}

那是我的库存片段

@Override
public void onActivityCreated(Bundle savedInstanceState){
getActivity().getSupportLoaderManager().initLoader(STOCK_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
final View rootView = inflater.inflate(R.layout.activity_main, container, false);
//set empty activity
ListView listView = rootView.findViewById(R.id.display_view);
View emptyView = rootView.findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
//create a new cursor adapter
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
listView.setAdapter(mCursorAdapter);
//create onClick listener to inflate the editor view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//create new intent to opend the editor
Intent intent = new Intent(getActivity(), EditorActivity.class);
//create Uri to pass the contents of the item selected to the edit view
Uri currentItem = ContentUris.withAppendedId(StoreEntry.CONTENT_URI, id);
intent.setData(currentItem);
startActivity(intent);
}
});
getLoaderManager().initLoader(STOCK_LOADER, null, this);
return rootView;
}

和我的食谱片段

}
@Override
public void onActivityCreated(Bundle savedInstanceState){
getActivity().getSupportLoaderManager().initLoader(RECIPE_lOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
final View rootView = inflater.inflate(R.layout.activity_main, container, false);
//set empty activity
ListView listView = rootView.findViewById(R.id.display_view);
View emptyView = rootView.findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
//create a new cursor adapter
mCursorAdapter = new RecipeCursorAdapter(getActivity(),null);
listView.setAdapter(mCursorAdapter);
//create onClick listener to inflate the editor view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//create new intent to opened the editor
Intent intent = new Intent(getActivity(), EditorActivity.class);
//create Uri to pass the contents of the item selected to the edit view
Uri currentItem = ContentUris.withAppendedId(RecipeEntry.CONTENT_URI, id);
intent.setData(currentItem);
startActivity(intent);
}
});

return rootView;
}

我还没有创建最后两个片段。如果您能找到错误,请告诉我。

日志消息始终显示清单片段

每次使用寻呼机侦听器选择页面时,您都必须更新位置

mPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageScrollStateChanged(int arg0) { }
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageSelected(int position) {
this.position = position
}
});

最新更新