0

这是一个不错的。我有一个简单的 XML 布局:menuprincipal.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">
    <LinearLayout
        android:id="@+id/llContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <LinearLayout 
            android:id="@+id/menuSlide"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="@color/black"
            android:layout_weight="0.01">

        </LinearLayout>
        <LinearLayout 
            android:id="@+id/resultadosEquipo"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@color/white"
            android:layout_weight="0.99"
            android:orientation="vertical">
            <LinearLayout
                android:id="@+id/layoutPrueba"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@color/white"
                >
                <ListView android:id="@+id/listNoticias"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
            </LinearLayout>
        </LinearLayout>        
    </LinearLayout>
</FrameLayout>

然后我有两个类来扩展“menuSlide”布局:

ExpandAnimation.java

    package app.rosunad.animations;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;


public class ExpandAnimation extends Animation implements Animation.AnimationListener{

    private View view;
    private static int ANIMATION_DURATION;
    private static final String LOG_CAT = "ExpandAnimation";
    private int lastWidth;
    @SuppressWarnings("unused")
    private int fromWidth;
    private int toWidth;
    @SuppressWarnings("unused")
    private static int STEP_SIZE=30;

    public ExpandAnimation(View v,int fromWidth, int toWidth, int duration){
        Log.v(LOG_CAT, "Entramos en el constructor del ExpandAnimation");
        view = v;
        ANIMATION_DURATION = duration;
        this.fromWidth = fromWidth;
        this.toWidth = toWidth;

        setDuration(ANIMATION_DURATION);
        setRepeatCount(duration);
        setFillAfter(false);
        setInterpolator(new AccelerateInterpolator());
        setAnimationListener(this);
        startNow();
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
        Log.v(LOG_CAT, "Entra en el onAnimationRepeat");
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = lastWidth += toWidth/ANIMATION_DURATION;
        view.setLayoutParams(lyp);
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
        Log.v(LOG_CAT, "Entra en el onAnimationStart");
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = 0;
        view.setLayoutParams(lyp);
        lastWidth=0;
    }

}

和 CollapseAnimation.java

    package app.rosunad.animations;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;

@SuppressWarnings("unused")
public class CollapseAnimation extends Animation implements Animation.AnimationListener{

    private View view;
    private static int ANIMATION_DURATION;
    private int lastWidth;
    private int fromWidth;
    private int toWidth;
    private static int STEP_SIZE=30;

    public CollapseAnimation(View v,int fromWidth,int duration){
        this.view = v;
        LayoutParams lyp = v.getLayoutParams();
        this.fromWidth = lyp.width;
        this.toWidth = lyp.width;
        ANIMATION_DURATION = duration;
        setRepeatCount(ANIMATION_DURATION);
        setFillAfter(false);
        setInterpolator(new AccelerateInterpolator());
        setAnimationListener(this);
    }
    @Override
    public void onAnimationEnd(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = this.lastWidth-(this.toWidth/ANIMATION_DURATION);
        this.lastWidth = lyp.width;
        view.setLayoutParams(lyp);
    }

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub
        LayoutParams lyp = view.getLayoutParams();
        this.lastWidth = lyp.width;
    }

}

