0
我正在使用分流碼infix2postfix方法爲Android編寫一個計算器應用程序(至少是某種程度上的,因爲我是相對較新的Java)。我有一個ViewPager設置用於在基本鍵盤和功能鍵盤之間滑動。這兩個鍵盤都是包含簡單按鈕的片段。該堆棧是我實現的一個自定義對象堆棧類。第一次滑動鍵盤完美滑動,但在第一次計算後,ViewPager開始落後了,我真的不明白爲什麼。有人有任何建議嗎?首次刷卡後Android 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);
}
}
我明白了,GridLayout能做到這一點嗎?這樣我只有一個GridLayout根元素和嵌套在其中的所有按鈕。 –