我正在尝试在滑动视图片段(名为BROWSE)中包含制表符片段。访问BROWSE时,将完成与远程服务器的连接,检索数据并在选项卡内的gridview中显示,单击其他选项卡,建立新连接,并使用新数据填充gridview。
问题是我无法保存选项卡的状态,我不希望每次都建立连接,只有在首次单击选项卡时才会建立与数据库的连接,如果单击选项卡再次,我希望gridview保持状态。
这是BROWSE片段的onCreate方法,在这个方法中,我正在初始化一个加载器以从远程服务器获取数据:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (t == 0) {
Log.v("BROWSE","++++++++ Inside onCreate t = 0 ++++++++");
tweets = Collections.emptyList();
// This is our REST action.
Uri twitterSearchUri = new Uri.Builder()
.scheme("http")
.authority(ip)
.path("browse.php")
.appendQueryParameter("SpecialOffer", "0")
.build();
Bundle params = new Bundle();
// params.putString("q", "android");
params.get("salim slem");
// These are the loader arguments. They are stored in a Bundle because
// LoaderManager will maintain the state of our Loaders for us and
// reload the Loader if necessary. This is the whole reason why
// we have even bothered to implement RESTLoader.
Bundle args = new Bundle();
args.putParcelable(ARGS_URI, twitterSearchUri);
args.putParcelable(ARGS_PARAMS, params);
//For loading Images
//--------------------------------------------------
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
//--------------------------------------------------
// Initialize the Loader.
getSupportLoaderManager().initLoader(LOADER_TWITTER_SEARCH, args, this);
}
if(t == 1) {
Log.v("BROWSE","++++++++ Inside onCreate t = 1 ++++++++");
tweets = Collections.emptyList();
// This is our REST action.
Uri twitterSearchUri = new Uri.Builder()
.scheme("http")
.authority(ip)
.path("browse.php")
.appendQueryParameter("SpecialOffer", "1")
.build();
Bundle params = new Bundle();
// params.putString("q", "android");
params.get("salim slem");
// These are the loader arguments. They are stored in a Bundle because
// LoaderManager will maintain the state of our Loaders for us and
// reload the Loader if necessary. This is the whole reason why
// we have even bothered to implement RESTLoader.
Bundle args = new Bundle();
args.putParcelable(TAB1_ARGS_URI, twitterSearchUri);
args.putParcelable(TAB1_ARGS_PARAMS, params);
//For loading Images
//--------------------------------------------------
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
//--------------------------------------------------
// Initialize the Loader.
getSupportLoaderManager().initLoader(TAB1_LOADER_TWITTER_SEARCH, args, this);
}
}
这里是onCreateView方法,其中选项卡与gridview一起创建
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("BROWSE", "Inside OnCreateView");
View view = inflater.inflate(R.layout.gridview, container, false);
gridView = (GridView) view.findViewById(R.id.gridViewImage);
gridView.setAdapter(new ImageTextAdapter(view.getContext(), LOADING));
// When an item in the gridview is clicked
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
FragmentPosition = 2;
Log.v("BROWSE: POSITION OF ITEM IN GRIDVIEW","## "+position+" ##");
DialogFragment newFragment = new BrowseDialogFragment(position);
newFragment.show(getSupportFragmentManager(), "missiles");
}
});
// creating tabs
mTabHost = (TabHost) view.findViewById(R.id.testtabhost1);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.gridViewImage));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.gridViewImage));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.gridViewImage));
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int i = mTabHost.getCurrentTab();
Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i);
if (i ==0) {
Bundle tempBundle = new Bundle();
t = 0;
onCreate(tempBundle);
}
if (i ==1) {
Bundle tempBundle = new Bundle();
t = 1;
onCreate(tempBundle);
}
}
});
return view;
}
我想我的问题是我无法从onCreateView向onCreate提供“savedInstanceState”。我的问题是,如何在选项卡中保存状态?也许使用给onCreate的包,但我还没找到怎么做!
提前感谢您的帮助!!
这就是我正在谈论的用户界面:
再次按下标签时,这就是用户界面:
答案 0 :(得分:0)
尝试为该案例使用数据库。很容易意识到这一点: tutorial
那么你应该做什么:
我希望这会有所帮助。
最好的问候
狩猎
答案 1 :(得分:0)
savedInstanceState变量传递给 onViewCreated 方法,该方法在 onCreateView 之后立即调用。
您应该尝试仅在onCreateView(gridView,mTabHost等)中扩充布局并初始化View字段,并在onViewCreated中恢复状态(或设置默认状态)