我正在使用ViewPager开发一个Activity,其中包含Fragments(每次最多4个片段),每个包含TableLayout.所以基本上加载了4个表.这是用于将数据加载到Fragment中的代码,该片段稍后附加到ViewPager:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { if (container == null) { return null; } LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); layoutParams.gravity=Gravity.CENTER_VERTICAL; layoutParams.setMargins(10,0); fragmetLayout = (LinearLayout)inflater.inflate(R.layout.grid_fragment_layout,container,false); table = (TableLayout) fragmetLayout.findViewById(R.id.tlGridTable); llParameterContainer = (LinearLayout) fragmetLayout.findViewById(R.id.llParametersContainer); tabName = (TextView) fragmetLayout.findViewById(R.id.tvTabName); tabName.setText(tab.getTabName()); //tabName.setLayoutParams(layoutParams); for (Parameter parameter : SGRaportManagerAppObj.getInstance().parametersRepository.getParametersRepository()) { ImageView parameterPoint = new ImageView(getActivity()); parameterPoint.setImageResource(R.drawable.parameter_chosen_point); parameterPoint.setPadding(10,10,0); parameterPoint.setLayoutParams(layoutParams); llParameterContainer.addView(parameterPoint); TextView parameterTextView = new TextView(getActivity()); parameterTextView.setText(parameter.getName()+":"); parameterTextView.setGravity(Gravity.CENTER); parameterTextView.setTextColor(getResources().getColor(R.color.my_black)); parameterTextView.setPadding(5,3,10); parameterTextView.setLayoutParams(layoutParams); llParameterContainer.addView(parameterTextView); TextView parameterChosenValueTextView = new TextView(getActivity()); parameterChosenValueTextView.setText(parameter.getChosenValue()); parameterChosenValueTextView.setGravity(Gravity.CENTER); parameterChosenValueTextView.setPadding(0,10); parameterChosenValueTextView.setLayoutParams(layoutParams); parameterChosenValueTextView.setTextColor(getResources().getColor(R.color.my_black)); llParameterContainer.addView(parameterChosenValueTextView); } //table.setStretchAllColumns(true); //table.setShrinkAllColumns(true); TableRow tableRow = new TableRow(getActivity()); TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT); tableRowParams.setMargins(2,0); tableRow.setLayoutParams(tableRowParams); Log.d(TAG,"TAB FROM FRAGMENT:"+tab.toString()); TableRow.LayoutParams tlparamsFirstColumn = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); tlparamsFirstColumn.setMargins(4,2,0); TableRow.LayoutParams tlparams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); tlparams.setMargins(0,0); //The First row for the column names. for (int i= 0; i < tab.getGrid().getGridColumnsList().size(); i++) { TextView tvName = new TextView(getActivity()); String columnName = tab.getGrid().getGridColumnsList().get(i).getGridColumnAlias(); tvName.setText(columnName); Log.d(TAG,"COLUMN ALIAS FROM FRAGMENT: "+columnName); if (i == 0) { tvName.setLayoutParams(tlparamsFirstColumn); } else { tvName.setLayoutParams(tlparams); } tvName.setGravity(Gravity.CENTER); tvName.setTextColor(getResources().getColor(R.color.my_white)); tvName.setPadding(10,10); tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_column_name_background)); tableRow.addView(tvName); } table.addView(tableRow); for (int j = 0; j < tab.getGrid().getGridData().getNumRows(); j++) { TableRow newRow = new TableRow(getActivity()); TableLayout.LayoutParams insideRowParams=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT); tableRowParams.setMargins(0,0); for (int k = 0; k < tab.getGrid().getGridData().getNumCols(j); k++) { TextView tvName = new TextView(getActivity()); String columnName = tab.getGrid().getGridData().get(j,k); tvName.setText(columnName); if ( (j % 2) == 0) { tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_first_row_background)); } else { tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.grid_second_row_background)); } if (k == 0) { tvName.setLayoutParams(tlparamsFirstColumn); } else { tvName.setLayoutParams(tlparams); } tvName.setGravity(Gravity.CENTER); tvName.setTextColor(getResources().getColor(R.color.my_black)); tvName.setPadding(10,10); newRow.addView(tvName); } table.addView(newRow); } return fragmetLayout; }
更新:
我的ViewPagerActivity布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/imageView3" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > </android.support.v4.view.ViewPager> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </TabHost> <ImageView android:id="@+id/imageView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="-3dp" android:background="@drawable/login_scr_top_bar" android:contentDescription="@drawable/login_scr_top_bar" /> <TextView android:id="@+id/tvReportName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:paddingLeft="15dp" android:paddingTop="13dp" android:text="@string/report_tabs" android:textColor="@color/my_black" android:textSize="18sp" android:textStyle="bold" /> <LinearLayout android:id="@+id/llTabsButtonsContainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:gravity="center_horizontal" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingTop="5dp" > </LinearLayout> <TextView android:id="@+id/tvReportTitle" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_alignBaseline="@+id/tvReportName" android:layout_alignBottom="@+id/tvReportName" android:layout_toLeftOf="@+id/bBackToParameters" android:layout_toRightOf="@+id/tvReportName" android:text="TextView" android:textSize="18sp" /> <Button android:id="@+id/bBackToParameters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tvReportTitle" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:background="@drawable/button_back_to_parameters_selector" android:clickable="true" android:onClick="backToParametersButtonOnClick" android:padding="3dp" android:text="@string/back_to_parameters" android:textColor="@color/my_white" android:textStyle="bold" /> </RelativeLayout>
和我的GridFramgnet布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/llParametersContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> <TextView android:id="@+id/tvTabName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/my_black" android:textStyle="bold" android:textSize="18sp" android:layout_marginLeft="10dp" android:text="It a test string for report Name"/> <ScrollView android:id="@+id/layout" android:layout_height="match_parent" android:scrollbars="horizontal|vertical" android:layout_width="match_parent" android:layout_marginTop="5dip" android:scrollbarStyle="outsideInset" android:fillViewport="true"> <HorizontalScrollView android:id="@+id/horizontalView" android:layout_height="wrap_content" android:scrollbars="horizontal|vertical" android:layout_width="wrap_content" android:layout_marginTop="5dip"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tlGridTable" > </TableLayout> </HorizontalScrollView> </ScrollView> </LinearLayout>
问题这对于3个片段加载大约需要10秒钟,因此用户在实际加载活动之前会获得黑屏10秒.我怎么能避免呢?运行在AsyncTask中填充表的所有过程将使我的ViewPager活动加载更快?甚至可以做到吗?
更新:
大部分时间都花费在填充/创建TableLayout上.如果从服务器收到大量数据,则需要创建大量TextView并在TableLayout中设置它们.
更新#2:
好吧,我正在消除这个问题,因为我理解我的需求(整个视图的水平和垂直滚动)我必须使用TableLayout,所以我需要等待这一次加载它.但是我需要找到一种方法来加载片段中的数据,至少在用户知道正在填充数据的情况下(显示适当的Dialog).在我开始片段初始化之前,我试图在ViewPager的onCreate中放置一个对话框,但由于某种原因它没有显示,我只看到黑色屏幕,直到片段被加载.
更新:
请参阅上一个问题,因为我知道无法确定加载时间:
问题:
有没有办法创建一个几乎为空的片段的ViewPager(每个片段只有一个单独的标题),将它们呈现给用户,然后,在活动已经加载并且可见(并且不显示黑屏)之后,呈现一个在显示对话框时对话框并填充片段?
任何帮助,将不胜感激.
谢谢.
解决方法
由于您使用了寻呼机,因此在启动活动时只加载第一个片段.
使用可见的进度条创建初始片段视图,并隐藏表并仅在切换到该片段时加载数据.
使用可见的进度条创建初始片段视图,并隐藏表并仅在切换到该片段时加载数据.
在活动中使用
mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { Fragment f = mAdapter.getFragment(mPager.getCurrentItem()); f.loadData(); // when you finish loading the data,hide the progress bar and show the table } @Override public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } });
要使用mAdapter.getFragment(),请从以下位置继承适配器:
public abstract class SmartFragmentPagerAdapter extends FragmentPagerAdapter{ private SparseArray<String> mFragmentTags; private FragmentManager mFragmentManager; public SmartFragmentPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); mFragmentManager = fragmentManager; mFragmentTags = new SparseArray<String>(); } @Override public Object instantiateItem(ViewGroup container,int position) { Object obj = super.instantiateItem(container,position); if (obj instanceof Fragment) { // record the fragment tag here. Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position,tag); } return obj; } public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); } public FragmentManager getFragmentManager(){ return mFragmentManager; } }