horoko

Untitled

Aug 11th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.82 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.support.v4.view.PagerAdapter;
  8. import android.support.v4.view.ViewPager;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ImageButton;
  13. import android.widget.TextView;
  14.  
  15. import org.lucasr.twowayview.TwoWayView;
  16.  
  17. import java.util.ArrayList;
  18.  
  19.  
  20. public class MainActivity extends Activity {
  21.  
  22.  
  23.     public static ArrayList<Uri> uris = new ArrayList<>();
  24.     private static ContactPagerAdapter adapter;
  25.     public static Integer ids;
  26.     private static int focusedPage;
  27.     private static final int SMOOTH_SCROLL = 0;
  28.     private static final int SET_SELECT_SCROLL = 1;
  29.     public static Boolean resultState;
  30.  
  31.     private static TwoWayView lvTest;
  32.     private static TouchImageView photoImage;
  33.     private static ViewPager pager;
  34.     private TextView textView;
  35.     private ImageButton imageButton;
  36.     private ImageButton ibtnLeft;
  37.     private ImageButton ibtnRight;
  38.     private Intent cameraIntent;
  39.     private CustomAdapter customAdapter;
  40.     private LayoutInflater inflater;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_main);
  46.         resultState = false;
  47.         inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  48.  
  49.         focusedPage = 0;
  50.         textView = (TextView) findViewById(R.id.textView);
  51.         lvTest = (TwoWayView) findViewById(R.id.lvItems);
  52.         pager = (ViewPager) findViewById(R.id.pager);
  53.         imageButton = (ImageButton) findViewById(R.id.imageButton);
  54.         ibtnLeft = (ImageButton) findViewById(R.id.imageButtonLeft);
  55.         ibtnRight = (ImageButton) findViewById(R.id.imageButtonRight);
  56.         adapter = new ContactPagerAdapter();
  57.  
  58.         ibtnRight.setVisibility(View.INVISIBLE);
  59.         ibtnLeft.setVisibility(View.INVISIBLE);
  60.         imageButton.setVisibility(View.INVISIBLE);
  61.  
  62.  
  63.         pager.setAdapter(adapter);
  64.         cameraIntent = new Intent(this, CameraActivity.class);
  65.  
  66.  
  67.         pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  68.  
  69.             @Override
  70.             public void onPageSelected(int position) {
  71.                 focusedPage = position;
  72.                 if (position == uris.size())
  73.                     pager.setCurrentItem(0);
  74.  
  75.                 setScrollTop(focusedPage, SMOOTH_SCROLL);
  76.  
  77.                 if (position == uris.size() - 1) {
  78.                     ibtnRight.setVisibility(View.INVISIBLE);
  79.                 } else {
  80.                     ibtnRight.setVisibility(View.VISIBLE);
  81.                 }
  82.                 if (position == 0) {
  83.                     ibtnLeft.setVisibility(View.INVISIBLE);
  84.                 } else {
  85.                     ibtnLeft.setVisibility(View.VISIBLE);
  86.                 }
  87.             }
  88.  
  89.             @Override
  90.             public void onPageScrolled(int arg0, float arg1, int arg2) {
  91.             }
  92.  
  93.             @Override
  94.             public void onPageScrollStateChanged(int arg0) {
  95.  
  96.             }
  97.         });
  98.     }
  99.  
  100.  
  101.     public class ContactPagerAdapter extends PagerAdapter {
  102.  
  103.         public ContactPagerAdapter() {
  104.             super();
  105.         }
  106.  
  107.         @Override
  108.         public int getItemPosition(Object object) {
  109.             return POSITION_NONE;
  110.         }
  111.  
  112.         @Override
  113.         public int getCount() {
  114.             return uris.size();
  115.         }
  116.  
  117.         @Override
  118.         public Object instantiateItem(ViewGroup container, int position) {
  119.             if (uris.size() != 0) {
  120.                 photoImage = new TouchImageView(getApplicationContext());
  121.                 //  photoImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
  122.                 photoImage.setImageURI(uris.get(position));
  123.                 //   Picasso.with(context).load(uris.get(position)).into(photoImage);
  124.            /*final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
  125.             photoImage.setPadding(padding, padding, padding, padding);*/
  126.                 container.addView(photoImage, 0);
  127.                 photoImage.setOnClickListener(new View.OnClickListener() {
  128.                     @Override
  129.                     public void onClick(View v) {
  130.  
  131.                     }
  132.                 });
  133.             }
  134.             return photoImage;
  135.         }
  136.  
  137.         @Override
  138.         public void destroyItem(ViewGroup container, int position, Object view) {
  139.             container.removeView((View) view);
  140.         }
  141.  
  142.         @Override
  143.         public boolean isViewFromObject(View v, Object o) {
  144.             return v == o;
  145.         }
  146.     }
  147.  
  148.     public static void changePhoto(int id) {
  149.         pager.setCurrentItem(id);
  150.         ids = id;
  151.     }
  152.  
  153.     @Override
  154.     public void onBackPressed() {
  155.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  156.         alertDialog.setTitle("Выйти?");
  157.         alertDialog.setPositiveButton("Да", new DialogInterface.OnClickListener() {
  158.             public void onClick(DialogInterface dialog, int which) {
  159.                 System.exit(0);
  160.             }
  161.         });
  162.         alertDialog.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
  163.             public void onClick(DialogInterface dialog, int which) {
  164.                 dialog.cancel();
  165.             }
  166.         });
  167.         alertDialog.show();
  168.     }
  169.  
  170.    /* public static void resizeImageView(Uri uri) {
  171.         File picture = new File(String.valueOf(uri));
  172.         if (picture.exists()) {
  173.             BitmapFactory.Options options = new BitmapFactory.Options();
  174.             options.inSampleSize = 0;
  175.             //   Bitmap myBitmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), options);
  176.         }
  177.     }*/
  178.  
  179.     public void onLeft(View v) {
  180.         pager.setCurrentItem(0);
  181.     }
  182.  
  183.     public void onRight(View v) {
  184.         pager.setCurrentItem(uris.size() - 1);
  185.     }
  186.  
  187.     public void setScrollTop(int pos, int scroll) {
  188.         switch (scroll) {
  189.             case 0:
  190.                 lvTest.smoothScrollToPosition(pos);
  191.                 break;
  192.             case 1:
  193.                 lvTest.setSelection(pos);
  194.                 break;
  195.         }
  196.     }
  197.  
  198.     @Override
  199.     public void onResume() {
  200.         super.onResume();
  201.         if (resultState) {
  202.             refreshView();
  203.         }
  204.     }
  205.  
  206.     public void startCamera(View v) {
  207.         setVisibility();
  208.         startActivity(cameraIntent);
  209.         resultState = true;
  210.     }
  211.  
  212.     public void onClickPhotoNew(View v) {
  213.         ids = null;
  214.         setVisibility();
  215.         startActivity(cameraIntent);
  216.         resultState = true;
  217.  
  218.     }
  219.     private void setVisibility(){
  220.     textView.setVisibility(View.INVISIBLE);
  221.     imageButton.setVisibility(View.VISIBLE);
  222. }
  223.  
  224.  
  225.     public void refreshView() {
  226.  
  227.         customAdapter = new CustomAdapter(this, uris, inflater);
  228.  
  229.         lvTest.setAdapter(customAdapter);
  230.  
  231.         adapter.notifyDataSetChanged();
  232.         pager.setAdapter(adapter);
  233.         if (ids == null) {
  234.             pager.setCurrentItem(uris.size() - 1);
  235.         } else {
  236.             pager.setCurrentItem(ids);
  237.         }
  238.         setScrollTop(uris.size() - 1, SMOOTH_SCROLL);
  239.         if (ids != null) {
  240.             setScrollTop(ids, SET_SELECT_SCROLL);
  241.         } else lvTest.setSelection(uris.size() - 1);
  242.         ids = null;
  243.     }
  244.  
  245.  
  246.     public void deletePhoto(int id) {
  247.         uris.remove(id);
  248.  
  249.        // uris.removeAll(Collections.singleton(null));
  250.      /*   for (int i = id; i < uris.size()-1-id; i++) {
  251.             if (i<i+1){
  252.                 uris.set(i,uris.get(i+1));
  253.             }
  254.         }*/
  255.  
  256.         refreshView();
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment