horoko

Untitled

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