android – 包含TableLayouts的片段的ViewPager加载缓慢:

前端之家收集整理的这篇文章主要介绍了android – 包含TableLayouts的片段的ViewPager加载缓慢:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用ViewPager开发一个Activity,其中包含Fragments(每次最多4个片段),每个包含TableLayout.所以基本上加载了4个表.这是用于将数据加载到Fragment中的代码,该片段稍后附加到ViewPager:
  1. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
  2. {
  3. if (container == null) {
  4. return null;
  5. }
  6.  
  7. LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  8. layoutParams.gravity=Gravity.CENTER_VERTICAL;
  9. layoutParams.setMargins(10,0);
  10.  
  11. fragmetLayout = (LinearLayout)inflater.inflate(R.layout.grid_fragment_layout,container,false);
  12. table = (TableLayout) fragmetLayout.findViewById(R.id.tlGridTable);
  13. llParameterContainer = (LinearLayout) fragmetLayout.findViewById(R.id.llParametersContainer);
  14. tabName = (TextView) fragmetLayout.findViewById(R.id.tvTabName);
  15. tabName.setText(tab.getTabName());
  16. //tabName.setLayoutParams(layoutParams);
  17.  
  18. for (Parameter parameter : SGRaportManagerAppObj.getInstance().parametersRepository.getParametersRepository())
  19. {
  20. ImageView parameterPoint = new ImageView(getActivity());
  21. parameterPoint.setImageResource(R.drawable.parameter_chosen_point);
  22. parameterPoint.setPadding(10,10,0);
  23. parameterPoint.setLayoutParams(layoutParams);
  24. llParameterContainer.addView(parameterPoint);
  25.  
  26. TextView parameterTextView = new TextView(getActivity());
  27. parameterTextView.setText(parameter.getName()+":");
  28. parameterTextView.setGravity(Gravity.CENTER);
  29. parameterTextView.setTextColor(getResources().getColor(R.color.my_black));
  30. parameterTextView.setPadding(5,3,10);
  31. parameterTextView.setLayoutParams(layoutParams);
  32. llParameterContainer.addView(parameterTextView);
  33. TextView parameterChosenValueTextView = new TextView(getActivity());
  34. parameterChosenValueTextView.setText(parameter.getChosenValue());
  35. parameterChosenValueTextView.setGravity(Gravity.CENTER);
  36. parameterChosenValueTextView.setPadding(0,10);
  37. parameterChosenValueTextView.setLayoutParams(layoutParams);
  38. parameterChosenValueTextView.setTextColor(getResources().getColor(R.color.my_black));
  39. llParameterContainer.addView(parameterChosenValueTextView);
  40.  
  41. }
  42. //table.setStretchAllColumns(true);
  43. //table.setShrinkAllColumns(true);
  44. TableRow tableRow = new TableRow(getActivity());
  45. TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
  46. tableRowParams.setMargins(2,0);
  47. tableRow.setLayoutParams(tableRowParams);
  48. Log.d(TAG,"TAB FROM FRAGMENT:"+tab.toString());
  49.  
  50.  
  51. TableRow.LayoutParams tlparamsFirstColumn = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
  52. tlparamsFirstColumn.setMargins(4,2,0);
  53. TableRow.LayoutParams tlparams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
  54. tlparams.setMargins(0,0);
  55.  
  56. //The First row for the column names.
  57. for (int i= 0; i < tab.getGrid().getGridColumnsList().size(); i++)
  58. {
  59. TextView tvName = new TextView(getActivity());
  60. String columnName = tab.getGrid().getGridColumnsList().get(i).getGridColumnAlias();
  61. tvName.setText(columnName);
  62. Log.d(TAG,"COLUMN ALIAS FROM FRAGMENT: "+columnName);
  63. if (i == 0)
  64. {
  65. tvName.setLayoutParams(tlparamsFirstColumn);
  66. }
  67. else
  68. {
  69. tvName.setLayoutParams(tlparams);
  70. }
  71. tvName.setGravity(Gravity.CENTER);
  72. tvName.setTextColor(getResources().getColor(R.color.my_white));
  73. tvName.setPadding(10,10);
  74. tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_column_name_background));
  75. tableRow.addView(tvName);
  76. }
  77. table.addView(tableRow);
  78.  
  79. for (int j = 0; j < tab.getGrid().getGridData().getNumRows(); j++)
  80. {
  81. TableRow newRow = new TableRow(getActivity());
  82. TableLayout.LayoutParams insideRowParams=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
  83. tableRowParams.setMargins(0,0);
  84. for (int k = 0; k < tab.getGrid().getGridData().getNumCols(j); k++)
  85. {
  86. TextView tvName = new TextView(getActivity());
  87. String columnName = tab.getGrid().getGridData().get(j,k);
  88. tvName.setText(columnName);
  89.  
  90. if ( (j % 2) == 0)
  91. {
  92. tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_first_row_background));
  93. }
  94. else
  95. {
  96. tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_second_row_background));
  97. }
  98. if (k == 0)
  99. {
  100. tvName.setLayoutParams(tlparamsFirstColumn);
  101. }
  102. else
  103. {
  104. tvName.setLayoutParams(tlparams);
  105. }
  106. tvName.setGravity(Gravity.CENTER);
  107. tvName.setTextColor(getResources().getColor(R.color.my_black));
  108. tvName.setPadding(10,10);
  109. newRow.addView(tvName);
  110. }
  111. table.addView(newRow);
  112. }
  113. return fragmetLayout;
  114. }

