2016-03-20 15 views
0

Ich schreibe eine Taschenrechner-App für Android mit dem Rangierbahnhof Infix2postfix-Methode (naja zumindest ist eine Art von ihr, da ich relativ neu in Java bin). Ich habe ein ViewPager-Set für das Wischen zwischen Basis-Tastatur und Funktionstastatur. Beide Tastaturen sind Fragmente, die einfache Schaltflächen enthalten. Der Stapel ist eine benutzerdefinierte Objektstapelklasse, die ich implementiert habe. Das erste Mal, wenn die Tastaturen durchwischt werden, gleiten sie perfekt, aber nach den ersten Fiew-Berechnungen beginnt der ViewPager ein wenig nachzulassen, und ich verstehe wirklich nicht warum. Hat jemand irgendwelche Vorschläge? HierAndroid ViewPager Animation Lag nach dem ersten Swipe

ist der Code für 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; 
    } 
} 

die Basis-Tastatur:

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

die Funktionstastatur:

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

Antwort

0

Ihre Ansicht Hierarchie ist riesig. Sie sollten es irgendwie flach machen, indem Sie verschachtelte LinearLayout s vermeiden und stattdessen RelativeLayout verwenden. Obwohl Sie keine schweren Operationen haben, ist die Verzögerung, die Sie erleben, auf Inflation, Layout und Zeichnen der Ansichten zurückzuführen.

Verwenden Sie den Hierarchie-Viewer und führen Sie die LINT-Prüfung aus. LINT wird Ihnen alle Möglichkeiten erklären, die Layouts zu glätten.

+0

Ich sehe den Punkt, würde GridLayout auch tun? Auf diese Weise hätte ich nur ein GridLayout-Wurzelelement und alle darin eingebetteten Schaltflächen. –

Verwandte Themen