Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. `<GridView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/gridview"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:columnWidth="90dp"
  6. android:numColumns="auto_fit"
  7. android:verticalSpacing="10dp"
  8. android:horizontalSpacing="10dp"
  9. android:stretchMode="columnWidth"
  10. android:gravity="center" />`
  11.  
  12. public class MyAdapter extends BaseAdapter {
  13. private Context mContext;
  14.  
  15. public MyAdapter(Context c) {
  16. mContext = c;
  17. }
  18.  
  19. public int getCount() {
  20. return mThumbIds.length;
  21. }
  22.  
  23. public Object getItem(int position) {
  24. return null;
  25. }
  26.  
  27. public long getItemId(int position) {
  28. return 0;
  29. }
  30.  
  31. // create a new ImageView for each item referenced by the Adapter
  32. public View getView(int position, View convertView, ViewGroup parent) {
  33. ImageView imageView;
  34. if (convertView == null) {
  35. // if it's not recycled, initialize some attributes
  36. imageView = new ImageView(mContext);
  37. imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
  38. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  39. imageView.setPadding(8, 8, 8, 8);
  40. } else {
  41. imageView = (ImageView) convertView;
  42. }
  43.  
  44. imageView.setImageResource(mThumbIds[position]);
  45. return imageView;
  46. }
  47.  
  48. // references to our images
  49. private Integer[] mThumbIds = {
  50. R.drawable.sample_2, R.drawable.sample_3,
  51. R.drawable.sample_4, R.drawable.sample_5,
  52. R.drawable.sample_6, R.drawable.sample_7,
  53. R.drawable.sample_0, R.drawable.sample_1,
  54. R.drawable.sample_2, R.drawable.sample_3,
  55. R.drawable.sample_4, R.drawable.sample_5,
  56. R.drawable.sample_6, R.drawable.sample_7,
  57. R.drawable.sample_0, R.drawable.sample_1,
  58. R.drawable.sample_2, R.drawable.sample_3,
  59. R.drawable.sample_4, R.drawable.sample_5,
  60. R.drawable.sample_6, R.drawable.sample_7
  61. };
  62. }
  63.  
  64. public class MainActivity extends AppCompatActivity {
  65.  
  66. private SectionsPagerAdapter mSectionsPagerAdapter;
  67.  
  68. private ViewPager mViewPager;
  69.  
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.activity_main);
  74.  
  75. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  76. setSupportActionBar(toolbar);
  77.  
  78. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  79.  
  80. mViewPager = (ViewPager) findViewById(R.id.container);
  81. mViewPager.setAdapter(mSectionsPagerAdapter);
  82.  
  83.  
  84. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  85. fab.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View view) {
  88. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  89. .setAction("Action", null).show();
  90. }
  91. });
  92.  
  93. //------------ the code that i added//////////////////////////////////
  94. GridView gridview = (GridView) findViewById(R.id.gridview);
  95. gridview.setAdapter(new MyAdapter(this));
  96.  
  97. gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  98. public void onItemClick(AdapterView<?> parent, View v,
  99. int position, long id) {
  100. Toast.makeText(getApplicationContext(), "" + position,
  101. Toast.LENGTH_SHORT).show();
  102. }
  103. });
  104. //end of the code that i added///////////////////////////////////////
  105. }
  106.  
  107.  
  108. @Override
  109. public boolean onCreateOptionsMenu(Menu menu) {
  110. // Inflate the menu; this adds items to the action bar if it is present.
  111. getMenuInflater().inflate(R.menu.menu_main, menu);
  112. return true;
  113. }
  114.  
  115. @Override
  116. public boolean onOptionsItemSelected(MenuItem item) {
  117. // Handle action bar item clicks here. The action bar will
  118. // automatically handle clicks on the Home/Up button, so long
  119. // as you specify a parent activity in AndroidManifest.xml.
  120. int id = item.getItemId();
  121.  
  122. //noinspection SimplifiableIfStatement
  123. if (id == R.id.action_settings) {
  124. return true;
  125. }
  126.  
  127. return super.onOptionsItemSelected(item);
  128. }
  129.  
  130. /**
  131. * A placeholder fragment containing a simple view.
  132. */
  133. public static class PlaceholderFragment extends Fragment {
  134. /**
  135. * The fragment argument representing the section number for this
  136. * fragment.
  137. */
  138. private static final String ARG_SECTION_NUMBER = "section_number";
  139.  
  140. public PlaceholderFragment() {
  141. }
  142.  
  143. /**
  144. * Returns a new instance of this fragment for the given section
  145. * number.
  146. */
  147. public static PlaceholderFragment newInstance(int sectionNumber) {
  148. PlaceholderFragment fragment = new PlaceholderFragment();
  149. Bundle args = new Bundle();
  150. args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  151. fragment.setArguments(args);
  152. return fragment;
  153. }
  154.  
  155. @Override
  156. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  157. Bundle savedInstanceState) {
  158. View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  159. TextView textView = (TextView) rootView.findViewById(R.id.section_label);
  160. textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
  161. return rootView;
  162. }
  163. }
  164.  
  165. /**
  166. * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  167. * one of the sections/tabs/pages.
  168. */
  169. public class SectionsPagerAdapter extends FragmentPagerAdapter {
  170.  
  171. public SectionsPagerAdapter(FragmentManager fm) {
  172. super(fm);
  173. }
  174.  
  175. @Override
  176. public Fragment getItem(int position) {
  177. // getItem is called to instantiate the fragment for the given page.
  178. // Return a PlaceholderFragment (defined as a static inner class below).
  179. return PlaceholderFragment.newInstance(position + 1);
  180. }
  181.  
  182. @Override
  183. public int getCount() {
  184. // Show 3 total pages.
  185. return 3;
  186. }
  187.  
  188. @Override
  189. public CharSequence getPageTitle(int position) {
  190. switch (position) {
  191. case 0:
  192. return "SECTION 1";
  193. case 1:
  194. return "SECTION 2";
  195. case 2:
  196. return "SECTION 3";
  197. }
  198. return null;
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement