图像查看器不会在当前位置动态删除

时间:2014-04-10 07:19:11

标签: android

当我点击图像查看器的当前删除图像图标然后删除其他位置图像但我想删除图像查看器的特定位置图像删除。如果图像大小为3.那么我找到图像位置1 2 2.再次回到那时我找到位置2 0 0。

我的活动是:

    public class OnlySelectGridViewScreen extends BaseActivity {

    public static OnlySelectedFullImageAdapter adapter;
    public static ViewPager viewPager;
    private static final int TAKE_PHOTO_CODE = 0;
    private ArrayList<String> imagePaths;
    private static final String TAG = "OnlySelectGridViewScreen";
    private TextView tvHeader;
    private GridView gridView;
    private int columnWidth;
    String[] selectImages;
    private int imgPosition = -1;
    private String locName, deptName, cusFolderName, realImagesDirectory;
    private ImageView btnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image_screen);

        imagePaths = new ArrayList<String>();
        Intent i = getIntent();
        locName = i.getStringExtra("locName");
        deptName = i.getStringExtra("deptName");
        cusFolderName = i.getStringExtra("cusFolderName");
        realImagesDirectory = i.getStringExtra("realImagesDirectory");

        selectImages = i.getStringArrayExtra("selectImages");
        Toast.makeText(getBaseContext(), TAG+"\n SelectImages size :"+selectImages.length, Toast.LENGTH_LONG).show();
        List<String> l = Arrays.<String> asList(selectImages);

        // if List<String> isn't specific enough:
        imagePaths = new ArrayList<String>(l);
        tvHeader = (TextView) findViewById(R.id.tvHeader);
        tvHeader.setText("" + locName + " / " + deptName + " / "
                + cusFolderName);
        viewPager = (ViewPager) findViewById(R.id.pager);

        int position = i.getIntExtra("position", 0);

        adapter = new OnlySelectedFullImageAdapter(
                OnlySelectGridViewScreen.this, imagePaths, locName, deptName,
                cusFolderName, realImagesDirectory);
        if (imagePaths.size() > 0) {
            viewPager.clearFocus();
            adapter.notifyDataSetChanged();
        }
        viewPager.setAdapter(adapter);

        // displaying selected image first
        viewPager.setCurrentItem(position);
        btnBack = (ImageView) findViewById(R.id.backNow);
        btnDelete = (ImageView) findViewById(R.id.btnDelete);


        // delete Current image

        btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                File file = new File(imagePaths.get(imgPosition));
                boolean deleted = file.delete();
                if (deleted) {
                    Toast.makeText(OnlySelectGridViewScreen.this,
                            "Image delete successfully.", Toast.LENGTH_LONG)
                            .show();
                    imagePaths.remove(imgPosition);
                    // start of update part
                    if (imagePaths.size() == 0) {
                        Toast.makeText(
                                getBaseContext(),
                                "Not image found, so add image or back to previous page.",
                                Toast.LENGTH_LONG).show();
                    } else {

                        int position = 0;

                        adapter = new OnlySelectedFullImageAdapter(
                                OnlySelectGridViewScreen.this, imagePaths,
                                locName, deptName, cusFolderName,
                                realImagesDirectory);
                        adapter.notifyDataSetChanged();
                        viewPager.clearFocus();
                        viewPager.setAdapter(adapter);

                        // displaying selected image first
                        viewPager.setCurrentItem(position);
                        // end of update part
                    }
                } else {
                    Toast.makeText(OnlySelectGridViewScreen.this,
                            "Sorry, Image delete fails.", Toast.LENGTH_LONG)
                            .show();

                }

            }
        });

    }

    public class OnlySelectedFullImageAdapter extends PagerAdapter {

        private Activity _activity;
        private ArrayList<String> _imagePaths;
        private LayoutInflater inflater;
        private String locName, deptName, folderName, realImagesDirectory;

        // constructor
        public OnlySelectedFullImageAdapter(Activity activity,
                ArrayList<String> imagePaths, String _mLocationName,
                String _mDeptName, String _mFolderName,
                String realImagesDirectory) {
            this._activity = activity;
            this._imagePaths = imagePaths;
            this.locName = _mLocationName;
            this.deptName = _mDeptName;
            this.folderName = _mFolderName;
            this.realImagesDirectory = realImagesDirectory;
        }

        @Override
        public int getCount() {
            return this._imagePaths.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((RelativeLayout) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            TouchImageView imgDisplay;
            imgPosition = position;
            inflater = (LayoutInflater) _activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View viewLayout = inflater.inflate(
                    R.layout.layout_fullscreen_image, container, false);

            imgDisplay = (TouchImageView) viewLayout
                    .findViewById(R.id.imgDisplay);
            // Get the dimensions of the View
            // int targetW = imgDisplay.getWidth();
            // int targetH = imgDisplay.getHeight();

            int targetW = 100;
            int targetH = 100;

            // Get the dimensions of the bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(_imagePaths.get(position), bmOptions);
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;

            Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),
                    bmOptions);
            imgDisplay.setImageBitmap(bitmap);

            ((ViewPager) container).addView(viewLayout);

            return viewLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((RelativeLayout) object);

        }

    }



}

full_image_screen.xml文件如下:

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Header aligned to top -->

    <include
        android:id="@+id/header"
        layout="@layout/header" >
    </include>

    <!-- Footer aligned to bottom -->

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#0c95d4"
        android:gravity="center" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center" >



            <ImageView
                android:id="@+id/btnDelete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:background="@drawable/img_selector"
                android:clickable="true"
                android:src="@drawable/delete_icon_black" />




        </LinearLayout>
    </RelativeLayout>

    <!-- Content below header and above footer -->

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>

</RelativeLayout>

适配器layout_fullscreen_image.xml文件是beow:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.databizsoftware.cg.helpers.TouchImageView
        android:id="@+id/imgDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter" />

</RelativeLayout>

最后,header.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:background="#fff"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="5" >

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:paddingLeft="8dp"
            android:text="Fixed Header"
            android:textColor="#33b5e5"
            android:textSize="12sp" />

        <ImageView
            android:id="@+id/iHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/img_selector"
            android:clickable="true"
            android:onClick="goToHome"
            android:visibility="visible"
            android:src="@drawable/home_black" />

        <ImageView
            android:id="@+id/ilogout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:background="@drawable/img_selector"
            android:clickable="true"
            android:onClick="logoutAction"
            android:paddingRight="8dp"
            android:src="@drawable/logout_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <View
            android:layout_width="match_parent"
            android:layout_height="3dip"
            android:background="#33b5e5" />
    </LinearLayout>

</RelativeLayout>

为什么不删除图像查看器的当前位置。请帮帮我。

1 个答案:

答案 0 :(得分:0)

Implement pageChangeListener and you can achive this.

 viewPager.setOnPageChangeListener(new OnPageChangeListener(){

            public void onPageScrollStateChanged(int arg) {

            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            public void onPageSelected(int position) {
               imgPosition = position;
            }

        });