ViewPager 中的一个片段没有显示
One Fragment inside ViewPager doesn't show up
我有一个 Tabbed Activity 托管一个 viewpager(使用 eclipse 向导自动生成)。我稍微修改了自动生成的 FragmentPagerAdapter,以便在每个选项卡上实例化一个特定的片段。最初我有 4 个选项卡,第一个选项卡有 3 个片段,最后一个选项卡仍然是自动生成的占位符,并且它们有效。当我用我的片段之一的实例替换最后一个占位符时,我遇到了以下问题:最后两个选项卡之一中的片段保持空白,如下所示:
_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|
[MasterFragment][RequestMap][MasterFragment][MasterFragment]
这保持blank_____________________|
此外,如果我进行 "random" 一系列操作(更改选项卡、单击按钮以执行某些操作,...)未显示交换的片段成为最后一个片段一个如下:
_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|
[MasterFragment][RequestMap][MasterFragment][MasterFragment]
这仍然是 blank__________________________________|
这是我的 FragmentPagerAdapter 的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case REQUEST_TAB:
masterFragment = new MasterFragment(MasterFragment.ALL_REQUEST);
return masterFragment;
case MAP_TAB:
requestMap = new RequestMap();
return requestMap;
case OWNER_TAB:
masterFragmentOwner = new MasterFragment(MasterFragment.OWNER_REQUEST);
System.out.println("I should have created the Owner MasterFragment");
return masterFragmentOwner;
case JOINED_TAB:
masterFragmentJoined = new MasterFragment(MasterFragment.JOINED_REQUEST);
return masterFragmentJoined;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case REQUEST_TAB:
return getString(R.string.request_tab_title).toUpperCase(l);
case MAP_TAB:
return getString(R.string.map_tab_title).toUpperCase(l);
case OWNER_TAB:
return getString(R.string.owner_tab_title).toUpperCase(l);
case JOINED_TAB:
return getString(R.string.joined_tab_title).toUpperCase(l);
default:
return "";
}
}
}
我省略了有关 viewPager 设置的代码,因为它是从 eclipse 向导自动生成的。虽然这是 MasterFragment class:
的代码
package it.polimi.frontend.fragment;
import it.polimi.appengine.entity.manager.model.Request;
import it.polimi.appengine.entity.manager.model.User;
import it.polimi.frontend.activity.R;
import it.polimi.frontend.util.QueryManager;
import it.polimi.frontend.util.QueryManager.OnRequestLoadedListener;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MasterFragment extends Fragment implements OnRequestLoadedListener, RequestList.OnRequestSelectedListener, RequestDetail.OnUserClickedListener{
private boolean twoPane;
private static View view;
public final static int ALL_REQUEST=0;
public final static int OWNER_REQUEST=1;
public final static int JOINED_REQUEST=2;
private int mode;
public MasterFragment(){
this.mode=0;
}
public MasterFragment(int mode){
this.mode=mode;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
QueryManager.getInstance().addListener(this);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_master,container, false);
List<Request> requests = new ArrayList<Request>();
RequestList requestListFragment = null;
switch (mode) {
case OWNER_REQUEST:
requests = QueryManager.getInstance().getCurrentUser().getRequests();
System.out.println("Dovrei aver recuperato le richieste dell'owner");
break;
case JOINED_REQUEST:
String mail=null;
if (QueryManager.getInstance().getCurrentUser()!=null){
User current =QueryManager.getInstance().getCurrentUser();
mail = current.getPwAccount() != null ?
current.getPwAccount()
: current.getGmailAccount() != null ?
current.getGmailAccount()
: current.getFbAccount() != null ?
current.getFbAccount()
: null;
requests = QueryManager.getInstance().getUserPartecipation(mail);
}
else
requests = new ArrayList<Request>();
break;
default: //ALL_REQUEST case and all the other possibilities
requests = QueryManager.getInstance().getRequests();
break;
}
requestListFragment = new RequestList(requests, mode);
if (view.findViewById(R.id.detail_container) != null) {//TABLET CASE
twoPane = true;
getChildFragmentManager().beginTransaction()
.replace(R.id.request_list_container,requestListFragment,RequestList.ID)
.commit();
} else { //PHONE CASE:
getChildFragmentManager().beginTransaction()
.replace(R.id.container,requestListFragment,RequestList.ID)
.commit();
}
} catch (InflateException e) {
// is already there, just return view as it is
e.printStackTrace();
}
return view;
}
@Override
public void onRequestSelected(int position, Request request) {
if (twoPane) {
DetailContainerFragment detailContFrag = new DetailContainerFragment(request,mode);
getChildFragmentManager().beginTransaction()
.replace(R.id.detail_container, detailContFrag, DetailContainerFragment.ID).commit();
} else {
switch (mode) {//This empty switch if for future changes
case OWNER_REQUEST:
break;
case JOINED_REQUEST:
break;
default://ALL_REQUEST
break;
}
RequestDetail fragment = new RequestDetail(request,mode);
Fragment reqList=getChildFragmentManager().findFragmentByTag(RequestList.ID);
getChildFragmentManager().beginTransaction()
.hide(reqList)
.addToBackStack(RequestDetail.ID)
.add(R.id.container,fragment,RequestDetail.ID)
.commit();
}
}
@Override
public void onUserClicked(User user,String requestId) {
if (!twoPane) {
FeedbackDetail fragment = new FeedbackDetail(user,this.mode,requestId);
Fragment reqDetail=getChildFragmentManager().findFragmentByTag(RequestDetail.ID);
getChildFragmentManager().beginTransaction()
.hide(reqDetail)
.addToBackStack(FeedbackDetail.ID)
.add(R.id.container,fragment,FeedbackDetail.ID)
.commit();
} else {
/*DetailContainerFragment should take care of it*/
}
}
@Override
public void onRequestLoaded(List<Request> requests) {
System.out.println("Ho caricato: "+requests.size());
RequestList requestListFragment = (RequestList)getChildFragmentManager().findFragmentByTag(RequestList.ID);
switch (mode) {//Also this switch is for future changes, but the requests list is anyway fetched and assigned to RequestList above
case OWNER_REQUEST:
//TODO
break;
case JOINED_REQUEST:
//TODO
break;
default: //ALL_REQUEST
if (requestListFragment!=null)
requestListFragment.setRequestAdapter(requests);
break;
}
}
}
如果我遗漏了一些重要的东西,请告诉我。
提前谢谢大家
编辑:
我忘了说我放置的System.out显示在控制台中,所以"blank"片段应该已经创建,并且应该已经通过他的onCreateView()。
我发现了这个错误。问题是
private static View view;
因为静态属性。删除 "static" 它再次起作用。实际上我什至现在都无法解释它是如何在第一个和最后一个选项卡上工作一段时间而不是在第三个选项卡上工作的,因为静态属性应该在 class 的所有实例之间共享。由于 ViewPager 的内部结构,问题似乎只出现在相邻的相同 MasterFragment 上,如果它们与它工作的任何其他片段交错。
我有一个 Tabbed Activity 托管一个 viewpager(使用 eclipse 向导自动生成)。我稍微修改了自动生成的 FragmentPagerAdapter,以便在每个选项卡上实例化一个特定的片段。最初我有 4 个选项卡,第一个选项卡有 3 个片段,最后一个选项卡仍然是自动生成的占位符,并且它们有效。当我用我的片段之一的实例替换最后一个占位符时,我遇到了以下问题:最后两个选项卡之一中的片段保持空白,如下所示:
_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|
[MasterFragment][RequestMap][MasterFragment][MasterFragment]
这保持blank_____________________|
此外,如果我进行 "random" 一系列操作(更改选项卡、单击按钮以执行某些操作,...)未显示交换的片段成为最后一个片段一个如下:
_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|
[MasterFragment][RequestMap][MasterFragment][MasterFragment]
这仍然是 blank__________________________________|
这是我的 FragmentPagerAdapter 的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case REQUEST_TAB:
masterFragment = new MasterFragment(MasterFragment.ALL_REQUEST);
return masterFragment;
case MAP_TAB:
requestMap = new RequestMap();
return requestMap;
case OWNER_TAB:
masterFragmentOwner = new MasterFragment(MasterFragment.OWNER_REQUEST);
System.out.println("I should have created the Owner MasterFragment");
return masterFragmentOwner;
case JOINED_TAB:
masterFragmentJoined = new MasterFragment(MasterFragment.JOINED_REQUEST);
return masterFragmentJoined;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case REQUEST_TAB:
return getString(R.string.request_tab_title).toUpperCase(l);
case MAP_TAB:
return getString(R.string.map_tab_title).toUpperCase(l);
case OWNER_TAB:
return getString(R.string.owner_tab_title).toUpperCase(l);
case JOINED_TAB:
return getString(R.string.joined_tab_title).toUpperCase(l);
default:
return "";
}
}
}
我省略了有关 viewPager 设置的代码,因为它是从 eclipse 向导自动生成的。虽然这是 MasterFragment class:
的代码package it.polimi.frontend.fragment;
import it.polimi.appengine.entity.manager.model.Request;
import it.polimi.appengine.entity.manager.model.User;
import it.polimi.frontend.activity.R;
import it.polimi.frontend.util.QueryManager;
import it.polimi.frontend.util.QueryManager.OnRequestLoadedListener;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MasterFragment extends Fragment implements OnRequestLoadedListener, RequestList.OnRequestSelectedListener, RequestDetail.OnUserClickedListener{
private boolean twoPane;
private static View view;
public final static int ALL_REQUEST=0;
public final static int OWNER_REQUEST=1;
public final static int JOINED_REQUEST=2;
private int mode;
public MasterFragment(){
this.mode=0;
}
public MasterFragment(int mode){
this.mode=mode;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
QueryManager.getInstance().addListener(this);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_master,container, false);
List<Request> requests = new ArrayList<Request>();
RequestList requestListFragment = null;
switch (mode) {
case OWNER_REQUEST:
requests = QueryManager.getInstance().getCurrentUser().getRequests();
System.out.println("Dovrei aver recuperato le richieste dell'owner");
break;
case JOINED_REQUEST:
String mail=null;
if (QueryManager.getInstance().getCurrentUser()!=null){
User current =QueryManager.getInstance().getCurrentUser();
mail = current.getPwAccount() != null ?
current.getPwAccount()
: current.getGmailAccount() != null ?
current.getGmailAccount()
: current.getFbAccount() != null ?
current.getFbAccount()
: null;
requests = QueryManager.getInstance().getUserPartecipation(mail);
}
else
requests = new ArrayList<Request>();
break;
default: //ALL_REQUEST case and all the other possibilities
requests = QueryManager.getInstance().getRequests();
break;
}
requestListFragment = new RequestList(requests, mode);
if (view.findViewById(R.id.detail_container) != null) {//TABLET CASE
twoPane = true;
getChildFragmentManager().beginTransaction()
.replace(R.id.request_list_container,requestListFragment,RequestList.ID)
.commit();
} else { //PHONE CASE:
getChildFragmentManager().beginTransaction()
.replace(R.id.container,requestListFragment,RequestList.ID)
.commit();
}
} catch (InflateException e) {
// is already there, just return view as it is
e.printStackTrace();
}
return view;
}
@Override
public void onRequestSelected(int position, Request request) {
if (twoPane) {
DetailContainerFragment detailContFrag = new DetailContainerFragment(request,mode);
getChildFragmentManager().beginTransaction()
.replace(R.id.detail_container, detailContFrag, DetailContainerFragment.ID).commit();
} else {
switch (mode) {//This empty switch if for future changes
case OWNER_REQUEST:
break;
case JOINED_REQUEST:
break;
default://ALL_REQUEST
break;
}
RequestDetail fragment = new RequestDetail(request,mode);
Fragment reqList=getChildFragmentManager().findFragmentByTag(RequestList.ID);
getChildFragmentManager().beginTransaction()
.hide(reqList)
.addToBackStack(RequestDetail.ID)
.add(R.id.container,fragment,RequestDetail.ID)
.commit();
}
}
@Override
public void onUserClicked(User user,String requestId) {
if (!twoPane) {
FeedbackDetail fragment = new FeedbackDetail(user,this.mode,requestId);
Fragment reqDetail=getChildFragmentManager().findFragmentByTag(RequestDetail.ID);
getChildFragmentManager().beginTransaction()
.hide(reqDetail)
.addToBackStack(FeedbackDetail.ID)
.add(R.id.container,fragment,FeedbackDetail.ID)
.commit();
} else {
/*DetailContainerFragment should take care of it*/
}
}
@Override
public void onRequestLoaded(List<Request> requests) {
System.out.println("Ho caricato: "+requests.size());
RequestList requestListFragment = (RequestList)getChildFragmentManager().findFragmentByTag(RequestList.ID);
switch (mode) {//Also this switch is for future changes, but the requests list is anyway fetched and assigned to RequestList above
case OWNER_REQUEST:
//TODO
break;
case JOINED_REQUEST:
//TODO
break;
default: //ALL_REQUEST
if (requestListFragment!=null)
requestListFragment.setRequestAdapter(requests);
break;
}
}
}
如果我遗漏了一些重要的东西,请告诉我。 提前谢谢大家
编辑: 我忘了说我放置的System.out显示在控制台中,所以"blank"片段应该已经创建,并且应该已经通过他的onCreateView()。
我发现了这个错误。问题是
private static View view;
因为静态属性。删除 "static" 它再次起作用。实际上我什至现在都无法解释它是如何在第一个和最后一个选项卡上工作一段时间而不是在第三个选项卡上工作的,因为静态属性应该在 class 的所有实例之间共享。由于 ViewPager 的内部结构,问题似乎只出现在相邻的相同 MasterFragment 上,如果它们与它工作的任何其他片段交错。