更新:

我的ViewPagerActivity布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <TabHost
  7. android:id="@android:id/tabhost"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_below="@+id/imageView3" >
  11.  
  12. <LinearLayout
  13. android:orientation="vertical"
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent" >
  16. <FrameLayout
  17. android:id="@android:id/tabcontent"
  18. android:layout_width="0dp"
  19. android:layout_height="0dp"
  20. android:layout_weight="0"/>
  21.  
  22. <android.support.v4.view.ViewPager
  23. android:id="@+id/pager"
  24. android:layout_width="fill_parent"
  25. android:layout_height="0dp"
  26. android:layout_weight="1" >
  27. </android.support.v4.view.ViewPager>
  28.  
  29. <TabWidget
  30. android:id="@android:id/tabs"
  31. android:orientation="horizontal"
  32. android:layout_gravity="center_horizontal"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content" />
  35. </LinearLayout>
  36. </TabHost>
  37.  
  38. <ImageView
  39. android:id="@+id/imageView3"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_marginTop="-3dp"
  43. android:background="@drawable/login_scr_top_bar"
  44. android:contentDescription="@drawable/login_scr_top_bar" />
  45.  
  46. <TextView
  47. android:id="@+id/tvReportName"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_gravity="top|left"
  51. android:paddingLeft="15dp"
  52. android:paddingTop="13dp"
  53. android:text="@string/report_tabs"
  54. android:textColor="@color/my_black"
  55. android:textSize="18sp"
  56. android:textStyle="bold" />
  57.  
  58. <LinearLayout
  59. android:id="@+id/llTabsButtonsContainer"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_alignParentBottom="true"
  63. android:layout_alignParentLeft="true"
  64. android:layout_alignParentRight="true"
  65. android:gravity="center_horizontal"
  66. android:orientation="horizontal"
  67. android:paddingBottom="5dp"
  68. android:paddingTop="5dp" >
  69. </LinearLayout>
  70.  
  71. <TextView
  72. android:id="@+id/tvReportTitle"
  73. android:layout_width="wrap_content"
  74. android:layout_height="20dp"
  75. android:layout_alignBaseline="@+id/tvReportName"
  76. android:layout_alignBottom="@+id/tvReportName"
  77. android:layout_toLeftOf="@+id/bBackToParameters"
  78. android:layout_toRightOf="@+id/tvReportName"
  79. android:text="TextView"
  80. android:textSize="18sp" />
  81.  
  82. <Button
  83. android:id="@+id/bBackToParameters"
  84. android:layout_width="wrap_content"
  85. android:layout_height="wrap_content"
  86. android:layout_alignBottom="@+id/tvReportTitle"
  87. android:layout_marginRight="10dp"
  88. android:layout_alignParentRight="true"
  89. android:background="@drawable/button_back_to_parameters_selector"
  90. android:clickable="true"
  91. android:onClick="backToParametersButtonOnClick"
  92. android:padding="3dp"
  93. android:text="@string/back_to_parameters"
  94. android:textColor="@color/my_white"
  95. android:textStyle="bold" />
  96.  
  97. </RelativeLayout>

