androidandroid-viewpagerfragmentstatepageradaptertouchimageview

Reset TouchImageView zoom in ViewPager


I have activity for fullscreen browsing of images (something like gallery). In this activity I have ViewPager with offscreen limit 6. I use TouchImageView for my images. The problem is, that when I first zoom image and swipe to another photo, I want to see not zoomed photo when I return to it. TouchImageView has resetZoom() function, but how can I get offscreen page view from the FragmentStatePageAdapter or from ViewPager? Here's code of my activity:

public class ImageSlidesActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {

final int PAGE_LIMIT = 6;
final int PAGE_MARGIN = 8;

long userID;
long deviceID;
String key;
String[] mAttachments;

ImageSlidesAdapter mAdaper;
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    setContentView(R.layout.activity_image_slides);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    //actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33ffffff")));

    Intent intent = getIntent();
    mAttachments = intent.getStringArrayExtra("attachments");
    int position = intent.getIntExtra("position", 0);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("authData", 0);
    key = pref.getString("key", null);
    userID = pref.getLong("userID", 0);
    deviceID = pref.getLong("deviceID", 0);

    setPageIndicator(position);
    mAdaper = new ImageSlidesAdapter(getSupportFragmentManager(), mAttachments);

    final View container = findViewById(R.id.container);
    container.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActionBar().hide();
            container.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    });
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdaper);
    mPager.setOnPageChangeListener(this);
    mPager.setOffscreenPageLimit(PAGE_LIMIT);
    mPager.setCurrentItem(position-1);

    /* cause bug with the white strip
    final float density = getResources().getDisplayMetrics().density;
    mPager.setPageMargin((int) (PAGE_MARGIN * density + 0.5f));
    mPager.setPageMarginDrawable(android.R.color.black);
    */
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.image_slides, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();
    }

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    setPageIndicator(position+1);
}

@Override
public void onPageScrollStateChanged(int state) {

}

private void setPageIndicator(int page) {
    setTitle(Integer.toString(page) + "/" + Integer.toString(mAttachments.length));
}

/**
 * A fragment containing zoomable image.
 */
public static class ShowImageFragment extends Fragment {

    String mAlias;
    long mUserId;
    long mDeviceId;
    String mKey;
    TouchImageView mImage;

    static ShowImageFragment newInstance(String alias, long userId, long deviceId,
    String key) {
        ShowImageFragment f = new ShowImageFragment();

        Bundle args = new Bundle();
        args.putString("alias", alias);
        args.putLong("userID", userId);
        args.putLong("deviceID", deviceId);
        args.putString("key", key);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mAlias = bundle.getString("alias");
        mUserId = bundle.getLong("userID");
        mDeviceId = bundle.getLong("deviceID");
        mKey = bundle.getString("key");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_image_slides, container, false);
        mImage = (TouchImageView) rootView.findViewById(R.id.imageView);
        final ProgressBar prBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... voids) {
                try {
                    String[] links = API.getImageLinkByAlias(mAlias, mUserId,
                            mDeviceId, mKey);
                    return ImageLoader.getInstance().loadImageSync(links[1]);
                } catch (Exception e) {
                    // TODO: handle exceptions
                    e.printStackTrace();
                    cancel(true);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                prBar.setVisibility(View.GONE);
                if (bitmap == null) { return; }
                mImage.setImageBitmap(bitmap);
                mImage.setVisibility(View.VISIBLE);
                mImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (getActivity().getActionBar().isShowing()) {
                            getActivity().getActionBar().hide();
                        } else {
                            getActivity().getActionBar().show();
                        }
                    }
                });
            }
        }.execute();
        return rootView;
    }
}

public class ImageSlidesAdapter extends FragmentStatePagerAdapter {

    private String[] mAliases;
    public ImageSlidesAdapter(android.support.v4.app.FragmentManager fm, String[] aliases) {
        super(fm);
        mAliases = aliases;
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return ShowImageFragment.newInstance(mAliases[position], userID, deviceID, key);
    }

    @Override
    public int getCount() {
        return mAliases.length;
    }
}

}


Solution

  • I overrided onPageSelected method and I get views by calling getChildAt().

    @Override
    public void onPageSelected(int position) {
        setPageIndicator(position+1);
        if (position > 0) {
            View view = mPager.getChildAt(position -1);
            if (view != null) {
                TouchImageView img = (TouchImageView) view.findViewById(R.id.image);
                img.resetZoom();
            }
        }
        if (position < mPager.getChildCount() - 1) {
            View view = mPager.getChildAt(position + 1);
            if (view != null) {
                TouchImageView img = (TouchImageView) view.findViewById(R.id.image);
                img.resetZoom();
            }
        }
    }