Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. import com.example.android.apis.R;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.os.Bundle;
  7. import android.view.ContextMenu;
  8. import android.view.GestureDetector;
  9. import android.view.MenuItem;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.view.ContextMenu.ContextMenuInfo;
  14. import android.view.GestureDetector.SimpleOnGestureListener;
  15. import android.view.View.OnTouchListener;
  16. import android.widget.AdapterView;
  17. import android.widget.BaseAdapter;
  18. import android.widget.Gallery;
  19. import android.widget.ImageView;
  20. import android.widget.Toast;
  21. import android.widget.AdapterView.AdapterContextMenuInfo;
  22. import android.widget.AdapterView.OnItemClickListener;
  23.  
  24. public class Gallery1 extends Activity {
  25.  
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.gallery_1);
  30.  
  31. // Reference the Gallery view
  32. final Gallery g = (Gallery) findViewById(R.id.gallery);
  33.  
  34. // Set the adapter to our custom adapter (below)
  35. g.setAdapter(new ImageAdapter(this));
  36.  
  37. // Set a item click listener, and just Toast the clicked position
  38. g.setOnItemClickListener(new OnItemClickListener() {
  39. public void onItemClick(AdapterView parent, View v, int position, long id) {
  40. Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
  41. }
  42. });
  43.  
  44. // Gesture detection
  45. final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(g));
  46. OnTouchListener gestureListener = new OnTouchListener() {
  47. @Override
  48. public boolean onTouch(View v, MotionEvent event) {
  49. boolean retVal = gestureDetector.onTouchEvent(event);
  50. int action = event.getAction();
  51. if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
  52. retVal = true;
  53. onUp();
  54. }
  55. return retVal;
  56. }
  57.  
  58. public void onUp() {
  59. // Here I am merely copying the Gallery's onUp() method.
  60. for (int i = g.getChildCount() - 1; i >= 0; i--) {
  61. g.getChildAt(i).setPressed(false);
  62. }
  63. g.setPressed(false);
  64. }
  65. };
  66. g.setOnTouchListener(gestureListener);
  67.  
  68. // We also want to show context menu for longpressed items in the gallery
  69. registerForContextMenu(g);
  70. }
  71.  
  72. @Override
  73. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  74. menu.add(R.string.gallery_2_text);
  75. }
  76.  
  77. @Override
  78. public boolean onContextItemSelected(MenuItem item) {
  79. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  80. Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
  81. return true;
  82. }
  83.  
  84. public class ImageAdapter extends BaseAdapter {
  85. int mGalleryItemBackground;
  86.  
  87. public ImageAdapter(Context c) {
  88. mContext = c;
  89. // See res/values/attrs.xml for the <declare-styleable> that defines
  90. // Gallery1.
  91. TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
  92. mGalleryItemBackground = a.getResourceId(
  93. R.styleable.Gallery1_android_galleryItemBackground, 0);
  94. a.recycle();
  95. }
  96.  
  97. public int getCount() {
  98. return mImageIds.length;
  99. }
  100.  
  101. public Object getItem(int position) {
  102. return position;
  103. }
  104.  
  105. public long getItemId(int position) {
  106. return position;
  107. }
  108.  
  109. public View getView(int position, View convertView, ViewGroup parent) {
  110. ImageView i = new ImageView(mContext);
  111.  
  112. i.setImageResource(mImageIds[position]);
  113. i.setScaleType(ImageView.ScaleType.FIT_XY);
  114. i.setLayoutParams(new Gallery.LayoutParams(136, 88));
  115.  
  116. // The preferred Gallery item background
  117. i.setBackgroundResource(mGalleryItemBackground);
  118.  
  119. return i;
  120. }
  121.  
  122. private Context mContext;
  123.  
  124. private Integer[] mImageIds = {
  125. R.drawable.gallery_photo_1,
  126. R.drawable.gallery_photo_2,
  127. R.drawable.gallery_photo_3,
  128. R.drawable.gallery_photo_4,
  129. R.drawable.gallery_photo_5,
  130. R.drawable.gallery_photo_6,
  131. R.drawable.gallery_photo_7,
  132. R.drawable.gallery_photo_8
  133. };
  134. }
  135.  
  136. public class MyGestureDetector extends SimpleOnGestureListener {
  137.  
  138. private Gallery gallery;
  139.  
  140. public MyGestureDetector(Gallery gallery) {
  141. this.gallery = gallery;
  142. }
  143.  
  144. @Override
  145. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  146. float velocityY) {
  147. return gallery.onFling(e1, e2, velocityX, velocityY);
  148. }
  149. }
  150.  
  151. }
  152.  
  153. setContentView(R.layout.somelayout);
  154. orientation = getResources().getConfiguration().orientation;
  155.  
  156. if ( orientation == Configuration.ORIENTATION_LANDSCAPE )
  157. {
  158.  
  159. Gallery gallery = (Gallery)findViewById( R.id.somegallery );
  160.  
  161. gallery.setAdapter( someAdapter );
  162.  
  163. gallery.setOnItemClickListener( new OnItemClickListener() {
  164. @Override
  165. public void onItemClick( AdapterView<?> parent, View view,
  166. int position, long id ) {
  167.  
  168. onClick( position );
  169. }
  170. });
  171. }
  172. else
  173. {
  174. setListAdapter( someAdapter );
  175.  
  176. getListView().setOnScrollListener(this);
  177. }
  178.  
  179. <view
  180. class="package$somegallery"
  181. android:id="@+id/somegallery"
  182. android:layout_height="fill_parent"
  183. android:layout_width="fill_parent">
  184. </view>
  185.  
  186. public static class somegallery extends Gallery
  187. {
  188. private Context mCtx;
  189.  
  190. public somegallery(Context context, AttributeSet attrs)
  191. {
  192. super(context, attrs);
  193. mCtx = context;
  194. }
  195.  
  196. @Override
  197. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  198. float velocityY) {
  199.  
  200. ( (CurrentActivity)mCtx ).onScroll();
  201.  
  202. return super.onFling(e1, e2, velocityX, velocityY);
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement