Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.example.android.apis.R;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.GestureDetector;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.GestureDetector.SimpleOnGestureListener;
- import android.view.View.OnTouchListener;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.Toast;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import android.widget.AdapterView.OnItemClickListener;
- public class Gallery1 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gallery_1);
- // Reference the Gallery view
- final Gallery g = (Gallery) findViewById(R.id.gallery);
- // Set the adapter to our custom adapter (below)
- g.setAdapter(new ImageAdapter(this));
- // Set a item click listener, and just Toast the clicked position
- g.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView parent, View v, int position, long id) {
- Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
- }
- });
- // Gesture detection
- final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(g));
- OnTouchListener gestureListener = new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- boolean retVal = gestureDetector.onTouchEvent(event);
- int action = event.getAction();
- if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
- retVal = true;
- onUp();
- }
- return retVal;
- }
- public void onUp() {
- // Here I am merely copying the Gallery's onUp() method.
- for (int i = g.getChildCount() - 1; i >= 0; i--) {
- g.getChildAt(i).setPressed(false);
- }
- g.setPressed(false);
- }
- };
- g.setOnTouchListener(gestureListener);
- // We also want to show context menu for longpressed items in the gallery
- registerForContextMenu(g);
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
- menu.add(R.string.gallery_2_text);
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
- Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
- return true;
- }
- public class ImageAdapter extends BaseAdapter {
- int mGalleryItemBackground;
- public ImageAdapter(Context c) {
- mContext = c;
- // See res/values/attrs.xml for the <declare-styleable> that defines
- // Gallery1.
- TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
- mGalleryItemBackground = a.getResourceId(
- R.styleable.Gallery1_android_galleryItemBackground, 0);
- a.recycle();
- }
- public int getCount() {
- return mImageIds.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(mImageIds[position]);
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- i.setLayoutParams(new Gallery.LayoutParams(136, 88));
- // The preferred Gallery item background
- i.setBackgroundResource(mGalleryItemBackground);
- return i;
- }
- private Context mContext;
- private Integer[] mImageIds = {
- R.drawable.gallery_photo_1,
- R.drawable.gallery_photo_2,
- R.drawable.gallery_photo_3,
- R.drawable.gallery_photo_4,
- R.drawable.gallery_photo_5,
- R.drawable.gallery_photo_6,
- R.drawable.gallery_photo_7,
- R.drawable.gallery_photo_8
- };
- }
- public class MyGestureDetector extends SimpleOnGestureListener {
- private Gallery gallery;
- public MyGestureDetector(Gallery gallery) {
- this.gallery = gallery;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- return gallery.onFling(e1, e2, velocityX, velocityY);
- }
- }
- }
- setContentView(R.layout.somelayout);
- orientation = getResources().getConfiguration().orientation;
- if ( orientation == Configuration.ORIENTATION_LANDSCAPE )
- {
- Gallery gallery = (Gallery)findViewById( R.id.somegallery );
- gallery.setAdapter( someAdapter );
- gallery.setOnItemClickListener( new OnItemClickListener() {
- @Override
- public void onItemClick( AdapterView<?> parent, View view,
- int position, long id ) {
- onClick( position );
- }
- });
- }
- else
- {
- setListAdapter( someAdapter );
- getListView().setOnScrollListener(this);
- }
- <view
- class="package$somegallery"
- android:id="@+id/somegallery"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent">
- </view>
- public static class somegallery extends Gallery
- {
- private Context mCtx;
- public somegallery(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- mCtx = context;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- ( (CurrentActivity)mCtx ).onScroll();
- return super.onFling(e1, e2, velocityX, velocityY);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement