2016-03-20 3 views
0

私はAndroid用の電卓アプリをinfony2postfixメソッドを使って作成しています(これはJavaの方が比較的新しいので、少なくともいくつかの種類です)。ベースキーボードとファンクションキーボードの間をスワイプするViewPagerがあります。両方のキーボードは、単純なボタンを含むフラグメントです。スタックは私が実装したカスタムObject Stackクラスです。最初にキーボードをスワイプすると完全にスライドしますが、最初のビューの計算後、ViewPagerは少し遅れを開始します。なぜか分かりません。誰か提案はありますか?Android 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> 
ここ

はViewPagerためのコードであります

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); 
    } 
} 

答えて

0

あなたのビュー階層は巨大です。 LinearLayoutをネストしないで、代わりにRelativeLayoutを使用することで、何とか平坦化する必要があります。あなたは重い操作をしていませんが、あなたが経験している遅れは、インフレーション、レイアウト、ビューの描画によるものです。

階層ビューアを使用し、LINTチェックを実行します。 LINTは、レイアウトをフラット化するためのすべての可能な方法を教えてくれます。

+0

私はポイントを参照して、GridLayoutも同様ですか?そうすれば、私は1つのGridLayoutルート要素とその中に入れ子にされたすべてのボタンを持つことができます。 –