1

I have an FragmentActivity holding a entry button and a viewpager.
When the entry button clicked, it will set an image url to a new FragmentPagerAdapter and set the adapter to viewpager.
And the FragmentPagerAdpater is holding fragment that shows an image.
Then press back button, and click that entry button again.
It will set a new image url to a new FragmentPagerAdapter and set the adapter to viewpager.

My problem is the fragment keep showing the first image, but not show other image when entry button is clicked. It seems some stuffs can't update.

What would be the problem?

TestGallery.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.season.test.TestGalleryFragment;

import java.util.ArrayList;


public class TestGallery extends FragmentActivity {
    private ArrayList<Integer> imgURLs;
    private GalleryPagerAdapter mAdapter;
    private Button btnEntry;
    private ViewPager viewPager;
    private int curr = 0;
    private int[] resourceIDs = {R.drawable.abc_btn_check_material, R.drawable.abc_ic_go_search_api_mtrl_alpha, R.drawable.ic_launcher};

    @Override
    public void onBackPressed() {
        if (viewPager.getVisibility() == View.VISIBLE) {
            btnEntry.setVisibility(View.VISIBLE);
            viewPager.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_gallery);
        btnEntry = (Button) findViewById(R.id.btnEntry);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        btnEntry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (curr >= resourceIDs.length) {
                    curr = 0;
                }
               if (imgURLs == null) {
                imgURLs = new ArrayList<>();
            }
            imgURLs.clear();
            imgURLs.add(resourceIDs[curr]);

            if (mAdapter == null) {
                mAdapter = new GalleryPagerAdapter(getSupportFragmentManager(), imgURLs);
                viewPager.setAdapter(mAdapter);
            }
            mAdapter.notifyDataSetChanged();

                btnEntry.setVisibility(View.GONE);
                viewPager.setVisibility(View.VISIBLE);

                curr++;
                btnEntry.setText("Click For Next Image: " + curr % resourceIDs.length);
                Toast.makeText(TestGallery.this,"Press Back Button",Toast.LENGTH_SHORT).show();
            }
        });

    }

    class GalleryPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Integer> imgURLs;


        GalleryPagerAdapter(FragmentManager fm, ArrayList<Integer> imgURLs) {
            super(fm);
            this.imgURLs = imgURLs;
        }

        @Override
        public Fragment getItem(int position) {
            return TestGalleryFragment.newInstance(imgURLs.get(position));
        }

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


    }

}

activity_test_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layoutDetails">

        <Button
            android:id="@+id/btnEntry"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Click to show next"
            android:textSize="30sp"
           />

        <android.support.v4.view.ViewPager
            android:background="#0000"
            android:id="@+id/viewPager"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>


</FrameLayout>

TestGalleryFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.season.test.R;

public class TestGalleryFragment extends Fragment {

    public static String KEY_IMG_URL = "imgURL";

    private Integer imgURL = R.drawable.ic_launcher;

    public static TestGalleryFragment newInstance(Integer imgURL) {
        Log.i("url", "frag url: " + imgURL);
        TestGalleryFragment fragment = new TestGalleryFragment();
        Bundle args = new Bundle();
        args.putInt(KEY_IMG_URL, imgURL);
        fragment.setArguments(args);
        return fragment;
    }

    public TestGalleryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            imgURL = getArguments().getInt(KEY_IMG_URL);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        ImageView imgPhoto = (ImageView) root.findViewById(R.id.imgPhoto);
        imgPhoto.setImageResource(imgURL);
        return root;
    }


}

fragment_test_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000">

    <ImageView
        android:id="@+id/imgPhoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />


</FrameLayout>
Season
  • 1,178
  • 2
  • 22
  • 42
  • You should make your `imgURLs` and `adapter` Globally and add new item when new entry in `imgURLs` when button pressed. – M D Feb 17 '15 at 08:59
  • @M D I've modified position of adapter and imgURLs and way to add item into imgURLs. But same problem still occurs – Season Feb 17 '15 at 09:39
  • I had a similiar problem which was caused by the visibility. Try to set "viewPager.setVisibility(View.VISIBLE);" BEFORE " viewPager.setAdapter(mAdapter);" – Cattivo May 04 '15 at 10:03

1 Answers1

1

I found out why this problem happen.
It is because fragment does not re
fresh/re-created after notifyDataSetChanged called. In order to make new fragments every time data changed. Better modify the FragmentPagerAdapter as suggested answer from
https://stackoverflow.com/a/26944013/2080233

Community
  • 1
  • 1
Season
  • 1,178
  • 2
  • 22
  • 42