和我的GridFramgnet布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:id="@+id/llParametersContainer"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="horizontal" >
  12. </LinearLayout>
  13.  
  14. <TextView
  15. android:id="@+id/tvTabName"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textColor="@color/my_black"
  19. android:textStyle="bold"
  20. android:textSize="18sp"
  21. android:layout_marginLeft="10dp"
  22. android:text="It a test string for report Name"/>
  23.  
  24. <ScrollView
  25. android:id="@+id/layout"
  26. android:layout_height="match_parent"
  27. android:scrollbars="horizontal|vertical"
  28. android:layout_width="match_parent"
  29. android:layout_marginTop="5dip"
  30. android:scrollbarStyle="outsideInset"
  31. android:fillViewport="true">
  32.  
  33. <HorizontalScrollView
  34. android:id="@+id/horizontalView"
  35. android:layout_height="wrap_content"
  36. android:scrollbars="horizontal|vertical"
  37. android:layout_width="wrap_content"
  38. android:layout_marginTop="5dip">
  39.  
  40. <TableLayout
  41. android:layout_width="match_parent"
  42. android:layout_height="match_parent"
  43. android:id="@+id/tlGridTable" >
  44. </TableLayout>
  45. </HorizontalScrollView>
  46. </ScrollView>
  47. </LinearLayout>

问题这对于3个片段加载大约需要10秒钟,因此用户在实际加载活动之前会获得黑屏10秒.我怎么能避免呢?运行在AsyncTask中填充表的所有过程将使我的ViewPager活动加载更快?甚至可以做到吗?

更新:
大部分时间都花费在填充/创建TableLayout上.如果从服务器收到大量数据,则需要创建大量TextView并在TableLayout中设置它们.

更新#2:
好吧,我正在消除这个问题,因为我理解我的需求(整个视图的水平和垂直滚动)我必须使用TableLayout,所以我需要等待这一次加载它.但是我需要找到一种方法来加载片段中的数据,至少在用户知道正在填充数据的情况下(显示适当的Dialog).在我开始片段初始化之前,我试图在ViewPager的onCreate中放置一个对话框,但由于某种原因它没有显示,我只看到黑色屏幕,直到片段被加载.

更新:

请参阅上一个问题,因为我知道无法确定加载时间:

问题:
有没有办法创建一个几乎为空的片段的ViewPager(每个片段只有一个单独的标题),将它们呈现给用户,然后,在活动已经加载并且可见(并且不显示黑屏)之后,呈现一个在显示对话框时对话框并填充片段?

任何帮助,将不胜感激.

谢谢.

解决方法

由于您使用了寻呼机,因此在启动活动时只加载第一个片段.
使用可见的进度条创建初始片段视图,并隐藏表并仅在切换到该片段时加载数据.

在活动中使用

  1. mPager.setOnPageChangeListener(new OnPageChangeListener() {
  2.  
  3. @Override
  4. public void onPageSelected(int position) {
  5. Fragment f = mAdapter.getFragment(mPager.getCurrentItem());
  6. f.loadData(); // when you finish loading the data,hide the progress bar and show the table
  7.  
  8. }
  9.  
  10. @Override
  11. public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
  12.  
  13. }
  14.  
  15. @Override
  16. public void onPageScrollStateChanged(int state) {
  17. }
  18. });

要使用mAdapter.getFragment(),请从以下位置继承适配器:

  1. public abstract class SmartFragmentPagerAdapter extends FragmentPagerAdapter{
  2.  
  3. private SparseArray<String> mFragmentTags;
  4. private FragmentManager mFragmentManager;
  5.  
  6. public SmartFragmentPagerAdapter(FragmentManager fragmentManager) {
  7. super(fragmentManager);
  8. mFragmentManager = fragmentManager;
  9. mFragmentTags = new SparseArray<String>();
  10. }
  11.  
  12. @Override
  13. public Object instantiateItem(ViewGroup container,int position) {
  14. Object obj = super.instantiateItem(container,position);
  15. if (obj instanceof Fragment) {
  16. // record the fragment tag here.
  17. Fragment f = (Fragment) obj;
  18. String tag = f.getTag();
  19. mFragmentTags.put(position,tag);
  20. }
  21. return obj;
  22. }
  23.  
  24. public Fragment getFragment(int position) {
  25. String tag = mFragmentTags.get(position);
  26. if (tag == null)
  27. return null;
  28. return mFragmentManager.findFragmentByTag(tag);
  29. }
  30.  
  31. public FragmentManager getFragmentManager(){
  32. return mFragmentManager;
  33. }
  34.  
  35. }

猜你在找的Android相关文章