Search code examples
androidandroid-viewpagerpull-to-refresh

The ViewPager doesn't scroll at al after refreshing the data


I have an activity which has a coordinator layout with viewpager and CircularPager Indicator. I am trying to use MaterialRefreshLayout Library to do pull to refresh

The library can be found at Pull to refresh library

However, after I use pull to refresh my data, the viewpager just doesn't update the rest of the fragment and hence it dosn't scroll. Once I do background foreground it scrolls alright. Below is the Layout and activity

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/main_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="myPackage.HomeActivity">

  <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

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

  </android.support.design.widget.AppBarLayout>

   <com.cjj.MaterialRefreshLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/refresh"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:overlay="true"
     app:progress_show_arrow="true"
     app:wave_show="false">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="65dp"
        android:orientation="vertical">

         <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/viewpager_height_none"
            android:layout_weight="1"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <mypackage.ui.CirclePageIndicator
            android:id="@+id/indicator"
            style="@style/PageIndicator"
            android:clickable="false"
            app:fillColor="@color/default_circle_indicator_stroke_color"
            app:pageColor="@color/circlepageindicator_fill_color"
            app:radius="@dimen/circlepageindicator_radius" />

       </LinearLayout>
     </com.cjj.MaterialRefreshLayout>

   <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

 </android.support.design.widget.CoordinatorLayout>

And Below is the Activity

public class HomeActivity extends AppCompatActivity {


    private SectionPagerAdapter mSectionPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;
    private CirclePageIndicator mPagerCountIndicator;
    private MaterialRefreshLayout materialRefreshLayout;
    private Integer mTaskCount;

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

        setContentView(R.layout.activity_home);
        Bundle extras = getIntent().getExtras();
        if(extras != null) {
            mTaskCount= extras.getInt("task_Count");
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       updateandShowViewPager();

        materialRefreshLayout = (MaterialRefreshLayout) findViewById(R.id.refresh);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                refresh();
            }
        });
        materialRefreshLayout.finishRefresh();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
    private void startMyAsyncTask(AsyncTask asyncTask){
        //execute my AsyncTask and call Update and Show ViewPager
    }

   private void updateandShowViewPager(){
       AsyncTaskCallBack asyncTaskCallBack = new AsyncTaskCallBack() {
           @Override
           public void onSuccess() {

                   displayViewPager();
               }
           }
       };

       for(int taskCount  = 1; taskCount <= mTaskCount; taskCount ++){
           myAsyncTask = new MyAsyncTask();
           startMyAsyncTask(myAsyncTask );
       }
   }

    private void displayViewPager(){
        mSectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionPagerAdapter);

        mTaskCountIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        mTaskCountIndicator .setViewPager(mViewPager);
    }

    private void refresh() {
        MyAsyncTask myAsyncTask = null;
        MyAsyncTaskCallBack myAsyncTaskCallBack = new MyAsyncTaskCallBack () {
            @Override
            public void onSuccess() {                   
                   updateViewPager();
                }
            }
        };

        for(int taskCount  = 1; taskCount <= mTaskCount; taskCount ++){
            myAsyncTask = new MyAsyncTask();
            startMyAsyncTask(myAsyncTask );
        }
    }

   private void updateViewPager(){
        mSectionPagerAdapter.setPagerItem(myMap);
        mSectionPagerAdapter.notifyDataSetChanged();
        mCartCountIndicator.notifyDataSetChanged();
         mViewPager.invalidate();
    }
}

Not Sure what am I doing wrong here. Appreciate any suggestion.


Solution

  • Okay, figured since it is the activity that is not being refreshed itself I tried this.onResume() in the refresh method once I do notifyDataSetChanged(). It works.

    Not sure it is the cleanest solution though