Android viewflipper和gallery

时间:2012-10-30 10:40:07

标签: android android-view android-gallery

我正在尝试开发一个包含带有视图鳍状肢的图库的应用程序。

图库显示所有图像。我想要做的是,视图翻板中显示的图像应该在图库中突出显示。

目前我可以在onClick上突出显示它。但是无法在两者之间进行通信。

我尝试使用以下方法

viewflipper.getDisplayedChild();

但它不起作用。

以下是我的代码

public class SliderActivity extends Activity 
{
int mFlipping = 0 ;
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
android.widget.ViewFlipper flipper;
Gallery gallery;


private int[] IMAGE_IDS = 
{
    R.drawable.android0, R.drawable.android1,           R.drawable.android2,R.drawable.android3,R.drawable.android4
};



@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mygame);
    flipper = (android.widget.ViewFlipper) findViewById(R.id.flipper1);

    for(int i=0;i<IMAGE_IDS.length;i++)
    {
    //  This will create dynamic image view and add them to ViewFlipper
        ImageView image = new ImageView(getApplicationContext());
        image.setBackgroundResource(IMAGE_IDS[i]);
        flipper.addView(image);
    }


    gallery=(Gallery)findViewById(R.id.gallery1);
    gallery.setAdapter(new ImageAdapter(this));

    if(mFlipping==0){
        /** Start Flipping */
        flipper.startFlipping();
        mFlipping=1;
        //mButton.setText(R.string.str_btn_stop);
    }
    else{
        /** Stop Flipping */
        flipper.stopFlipping();     
        mFlipping=0;
    //  mButton.setText(R.string.str_btn_start);
    }

}


public class ImageAdapter extends BaseAdapter 
{
       int mGalleryItemBackground;
       private Context mContext;

       public ImageAdapter(Context c) 
       {
           mContext = c;
           TypedArray a = obtainStyledAttributes(R.styleable.Theme);
           mGalleryItemBackground = a.getResourceId(
             R.styleable.Theme_android_galleryItemBackground,0);
           a.recycle();
       }

       public int getCount() 
       {
           return IMAGE_IDS.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 i = new ImageView(mContext);
           i.setImageResource(IMAGE_IDS[position]);
           i.setLayoutParams(new Gallery.LayoutParams(150, 100));
           i.setScaleType(ImageView.ScaleType.FIT_XY);
           i.setBackgroundResource(mGalleryItemBackground);

           return i;
       }
    }

public void onClick(View v) {

    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
  }

}

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题...我认为你需要使用setOnItemSelectedListener ......

gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapter, View gallery,
                int position, long arg3) {          

            myPosition = position;
                           //tell your flipper something useful here using the current position of the gallery

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

在onItemSelected回调中与你的鳍状肢交谈。