Swipe Refresh Layout in Android Fragment

swiperefreshlayout recyclerview fragment
swiperefreshlayout not working in fragment
swiperefreshlayout androidx
swipe refresh layout android github
refresh fragment android
android pull to refresh scrollview
swipe refresh layout from bottom in android
android swiperefreshlayout linearlayout

Im trying to implement SwipeRefreshLayout on a Fragment.

I already solved it to implement it on an Activity, but when I implement it on Fragment: java.lang.NoClassDefFoundError: com...

Does anyone has an idea? Thanks!

Similar post without answers/solution: Swipe Refresh Layout - Class not found

My code:

public class Principal extends Fragment implements OnRefreshListener {
    SwipeRefreshLayout swipeLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.principal, container, false);

        swipeLayout = (SwipeRefreshLayout) view.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);

        return view;
    }
}

And my layout:

<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">

    <com.enhancedListView.EnhancedListView
        android:id="@+id/listaNoticias"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="5dp" >
    </com.enhancedListView.EnhancedListView>

</android.support.v4.widget.SwipeRefreshLayout>

Full stack trace

08-13 11:36:19.292: E/AndroidRuntime(31572): FATAL EXCEPTION: main
08-13 11:36:19.292: E/AndroidRuntime(31572): Process: com.xxx.xx, PID: 31572
08-13 11:36:19.292: E/AndroidRuntime(31572): java.lang.NoClassDefFoundError: com.xxx.fragments.Principal
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.xxx.hem.activities.MainActivity.CargarPrincipal(MainActivity.java:676)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.xxx.hem.activities.MainActivity.onCreate(MainActivity.java:297)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.Activity.performCreate(Activity.java:5231)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.os.Looper.loop(Looper.java:136)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at android.app.ActivityThread.main(ActivityThread.java:5001)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at java.lang.reflect.Method.invoke(Native Method)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Solved it :)

My XML

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <com.enhancedListView.EnhancedListView
        android:id="@+id/listaNoticias"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="5dp" >
    </com.enhancedListView.EnhancedListView>

</android.support.v4.widget.SwipeRefreshLayout>

My Fragment

public class Principal extends Fragment implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.principal, container, false);

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeColors(android.R.color.holo_green_dark, 
            android.R.color.holo_red_dark, 
            android.R.color.holo_blue_dark, 
            android.R.color.holo_orange_dark); (...)

@Override
public void onRefresh() {
    new myTask().execute();     
}

Swipe Refresh Layout in Android Fragment, Android sample program which implement SwipeRefreshLayout to refresh the extends Fragment { private static final int Item_Count = 15; SwipeRefreshLayout� MainActivity.java, SwipeRefreshLayoutFragment.java, swipe_refresh_layout_fragment.xml, colors.xml, strings.xml. Let’s do it one by one. MainActivity.java: Create this to add the fragment when the activity loads. Add the code in onCreate(){…}

Put your swipeLayout initialization in onViewCreated(View view, Bundle bundle) instead of onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

        swipeLayout = (SwipeRefreshLayout) view.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);
}

Android Articles � Swipe to Refresh Layout, You can also use the SwipeRefreshLayout widget with a ListFragment . If the layout contains a ListView with the ID "@android:id/list" , the swipe-� Declaring dependencies. To add a dependency on SwipeRefreshLayout, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.

Just be sure that you put the code for swipe refresh layout in override fun onViewCreated(view: View, savedInstanceState: Bundle?) {} instead of onCreateView.

Adding Swipe-to-Refresh To Your App, Android SwipeRefreshLayout is used for pull down to refresh or Swipe down to refresh screen ScrollView, ListView or RecyclerView. This lesson shows you how to update your app when the user requests a manual refresh, whether the user triggers the refresh with a swipe gesture or by using the action bar refresh action. Respond to the Refresh Gesture. When the user makes a swipe gesture, the system displays the progress indicator and calls your app's callback method.

Android SwipeRefreshLayout, SwipeRefreshLayout. The following examples show how to use android.support. v4.widget.SwipeRefreshLayout. These examples are extracted from open� Android TabLayout Example using with Swipe and Fragment in Android - Sliding Tabs - Duration: 24:00. Pull to Refresh Layout in android - Duration: 6:06. ID 20,199 views.

Swipe Refresh List Fragment, SwipeRefreshLayout is a new Android layout that adds pull to refresh This is the current implementation that worked for me (I am using a ListFragment):. Android SwipeRefreshLayout. Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView. The basic need for a SwipeRefreshLayout is to allow the users to refresh the screen manually.

Java Code Examples for android.support.v4.widget , SwipeRefreshLayout is a ViewGroup that can hold only one scrollable view as a child. This can be either a ScrollView or an AdapterView such as a ListView or a � But android made our day easier by introducing SwipeRefreshLayout in android.support.v4 to detect the vertical swipe on any view. Methods of SwipeRefresh are quite few: – setOnRefreshListener(OnRefreshListener): adds a listener to let other parts of the code know when refreshing begins.

Comments
  • Yeah, the error is "java.lang.NoClassDefFoundError: com..." ..; I'm dying to see the rest of the error.
  • @StephaneMathis updated with full stack trace
  • Ok, so what is at line 676 of your MainActivity ?
  • @StephaneMathis Principal h = new Principal(); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, h).commit();
  • new myTask().execute(); What is myTask here? @nacho_zona3
  • @AovinMahmud don't know if too late but MyTask is a BaseAsyncTask, you will execute (basically a thread)
  • you must call swipeLayout.setRefreshing(false); in onRefresh() method otherwise It will show refresh indicator continuously.
  • @user3931604 remove the last line return view; as onViewCreate() isn't supposed to return anything.