Commit 8306f07b by 段启岩

图片浏览器完成

parent 21d72c4f
...@@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity; ...@@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
...@@ -20,11 +21,13 @@ import cn.yunliyunwai.beyondclouds.databinding.FragmentPhotoBinding; ...@@ -20,11 +21,13 @@ import cn.yunliyunwai.beyondclouds.databinding.FragmentPhotoBinding;
public class PhotoViewerActivity extends AppCompatActivity { public class PhotoViewerActivity extends AppCompatActivity {
public static final String ARGS_PHOTO_LINKS = "PHOTO_LINKS"; public static final String ARG_PHOTO_LINKS = "PHOTO_LINKS";
public static final String ARG_CURRENT_INDEX = "CURRENT_INDEX";
ActivityPhotoviewerBinding binding; ActivityPhotoviewerBinding binding;
private String[] mPhotos; private String[] mPhotos;
private int mCurrentIndex;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
...@@ -33,10 +36,30 @@ public class PhotoViewerActivity extends AppCompatActivity { ...@@ -33,10 +36,30 @@ public class PhotoViewerActivity extends AppCompatActivity {
setContentView(binding.getRoot()); setContentView(binding.getRoot());
Intent intent = getIntent(); Intent intent = getIntent();
mPhotos = intent.getStringArrayExtra(ARGS_PHOTO_LINKS); mPhotos = intent.getStringArrayExtra(ARG_PHOTO_LINKS);
mCurrentIndex = intent.getIntExtra(ARG_CURRENT_INDEX, 0);
binding.txtCurrentIndex.setText((mCurrentIndex + 1) + "");
binding.txtImgCount.setText(mPhotos.length + "");
PhotosPagerAdapter adapter = new PhotosPagerAdapter(getSupportFragmentManager(), mPhotos); PhotosPagerAdapter adapter = new PhotosPagerAdapter(getSupportFragmentManager(), mPhotos);
binding.viewPager.setAdapter(adapter); binding.viewPager.setAdapter(adapter);
binding.viewPager.setCurrentItem(mCurrentIndex);
binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
binding.txtCurrentIndex.setText((position + 1) + "");
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
} }
public static class PhotosPagerAdapter extends FragmentPagerAdapter { public static class PhotosPagerAdapter extends FragmentPagerAdapter {
......
...@@ -207,9 +207,10 @@ public class NavHandlers { ...@@ -207,9 +207,10 @@ public class NavHandlers {
view.getContext().startActivity(intent); view.getContext().startActivity(intent);
} }
public static void toPhotoViewer(View view, String[] photoLinks) { public static void toPhotoViewer(View view, String[] photoLinks, int currentIndex) {
Intent intent = new Intent(view.getContext(), PhotoViewerActivity.class); Intent intent = new Intent(view.getContext(), PhotoViewerActivity.class);
intent.putExtra(PhotoViewerActivity.ARGS_PHOTO_LINKS, photoLinks); intent.putExtra(PhotoViewerActivity.ARG_PHOTO_LINKS, photoLinks);
intent.putExtra(PhotoViewerActivity.ARG_CURRENT_INDEX, currentIndex);
view.getContext().startActivity(intent); view.getContext().startActivity(intent);
} }
} }
...@@ -128,10 +128,11 @@ public class DynamicGridLayout extends ViewGroup { ...@@ -128,10 +128,11 @@ public class DynamicGridLayout extends ViewGroup {
int left = computePosition(currentCol, imageSize) + getPaddingLeft(); int left = computePosition(currentCol, imageSize) + getPaddingLeft();
int top = computePosition(currentRow, imageSize) + getPaddingTop(); int top = computePosition(currentRow, imageSize) + getPaddingTop();
imageView.layout(left, top, left + imageSize, top + imageSize); imageView.layout(left, top, left + imageSize, top + imageSize);
final int currentIndex = i;
imageView.setOnClickListener(new OnClickListener() { imageView.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
NavHandlers.toPhotoViewer(v, mImgUrls.toArray(new String[0])); NavHandlers.toPhotoViewer(v, mImgUrls.toArray(new String[0]), currentIndex);
} }
}); });
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout android:background="#000000"
android:background="#000000" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.viewpager.widget.ViewPager <androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager" android:id="@+id/view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/img_indicator"
app:layout_constraintStart_toStartOf="@id/view_pager"
app:layout_constraintEnd_toEndOf="@id/view_pager"
app:layout_constraintBottom_toBottomOf="@id/view_pager"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_current_index"
android:text="1"
android:textSize="24dp"
android:typeface="serif"
android:textColor="@color/colorWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="/"
android:textColor="#f2f2f2"
android:typeface="serif"
android:textSize="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt_img_count"
android:text="9"
android:typeface="serif"
android:textColor="#f2f2f2"
android:textSize="14dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment