Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
ViewPager in screenshot dialog to add ability to swipe between images. (
Browse files Browse the repository at this point in the history
  • Loading branch information
drivfe authored and Garret Yoder committed Sep 21, 2016
1 parent 0202588 commit d166db3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package subreddit.android.appstore.screens.details;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -60,6 +59,7 @@ public class AppDetailsFragment extends BasePresenterFragment<AppDetailsContract

private List<String> contactItems = new ArrayList<>();
private List<String> downloadItems = new ArrayList<>();
private List<String> screenshotUrls = new ArrayList<>();
private ScreenshotsAdapter screenshotsAdapter;

ArrayList<Download> downloads = new ArrayList<>();
Expand Down Expand Up @@ -116,7 +116,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
toolbar.setNavigationOnClickListener(this);
toolbar.inflateMenu(R.menu.appdetails_fragment);
toolbar.setOnMenuItemClickListener(this);
screenshotsAdapter = new ScreenshotsAdapter(getContext());
screenshotsAdapter = new ScreenshotsAdapter(getContext(), 3);
screenshotPager.setAdapter(screenshotsAdapter);
screenshotPager.setOffscreenPageLimit(3);
screenshotsAdapter.setScreenshotClickedListener(this);
Expand Down Expand Up @@ -248,8 +248,9 @@ private void openInChrome(String url) {
@Override
public void displayScreenshots(@Nullable ScrapeResult scrapeResult) {
if (scrapeResult != null) {
this.screenshotUrls = new ArrayList<>(scrapeResult.getScreenshotUrls());
screenshotPager.setVisibility(View.VISIBLE);
screenshotsAdapter.update(new ArrayList<>(scrapeResult.getScreenshotUrls()));
screenshotsAdapter.update(screenshotUrls);
} else screenshotPager.setVisibility(View.GONE);
}

Expand All @@ -266,6 +267,6 @@ public void displayIcon(@Nullable AppInfo appInfo) {

@Override
public void onScreenshotClicked(String url) {
new ScreenshotDialog(getContext(), url).show();
new ScreenshotDialog(getContext(), screenshotUrls, screenshotUrls.indexOf(url)).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import subreddit.android.appstore.R;

public class ScreenshotDialog extends Dialog implements View.OnClickListener {
@BindView(R.id.dialog_image) ImageView image;
@BindView(R.id.dialog_toolbar) Toolbar toolbar;
private String url;
private Context context;

public ScreenshotDialog(Context context, String url) {
super(context,android.R.style.Theme_Black_NoTitleBar);
this.url=url;
this.context=context;
private final List<String> urls;
private final int currentImage;
@BindView(R.id.screenshot_dialog_pager) ViewPager viewPager;
@BindView(R.id.screenshot_dialog_toolbar) Toolbar toolbar;
@BindView(R.id.screenshot_dialog_page_indicator) TextView pageIndicator;

public ScreenshotDialog(Context context, List<String> urls, int currentImage) {
super(context, android.R.style.Theme_Black_NoTitleBar);
this.currentImage = currentImage;
this.urls = urls;
}

@Override
Expand All @@ -32,7 +34,32 @@ protected void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_48px);
toolbar.setNavigationOnClickListener(this);
Glide.with(context).load(url).into(image);

ScreenshotsAdapter screenshotsAdapter = new ScreenshotsAdapter(getContext(), 1);
screenshotsAdapter.update(urls);
viewPager.setAdapter(screenshotsAdapter);
viewPager.setCurrentItem(currentImage, false);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
updatePageIndicator(position);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
updatePageIndicator(currentImage);
}

private void updatePageIndicator(int position) {
pageIndicator.setText(String.format("%s/%s", position + 1, urls.size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import subreddit.android.appstore.util.ui.glide.PlaceHolderRequestListener;

public class ScreenshotsAdapter extends PagerAdapter {
private final int imagesPerPage;
private Context context;
private LayoutInflater layoutInflater;
List<String> urls = new ArrayList<>();
ScreenshotClickedListener l;

public ScreenshotsAdapter(Context context) {
public ScreenshotsAdapter(Context context, int imagesPerPage) {
this.imagesPerPage = imagesPerPage;
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Expand Down Expand Up @@ -70,7 +72,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {

@Override
public float getPageWidth(int position) {
return super.getPageWidth(position) / 3;
return super.getPageWidth(position) / imagesPerPage;
}

public void setScreenshotClickedListener(ScreenshotClickedListener l) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/adapter_gallery_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:scaleType="fitCenter"
android:visibility="invisible"/>

<ProgressBar
Expand Down
35 changes: 24 additions & 11 deletions app/src/main/res/layout/dialog_screenshot.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout 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">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/dialog_image"/>
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/screenshot_dialog_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialog_toolbar"
android:background="@android:color/transparent"/>
android:background="@color/colorPrimary">

<TextView
android:id="@+id/screenshot_dialog_page_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:textColor="@android:color/white"
tools:text="1/4" />

</android.support.v7.widget.Toolbar>

<android.support.v4.view.ViewPager
android:id="@+id/screenshot_dialog_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>
</LinearLayout>

0 comments on commit d166db3

Please sign in to comment.