Reputation: 109
I am trying to implement the Pull to refresh feature in Android. For this, I ended up using SwipeRefresLayout from Support library. But it is not working as I wanted to.
I am needed to implement it inside fragment. here is the code for fragment_home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/group_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="45dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</FrameLayout>
</LinearLayout>
And this is the fragment placed inside the MainActivity.java
public static class HomeFragment extends Fragment {
public static final String ARG_CATEGORY_NUMBER = "category_number";
private UiLifecycleHelper uiHelper;
public int currentimageindex = 0;
private SwipeRefreshLayout swipeLayout;
public HomeFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
//SWIPE TO REFRESH
swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener((OnRefreshListener) getActivity());
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
ListView hlvCategory = (ListView) rootView
.findViewById(R.id.group_content);
AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory,
mAdView, mDrawerList);
hjd.execute();
return rootView;
}
//SWIPE TO REFRESH
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
But this gives out the error as like it was unable to find the class for SwipeRefresh Layout. Can you suggest me how can i implement it here??
Upvotes: 0
Views: 1658
Reputation: 2457
Here in your fragment i found the problem is with set up the refresh listener
public static class HomeFragment extends Fragment implements OnRefreshListener{
public static final String ARG_CATEGORY_NUMBER = "category_number";
private UiLifecycleHelper uiHelper;
public int currentimageindex = 0;
private SwipeRefreshLayout swipeLayout;
public HomeFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
//SWIPE TO REFRESH
swipeLayout = (SwipeRefreshLayout) container.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
ListView hlvCategory = (ListView) rootView
.findViewById(R.id.group_content);
AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
HomeJsonData hjd = new HomeJsonData(getActivity(), hlvCategory,
mAdView, mDrawerList);
hjd.execute();
return rootView;
}
//SWIPE TO REFRESH
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
and make sure you have added supportV4 library
try as above this will help you,happy coding :)
Upvotes: 1
Reputation: 12605
You should implement the OnRefreshListener in your Fargment
public static class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener
and set swipeLayout.setOnRefreshListener(this);
Upvotes: 0
Reputation: 1268
Please refer this links, this may help
http://antonioleiva.com/swiperefreshlayout/
Upvotes: 0