2011-01-03 9 views
15

Por lo tanto, he seguido este hilo en particular (How to stop scrolling in a Gallery Widget?) pero no puedo hacer que funcione correctamente.Creación de una galería personalizada: anulación de onFling

He creado una clase personalizada de MyGallery que amplía la Galería. He agregado el código en el enlace de arriba ... ¿Se supone que debo agregar <com.example.mygallery al archivo XML? Si es así, ¿también agrego la importación al archivo java o no es necesario debido al archivo XML? Estoy muy confundido.

Quiero simplemente hacer que la galería se mueva una imagen a la vez por aventura.

archivo XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/carlot_background" 
    > 
<com.gallerytest.mygallery 
    android:id="@+id/thisgallery" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

mygallery.java:

package com.gallerytest; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.widget.Gallery; 

public class mygallery extends Gallery { 

    public mygallery(Context ctx, AttributeSet attrSet) { 
     super(ctx); 
     // TODO Auto-generated constructor stub 
    } 

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
      return e2.getX() > e1.getX(); 
     } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ 
     int kEvent; 
     if(isScrollingLeft(e1, e2)){ //Check if scrolling left 
     kEvent = KeyEvent.KEYCODE_DPAD_LEFT; 
     } 
     else{ //Otherwise scrolling right 
     kEvent = KeyEvent.KEYCODE_DPAD_RIGHT; 
     } 
     onKeyDown(kEvent, null); 
     return true; 
    } 

} 

main.java: paquete com.gallerytest;

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mygallery gallery = (mygallery) findViewById(R.id.thisgallery); 

     gallery.setAdapter(new AddImgAdp(this)); 

     gallery.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, int position, long id) { 

       Toast.makeText(main.this, "Position=" + position, Toast.LENGTH_SHORT).show(); 
      } 

     }); 

    } 

    public class AddImgAdp extends BaseAdapter { 
     int GalItemBg; 
     private Context cont; 


     private Integer[] Imgid = { 
       R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5}; 

     public AddImgAdp(Context c) { 
      cont = c; 
      TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1); 
      GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
      typArray.recycle(); 
     } 

     public int getCount() { 
      return Imgid.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView imgView = new ImageView(cont); 

      imgView.setImageResource(Imgid[position]); 

      imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imgView.setBackgroundResource(0x0106000d); 
      imgView.setLayoutParams(new mygallery.LayoutParams(300, 240)); 

      return imgView; 
     } 
    } 
} 

Me gustaría algo de ayuda. ¡¡Gracias!!

~ Rick

Respuesta

23

simplemente añada el parámetro attrset al constructor de su galería de encargo:

super(ctx, attrSet); 

Esto funcionó para mí. Leo Vannucci

+0

¡Funciona! Gracias chicos. – user560837

+3

¿Quizás podría aceptar esto como la respuesta final? –

+0

me ayudó también .......... gracias – viv

3

Sí. Solo debe usar com.gallerytest.mygallery en lugar de Gallery en XML. Todo funcionará bien porque mygallery es una subclase de Gallery. No es necesario importar en XML.

+0

Ok. Entonces parece que mi código setAdapter ya no funciona correctamente con el código anterior. Todo lo que hice fue cambiar de una vista de Galería a una Galería personalizada. ¿Alguien tiene ganas de probar el código y ver si puede identificar el problema? Probablemente estoy pasando por alto algo simple. GRACIAS! ... aunque parece que dejé fuera el código AddImgAdp ... se encuentra en el enlace de arriba. – user560837

1

Cambiar el XML era la clave ... He estado recibiendo TypeCastException por algún tiempo y no he podido encontrar el motivo en mi código. Finalmente encontré en esta publicación "Deberías usar com.gallerytest.mygallery en lugar de Gallery en XML" y resolvió mi problema. muchas gracias.

Cuestiones relacionadas