mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add ProgressBar to MediaDetailPagerFragment for Image Loading Indicator (#5736)
* Add progress bar to fragment_media_detail_pager.xml * Add progress bar to MediaDetailPagerFragment.java * Add javadoc & comments * Fix tests --------- Co-authored-by: Giannis Karyotakis <110292528+karyotakisg@users.noreply.github.com>
This commit is contained in:
parent
3dc7180784
commit
48bd3c07b8
2 changed files with 46 additions and 23 deletions
|
|
@ -2,6 +2,9 @@ package fr.free.nrw.commons.media;
|
|||
|
||||
import static fr.free.nrw.commons.Utils.handleWebUrl;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.widget.ProgressBar;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
|
|
@ -76,6 +79,11 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
private boolean isFromFeaturedRootFragment;
|
||||
private int position;
|
||||
|
||||
/**
|
||||
* ProgressBar used to indicate the loading status of media items.
|
||||
*/
|
||||
private ProgressBar imageProgressBar;
|
||||
|
||||
private ArrayList<Integer> removedItems=new ArrayList<Integer>();
|
||||
|
||||
public void clearRemoved(){
|
||||
|
|
@ -116,7 +124,8 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
Bundle savedInstanceState) {
|
||||
binding = FragmentMediaDetailPagerBinding.inflate(inflater, container, false);
|
||||
binding.mediaDetailsPager.addOnPageChangeListener(this);
|
||||
|
||||
// Initialize the ProgressBar by finding it in the layout
|
||||
imageProgressBar = binding.getRoot().findViewById(R.id.itemProgressBar);
|
||||
adapter = new MediaDetailAdapter(getChildFragmentManager());
|
||||
|
||||
// ActionBar is now supported in both activities - if this crashes something is quite wrong
|
||||
|
|
@ -497,19 +506,27 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
* @param position current item that to be shown
|
||||
*/
|
||||
private void setViewPagerCurrentItem(int position) {
|
||||
final Boolean[] currentItemNotShown = {true};
|
||||
Runnable runnable = new Runnable() {
|
||||
|
||||
final Handler handler = new Handler(Looper.getMainLooper());
|
||||
final Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while(currentItemNotShown[0]){
|
||||
// Show the ProgressBar while waiting for the item to load
|
||||
imageProgressBar.setVisibility(View.VISIBLE);
|
||||
// Check if the adapter has enough items loaded
|
||||
if(adapter.getCount() > position){
|
||||
// Set the current item in the ViewPager
|
||||
binding.mediaDetailsPager.setCurrentItem(position, false);
|
||||
currentItemNotShown[0] = false;
|
||||
}
|
||||
// Hide the ProgressBar once the item is loaded
|
||||
imageProgressBar.setVisibility(View.GONE);
|
||||
} else {
|
||||
// If the item is not ready yet, post the Runnable again
|
||||
handler.post(this);
|
||||
}
|
||||
}
|
||||
};
|
||||
new Thread(runnable).start();
|
||||
// Start the Runnable
|
||||
handler.post(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,12 +3,18 @@
|
|||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
>
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<ProgressBar
|
||||
android:id="@+id/itemProgressBar"
|
||||
style="?android:attr/progressBarStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:progressBackgroundTint="@android:color/white"/>
|
||||
|
||||
<androidx.viewpager.widget.ViewPager
|
||||
android:id="@+id/mediaDetailsPager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fadingEdge="none"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
android:fadingEdge="none" />
|
||||
</LinearLayout>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue