Android 当XML文件包含ImageView时,ViewPager有点滞后(结巴)
我有一个ViewPager,每个页面都有一个新的片段。我制作了一个片段来测试我的ViewPager,这个片段的布局如下所示:Android 当XML文件包含ImageView时,ViewPager有点滞后(结巴),android,android-fragments,imageview,android-viewpager,Android,Android Fragments,Imageview,Android Viewpager,我有一个ViewPager,每个页面都有一个新的片段。我制作了一个片段来测试我的ViewPager,这个片段的布局如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:
<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>
我使用了三个相同的片段来测试刷卡功能。滑动并不太糟糕,但与不包含任何ImageView的片段相比,它仍然相对滞后。所以我怀疑我在刷卡时的每一个动作都需要更长的时间,因为图像需要在屏幕上重新绘制,这可能是罪魁祸首吗
这是我的主屏幕处理程序,它包含可以导航到的可能项目的列表视图
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;
}
}
public类MainScreenHandler扩展活动{
私有ArrayList subScreenFragmentList=新ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main\u screen\u handler\u布局);
子屏幕碎片列表。添加(新的子屏幕碎片(“行驶高度”,新的FragmentRideHeightOverview(),新的FragmentRideHeightOverview(),
新的FragmentRideHeightOverview(),null));
添加(新的子屏幕碎片(“Toe”),新的FragmentRideHeightOverview(),新的FragmentRideHeightOverview(),
新的FragmentRideHeightOverview(),null));
String[]listViewNameArray=新字符串[subScreenFragmentList.size()];
对于(int i=0;i
还有我的子屏幕处理程序,它处理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...
}
}
公共类子ScreenHandler扩展碎片活动{
@抑制警告(“未选中”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sub\u屏幕\u处理程序\u布局);
ArrayList fragmentList=新建ArrayList();
addAll((ArrayList)getIntent().getSerializableExtra(“碎片列表”));
用于(对象项:碎片列表){
如果(!(片段的项目实例)){
抛出新的ClassCastException(“包含非片段对象的列表:+item.getClass().getCanonicalName());
}
}
ViewPager ViewPager=(ViewPager)findViewById(R.id.ViewPager);
setAdapter(新的myPagerAdapter(getFragmentManager(),fragmentList));
CirclePageIndicator CirclePageIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
circlePageIndicator.setViewPager(viewPager);
}
类myPagerAdapter扩展了FragmentPagerAdapter{
ArrayList fragmentList=新建ArrayList();
公共myPagerAdapter(FragmentManager fm、ArrayList fragments){
超级(fm);
fragmentList.addAll(片段);
}
@凌驾
公共片段getItem(int i){
返回碎片列表。获取(i);
}
@凌驾
public int getCount(){
return fragmentList.size();//此处尚未实现任何内容。。。
}
}
注:
我无法使代码正确排列,因此忽略错误的格式。有人能为我提供解决方案吗?我也有同样的问题。你找到解决方案了吗?