[英]Android ViewPager animation lag after first swipe
我正在使用调车场infix2postfix方法为Android编写一个计算器应用程序(至少是某种形式,因为我对Java比较陌生)。 我设置了一个ViewPager,用于在基本键盘和功能键盘之间滑动。 两个键盘都是包含简单按钮的片段。 堆栈是我实现的自定义对象堆栈类。 第一次轻扫键盘可以完美滑动,但是在进行了第一次计算之后,ViewPager开始滞后了一点,我真的不明白为什么。 有人有什么建议吗?
这是ViewPager的代码:
public class MainActivity extends AppCompatActivity {
private static final int NUM_PAGES = 2;
private Fragment[] keypads = new Fragment[NUM_PAGES];
private ViewPager pager;
private PagerAdapter pageradapter;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
pager = (ViewPager) findViewById(R.id.keypadview);
pageradapter = new KeypadSliderAdapter(getSupportFragmentManager());
pager.setAdapter(pageradapter);
keypads[0] = new NumericKeypad();
keypads[1] = new FunctionKeypad();
}
private class KeypadSliderAdapter extends FragmentStatePagerAdapter
{
public KeypadSliderAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
return keypads[position];
}
@Override
public int getCount()
{
return NUM_PAGES;
}
}
基本键盘:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="2"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="3"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="+"
android:onClick="keyPressed"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="4"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="5"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="6"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="-"
android:onClick="keyPressed"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="7"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="8"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="9"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="*"
android:onClick="keyPressed"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="."
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:text="0"
android:onClick="keyPressed"/>
<Button android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:text="="
android:onClick="equalsKeyPressed"/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:onClick="keyPressed"/>
</LinearLayout>
package com.soloinfor.calculator;
import android.os.Bundle;
import android.view.View;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class NumericKeypad extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.numeric_keypad, container, false);
return view;
}
}
功能键盘:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="sin()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="sin("/>
<Button android:text="cos()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="cos("/>
<Button android:text="tan()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="tan("/>
<Button android:text="π"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="π"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="ln()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="ln("/>
<Button android:text="log\u2081\u2080()"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAllCaps="false"
android:onClick="functionKeyPressed"
android:tag="log\u2081\u2080("/>
</LinearLayout>
package com.soloinfor.calculator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
public class FunctionKeypad extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.function_keypad, container, false);
}
}
您的视图层次结构庞大。 您应该通过避免嵌套的LinearLayout
并改用RelativeLayout
来使它变平。 尽管您没有繁重的操作,但是您遇到的滞后是由于通货膨胀,布局和绘制视图而造成的。
使用层次结构查看器并运行LINT检查。 LINT将告诉您所有可能的扁平化布局方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.