大佬教程收集整理的这篇文章主要介绍了Android:Fragment加载速度太慢,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个与Web服务器一起工作的应用程序,因此有很多查询要做.在我的片段中,名为“tab_profile”,我调用2个API查询,还有许多其他人在我的自定义ListView上加载图像.
所以我的问题是这个片段在加载(井连接)时需要1-2秒,甚至在连接不良时崩溃,例如3G.
public class tab_profile extends Fragment implements AdapterView.onItemClickListener { private String imagepath = null; private ImageView imageview = null; View rootView = null; private ArrayList<PollItem> arrayPolls; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceStatE) { rootView = inflater.inflate(R.layout.tab_profile,container,falsE); final Button button_follower = (Button) rootView.findViewById(R.id.button_follower); final Button button_following = (Button) rootView.findViewById(R.id.button_following); final TextView text_username = (TextView) rootView.findViewById(R.id.text_userName); imageview = (ImageView) rootView.findViewById(R.id.img_own_pp); final Session session = new Session(getActivity()); final Network network = new Network(getActivity()); text_username.setText(session.getValue("username")); if(session.getValue("username").length() < 3) { getActivity().finish(); return null; } JSONArray arr; String res = network.POST("{\"token\":\"" + session.getValue("token") + "\"}","profile.PHP"); try { JSONObject obj = new JSONObject(res); arr = obj.getJSONArray("data"); if (!obj.get("status").equals("false")) { button_follower.setText("Follower\n" + arr.getJSONObject(0).getString("follower")); button_following.setText("Following\n" + arr.getJSONObject(0).getString("following")); if (arr.getJSONObject(0).getString("pp").equals("true")) { network.setImageViewImage(imageview,"usermedia/pp/" + session.getValue("id")); } } } catch (JSONException E) { e.printStackTrace(); } arrayPolls = new ArrayList<>(); getList(); //listeners here return rootView; }
然后第二个主要函数叫做“getList()”:
public void getList() { Network netwok = new Network(getActivity()); Session session = new Session(getActivity()); String resp = netwok.POST("{\"token\":\""+session.getValue("token")+"\","+ "\"get\":\"true\""+"}","mypoll.PHP"); try { JSONObject obj = new JSONObject(resp); JSONArray arr = obj.getJSONArray("data"); if(obj.get("status").equals("false")) { return; } arrayPolls.clear(); for(int i=0; i<arr.length(); i++) { JSONObject ot = arr.getJSONObject(i); PollItem tmp = new PollItem(ot.geTint("ID"),ot.geTint("userid"),ot.getString("description"),ot.getString("username")); arrayPolls.add(tmp); } } catch (JSONException E) { e.printStackTrace(); } ListView list_poll_item = (ListView) rootView.findViewById(R.id.list_friend_poll); PollOverviewItemAdapter adapter = new PollOverviewItemAdapter(getActivity(),arrayPolls,tab_profile.this,imageview); list_poll_item.setAdapter(adapter); list_poll_item.setOnItemClickListener(this); }
private class POST_requEST extends AsyncTask<String,Integer,String>{ protected String doInBACkground(String... params) { String line; try { DefaulthttpClient httpClient = new DefaulthttpClient(); httpPost httpPost = new httpPost(params[0]); ArrayList<NameValuePair> postParameters; postParameters = new ArrayList<>(); postParameters.add(new BasicNameValuePair("json",query)); httpPost.setEntity(new UrlEncodedFormEntity(postParameters)); httpResponse httpResponse = httpClient.execute(httpPost); httpentity httpentity = httpResponse.getEntity(); line = EntityUtils.toString(httpentity); } catch (UnsupportedEncodingException E) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (MalformedURLException E) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (IOException E) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } return line; } protected void onPostExecute(String result) { super.onPostExecute(result); } }
public class DownloadImageTask extends AsyncTask<String,Void,Bitmap> { ImageView bmImage; public DownloadImageTask(ImageView bmImagE) { this.bR_483_11845@Image = bmImage; } protected Bitmap doInBACkground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception E) { Log.e("Error",e.getmessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } }
所以我认为这些是我的代码的重要部分.我真的希望找到解决这个问题的方法.我已经多次尝试加载我的UI,然后加载来自服务器的数据,但它是相同的.我通过使用“onStart()”尝试了这个.
提前致谢… :-)
以上是大佬教程为你收集整理的Android:Fragment加载速度太慢全部内容,希望文章能够帮你解决Android:Fragment加载速度太慢所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。