我使用Two Fragment创建了一个Android应用程序, MainFragment - >的 WebviewFragment 即可。 MainFragment是GridView,它从 Json 链接获取网格项。当我通过按后退按钮从 WebviewFragment 导航回 MainFragment 时,Gridview会再次获取数据。对此有什么解决方案吗?
MainFragment
public class MainFragment extends Fragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://api....";
private ProgressDialog pDialog;
private List<Movie> movieList;
private GridView gridView;
private TextView scrollingtext;
private AppController appController;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
movieList = new ArrayList<Movie>();
View view = inflater.inflate(R.layout.main_fragment, container, false);
scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(true);
gridView = (GridView) view.findViewById(R.id.Grid_view);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), movieList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("LINK","http://www.kalerkantho.com/");
Fragment newFragment = new WebviewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment.setArguments(bundle);
transaction.replace(R.id.main_fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage(" Loading ...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
appController.getInstance().addToRequestQueue(movieReq);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
WebviewFragment
public class WebviewFragment extends Fragment {
WebView webView;
String newslink;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview,container,false);
newslink = getArguments().getString("LINK");
webView = (WebView) view.findViewById(R.id.webView);
webView.loadUrl(newslink);
webView.getSettings().setJavaScriptEnabled(true);
return view;
}
}
}
答案 0 :(得分:1)
您可以使用SharedPreferences保存一个标记,表明您是否已加载数据,然后在片段之间切换时检查此标志。
如果您的标志处于活动状态,您还应该将数据保存在SharedPreferences中,或者维护内部数据库并加载该数据,而不是再次转换JSON。
答案 1 :(得分:1)
您可以使用Fragment完成此操作。将主内容父布局设置为Framelayout,并在使用命令fragment.add按下按钮或所需内容时使Webview膨胀,并在backpress时使用fragment.remove。
答案 2 :(得分:1)
view
时,膨胀null
不会popback
。所以只需在替换Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view ==null){//null means fragment replaced and not null means popbacked
View view = inflater.inflate(R.layout.main_fragment, container, false);
init();
}
return view;
}
private void init() {
scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(true);
gridView = (GridView) view.findViewById(R.id.Grid_view);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), movieList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("LINK", "http://www.kalerkantho.com/");
Fragment newFragment = new WebviewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment.setArguments(bundle);
transaction.replace(R.id.main_fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage(" Loading ...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
appController.getInstance().addToRequestQueue(movieReq);
}
答案 3 :(得分:0)
您还可以在setRetainInstance(true);
中使用MainFragment
来保持实例状态。
希望这有帮助。