这一切都由 te 活动 MenuPrincipal.java 控制:

    public class MenuPrincipal extends ActionBarActivity {
    public static String URL_BASE_FOTOS = "http://test";
    public static String URL_BASE = "http://test/";
    public static String URL_NOTICIAS = URL_BASE + "getNoticias.php";
    public static final String EXTRA_IDUSUARIO = "app.rosunad.lls.idusuario";
    public static final String LOG_TAG = "MenuPrincipal";
    public int idUsuario = 0;
    public boolean icon_home_changed = false;

    public ArrayList<Noticia> arrNoticias = new ArrayList<Noticia>();

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Intent antIntent = getIntent();
        idUsuario = antIntent.getIntExtra(EXTRA_IDUSUARIO, 0);

        setHomeNavigation();
        setContentView(R.layout.menuprincipal);

        new GetNoticias().execute(URL_BASE_FOTOS);
    }

    public boolean onCreateOptionsMenu(Menu menu){
        Log.v(LOG_TAG,"En el onCreateOptionsMenu del MenuPrincipal");
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menuactionbar, menu);

        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.menu_refresh:
                Toast.makeText(getApplicationContext(), "Fake Refresh", Toast.LENGTH_SHORT).show();
                getActionBarHelper().setRefreshActionItemState(true);
                getWindow().getDecorView().postDelayed(new Runnable(){
                    public void run(){
                        getActionBarHelper().setRefreshActionItemState(false);
                    }
                }, 1000);
                break;
            case android.R.id.home:
                getActionBarHelper().setHomeIconItemState(icon_home_changed);
                LinearLayout menuSlide = (LinearLayout)findViewById(R.id.menuSlide);
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int screenWidth = metrics.widthPixels;

                if(icon_home_changed){
                    menuSlide.startAnimation(new CollapseAnimation(menuSlide, (int)(screenWidth*0.7), 10));
                }else{
                    menuSlide.startAnimation(new ExpandAnimation(menuSlide, 0, (int)(screenWidth*0.7), 10));
                }


                // Vamos a iniciar la animación del frame
                icon_home_changed = !icon_home_changed;
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @TargetApi(14)
    public void setHomeNavigation(){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            this.getActionBar().setHomeButtonEnabled(true);
        }
    }

    public void publishNoticias(){
        Log.v(LOG_TAG,"Entra en el publishNoticias");
        // Aqui es donde mostramos los datos de las noticias recogidas en GetNoticias
        NoticiasAdapter adap = new NoticiasAdapter(this,R.layout.fila_noticias,arrNoticias);
        ListView listView = (ListView)findViewById(R.id.listNoticias);
        listView.setAdapter(adap);
    }

    public class GetNoticias extends AsyncTask<String,Void,ArrayList<Noticia>>{

        @Override
        protected ArrayList<Noticia> doInBackground(String... params) {
            // TODO Auto-generated method stub
            // Aqui hacemos la consulta a la base de datos para recoger las noticias
            Log.v(LOG_TAG,"Entra en el doInBackground");
            Bitmap icono;
            ArrayList<Noticia> response = new ArrayList<Noticia>();
            JSONObject fila;
            try {

                HttpClass cox = new HttpClass();
                cox.setURL(URL_NOTICIAS);

                cox.SqlEx();

                // Ahora recogemos los valores y vamos creando el Array
                if(cox.GetJResponseArray()!=null){
                    CheckResponse ck = new CheckResponse(cox.GetJResponseArray());

                    if(ck.HasRowsArray()){
                        Log.v(LOG_TAG,"Tiene filas");
                        for(int i = 0 ; i < cox.GetJResponseArray().length()-1; i ++){
                            fila = cox.GetJResponseArray().getJSONObject(i);

                            if(fila.getInt("id_noticia")!=0){
                                // Debemos cargar primero el icono de la noticia
                                if(fila.getInt("id_foto")!=0){
                                    icono = BitmapFactory.decodeStream((InputStream) new URL(URL_BASE_FOTOS + "foto" + fila.getInt("id_foto") + "P.jpg").getContent());
                                }else{
                                    icono = BitmapFactory.decodeStream((InputStream) new URL(URL_BASE_FOTOS + "nop.gif").getContent());
                                }
                                Log.v(LOG_TAG,"Añade elementos");
                                response.add( new Noticia(fila.getInt("id_noticia"),fila.getString("titular"),icono ));
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPostExecute(ArrayList<Noticia> result){
            Log.v(LOG_TAG,"Entra en el onPostExecute");
            Log.v(LOG_TAG,"El resultado tiene: " + result.size());
            arrNoticias = result;
            publishNoticias();
        }

    }
}

在我引入 ListView 元素之前,进行不同的测试一切正常,因为当我调用 ExpandAnimation (单击主页按钮)仅到达构造函数时,onAnimationStart 不会触发。但是,这很有趣,如果只是在单击主页按钮后单击 ListView 的一个项目,动画就会开始。:o

重新单击主页按钮启动 CollapseAnimation 没有问题。如果删除列表视图一切正常。

4

1 回答 1

0

你的ListViewid 必须是@android:id/list.

如果你想格式化你的ListView,你必须创建另一个 XML 文件来概述你将如何实现列表视图的每一行。

希望这可以帮助。

于 2012-08-21T15:34:28.113 回答