问题描述
我创建了两个片段的Android应用程序, MainFragment - > WebviewFragment 。该MainFragment是其中的GridView从获取的的Json 链接网格项目。当我从导航回 WebviewFragment 为 MainFragment 按pressing后退按钮时,GridView再次获取数据。请问有什么办法解决这个?
MainFragment
公共类MainFragment扩展片段{
//日志标签 私有静态最后弦乐TAG = MainActivity.class.getSimpleName(); //电影JSON网址 私有静态最终字符串URL =HTTP:// API ......;
私人ProgressDialog pDialog;
私人列表<电影及GT; movieList;
私人GridView控件GridView控件;
私人TextView的scrollingtext;
私人AppController的AppController的; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ movieList =新的ArrayList<电影及GT;(); 查看查看= inflater.inflate(R.layout.main_fragment,集装箱,FALSE);
scrollingtext =(TextView中)view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(真);
GridView控件=(GridView控件)view.findViewById(R.id.Grid_view);
最后CustomListAdapter适配器=新CustomListAdapter(getActivity(),movieList);
gridView.setAdapter(适配器); gridView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){ 束束=新包();
bundle.putString(LINK,http://www.kalerkantho.com/); 片段newFragment =新WebviewFragment();
FragmentTransaction交易= getFragmentManager()调用BeginTransaction()。
newFragment.setArguments(包); transaction.replace(R.id.main_fragment_container,newFragment);
transaction.addToBackStack(NULL);
器transaction.commit();
}
});
pDialog =新ProgressDialog(getActivity());
//发出HTTP请求之前显示进度对话框
pDialog.setMessage(正在加载...);
pDialog.show(); //创建凌空要求OBJ
JsonArrayRequest movieReq =新JsonArrayRequest(网址,新Response.Listener< JSONArray>(){
@覆盖
公共无效onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog(); //解析JSON
的for(int i = 0; I< response.length();我++){
尝试{ JSONObject的OBJ = response.getJSONObject(I)
电影电影=新电影();
movie.setTitle(obj.getString(标题));
movie.setThumbnailUrl(obj.getString(图像));
//添加电影电影阵列
movieList.add(电影); }赶上(JSONException E){
e.printStackTrace();
} } //通知有关数据变化表适配器
//使之呈现与更新后的数据列表视图
adapter.notifyDataSetChanged();
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(TAG,错误:+ error.getMessage());
hidePDialog(); }
}); //添加请求,请求队列
。appController.getInstance()addToRequestQueue(movieReq); 返回视图。
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
hidePDialog();
} 私人无效hidePDialog(){
如果(pDialog!= NULL){
pDialog.dismiss();
pDialog = NULL;
}
}
WebviewFragment
公共类WebviewFragment扩展片段{
web视图的WebView;
串新闻链接; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.fragment_webview,集装箱,FALSE); 新闻链接= getArguments()的getString(链接)。 web视图=(的WebView)view.findViewById(R.id.webView);
webView.loadUrl(新闻链接)
webView.getSettings()setJavaScriptEnabled(真)。
返回视图。
}
}
}
充气视图
不会空
当你 popback
。所以刚充气视图时替换片段
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
如果(查看== NULL){// null表示片段取代,而不是null意味着popbacked
查看查看= inflater.inflate(R.layout.main_fragment,集装箱,FALSE);
在里面();
} 返回视图。
}私人无效的init(){
scrollingtext =(TextView中)view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(真);
GridView控件=(GridView控件)view.findViewById(R.id.Grid_view);
最后CustomListAdapter适配器=新CustomListAdapter(getActivity(),movieList);
gridView.setAdapter(适配器);
gridView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
束束=新包();
bundle.putString(LINK,http://www.kalerkantho.com/); 片段newFragment =新WebviewFragment();
FragmentTransaction交易= getFragmentManager()调用BeginTransaction()。
newFragment.setArguments(包); transaction.replace(R.id.main_fragment_container,newFragment);
transaction.addToBackStack(NULL);
器transaction.commit();
}
});
pDialog =新ProgressDialog(getActivity());
//发出HTTP请求之前显示进度对话框
pDialog.setMessage(正在加载...);
pDialog.show(); //创建凌空要求OBJ
JsonArrayRequest movieReq =新JsonArrayRequest(网址,新Response.Listener< JSONArray>(){
@覆盖
公共无效onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog(); //解析JSON
的for(int i = 0; I< response.length();我++){
尝试{ JSONObject的OBJ = response.getJSONObject(I)
电影电影=新电影();
movie.setTitle(obj.getString(标题));
movie.setThumbnailUrl(obj.getString(图像));
//添加电影电影阵列
movieList.add(电影); }赶上(JSONException E){
e.printStackTrace();
}
}
//通知有关数据变化表适配器
//使之呈现与更新后的数据列表视图
adapter.notifyDataSetChanged();
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(TAG,错误:+ error.getMessage());
hidePDialog(); }
});
//添加请求,请求队列
。appController.getInstance()addToRequestQueue(movieReq);
}
I created an Android application with Two Fragment, MainFragment --> WebviewFragment. The MainFragment is GridView which fetch the Grid Item from Json link. When I navigate back from WebviewFragment to MainFragment by pressing the back button, the Gridview fetches the data again. Is there any solution for this?
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;
}
}
}
inflated view
wouldn't be null
when you popback
. so just inflate view when you replace 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);
}
这篇关于如何再次停止加载GridView的时候我preSS后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!