我有一个ViewPager,每个页面都有一个新的片段。我制作了一个片段来测试我的ViewPager,这个片段有一个如下所示的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:textAppearance="@android:style/TextAppearance.Large"
android:text="@string/textView_ride_height_overview_heading"
android:id="@+id/textView_ride_height_overview_heading"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="2dp"
android:background="#FFFF3BB2"
android:id="@+id/view_ride_height_heading"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="@drawable/image_border"
android:src="@drawable/ride_height_overview"
android:contentDescription="@string/imageView_ride_height_overview_content_description"
android:id="@+id/imageView_ride_height_overview"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView_ride_height_overview"
android:layout_marginTop="10dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="@string/textView_ride_height_overview_summary"/>
</RelativeLayout>
</ScrollView>
</LinearLayout>
我使用了三个相同的片段来测试滑动功能。滑动不是太糟糕但与不包含任何图像视图的碎片相比仍然相对滞后。所以我怀疑我刷卡时所做的每一个动作都需要更长时间,因为图像需要在屏幕上重新绘制,这可能是罪魁祸首吗?
这是我的主屏幕处理程序,它包含您可以导航到的可能项目的列表视图。
public class MainScreenHandler extends Activity {
private ArrayList<SubScreenFragments> subScreenFragmentList = new ArrayList<SubScreenFragments>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_handler_layout);
subScreenFragmentList.add(new SubScreenFragments("Ride height", new FragmentRideHeightOverview(), new FragmentRideHeightOverview(),
new FragmentRideHeightOverview(), null));
subScreenFragmentList.add(new SubScreenFragments("Toe", new FragmentRideHeightOverview(), new FragmentRideHeightOverview(),
new FragmentRideHeightOverview(), null));
String[] listViewNameArray = new String[subScreenFragmentList.size()];
for (int i = 0; i < listViewNameArray.length; i++) {
listViewNameArray[i] = subScreenFragmentList.get(i).getListViewName();
}
ListView listview = (ListView) findViewById(R.id.listview);
listview.setClickable(true);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row_layout, listViewNameArray);
listview.setAdapter(listAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent myIntent = new Intent(getBaseContext(), SubScreenHandler.class);
Bundle myBundle = new Bundle();
myBundle.putSerializable("fragmentList", subScreenFragmentList.get(i).getFragments());
myIntent.putExtras(myBundle);
startActivity(myIntent);
}
});
}
}
class SubScreenFragments {
String listViewName;
Fragment fragmentTab1, fragmentTab2, fragmentTab3, fragmentTab4;
public SubScreenFragments(String listViewName, Fragment fragmentTab1, Fragment fragmentTab2, Fragment fragmentTab3, Fragment fragmentTab4) {
this.listViewName = listViewName;
this.fragmentTab1 = fragmentTab1;
this.fragmentTab2 = fragmentTab2;
this.fragmentTab3 = fragmentTab3;
this.fragmentTab4 = fragmentTab4;
}
public String getListViewName() {
return listViewName;
}
public ArrayList<Fragment> getFragments() {
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
if (fragmentTab1 != null)
fragmentList.add(fragmentTab1);
if (fragmentTab2 != null)
fragmentList.add(fragmentTab2);
if (fragmentTab3 != null)
fragmentList.add(fragmentTab3);
if (fragmentTab4 != null)
fragmentList.add(fragmentTab4);
return fragmentList;
}
}
我的子屏幕处理程序,它处理viewpager的片段填充。
public class SubScreenHandler extends FragmentActivity{
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_screen_handler_layout);
ArrayList<Fragment> fragmentList=new ArrayList<Fragment>();
fragmentList.addAll((ArrayList<Fragment>) getIntent().getSerializableExtra("fragmentList"));
for (Object item : fragmentList) {
if (!(item instanceof Fragment)) {
throw new ClassCastException("List contained non-Fragment objects: " + item.getClass().getCanonicalName());
}
}
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new myPagerAdapter(getFragmentManager(), fragmentList));
CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circlePageIndicator.setViewPager(viewPager);
}
class myPagerAdapter extends FragmentPagerAdapter {
ArrayList <Fragment> fragmentList = new ArrayList<Fragment>();
public myPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
fragmentList.addAll(fragments);
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size(); //nothing implemented here yet...
}
}
注意: 我无法正确排列代码,因此请忽略错误的格式。