【发布时间】:2016-03-20 14:51:54
【问题描述】:
我正在使用调车场 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);
}
}
【问题讨论】:
标签: java android android-fragments android-viewpager lag