ActionBar - PullToRefresh
Asked Answered
W

2

7

I am developing an App where you can view BusStop Timetables. And users can refresh with the ActionBar PullToRefresh (Library). My app has also translucent statusbar on KitKat enabled.

Now the ActionBar Overlay, which should be normally overlap the actionbar, is now displaced upwards.

How can I solve that?

Best regards!

Wench answered 9/5, 2014 at 23:41 Comment(4)
So, can you post us some code ? And if possible an image of what is going on would helpLongo
you should update your answer for more detail what have you tried, then everyone can help you.Dallapiccola
Providing code will help get an answer. In the mean time, this tutorial helped me get mine going, perhaps it can help you.Jeopardous
I don't think this question needs code, the problem is that the pull to refresh bar does not have the margin of the status bar anymore and is displayed right at the top of the screen.Maggee
M
10

What I did is create a custom header layout for the pull-to-refresh layout. I just copied the original one, added the height of the status bar which is usually 25dp and added a 25dp top padding.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="73dp"
    android:paddingTop="25dp" >

    <FrameLayout
        android:id="@id/ptr_content"
        android:layout_width="match_parent"
        android:layout_height="73dp" >

        <TextView
            android:id="@id/ptr_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </FrameLayout>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@id/ptr_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="@dimen/ptr_progress_bar_stroke_width" />

</RelativeLayout>

Now set the layout when you setup the pull to refresh layout:

ActionBarPullToRefresh.from(getActivity()).listener(new OnRefreshListener() {

    @Override
    public void onRefreshStarted(View view) {
        refresh();
    }

}).headerLayout(R.layout.header).build()).setup(ptrLayout);
Maggee answered 29/7, 2014 at 15:4 Comment(4)
Could you award the bounty if it helped :)Maggee
Sure :) But how do I do that?Wench
Is there an award bounty button?Maggee
@MarkBuikema I don't think you can award the bounty to yourself. In any case, putting 25dp as a fixed value seems a little brittle, did you check for example #20584825 ?Inappetence
K
1

try this.

res/layout/activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.swipetorefresh.MainActivity"
  tools:ignore="MergeRootFrame" />

res/layout/fragment_main.xml

<android.support.v4.widget.SwipeRefreshLayout           
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:ignore="MergeRootFrame" >

   <ListView
       android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

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

MainActivity.java

  public class MainActivity extends Activity {

   @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}
}

  public static class PlaceholderFragment extends ListFragment implements OnRefreshListener {

private SwipeRefreshLayout mSwipeRefreshLayout;

private static final int LIST_ITEM_COUNT = 5;
private int mOffset = 0;

private ArrayAdapter<String> mListAdapter;

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);

    // Configure the swipe refresh layout
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.container);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setColorScheme(
            R.color.swipe_color_1, R.color.swipe_color_2,
            R.color.swipe_color_3, R.color.swipe_color_4);

    // Put the first batch of countries in the list
    mListAdapter = new ArrayAdapter<String>(
            getActivity(),
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            getCountries(mOffset));

    setListAdapter(mListAdapter);

    return rootView;
}

private List<String> getCountries(int offset) {
    ArrayList<String> countriesList = new ArrayList<String>();
    for(int i=0; i<LIST_ITEM_COUNT;i++){
        countriesList.add(COUNTRIES[offset+i]);
    }

    mOffset = offset + LIST_ITEM_COUNT;
    return countriesList;
}

@Override
public void onRefresh() {
    // Start showing the refresh animation
    mSwipeRefreshLayout.setRefreshing(true);

    // Simulate a long running activity
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           updateCountries();
        }
    }, 5000);
}



private void updateCountries() {

    // Add the next batch of countries to the list
    mListAdapter.addAll(getCountries(mOffset));

    // Signify that we are done refreshing
    mSwipeRefreshLayout.setRefreshing(false);
}



private static final String[] COUNTRIES = {"Afghanistan",
    "Albania", "Algeria", "American Samoa", "Andorra", "Angola",
    "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
    "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
    "Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
    "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil",
    "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi",
    "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
    "Central African Republic", "Chad", "Chile", "China",
    "Christmas Island", "Cocos (Keeling) Islands", "Colombia",
    "Comoros", "Democratic Republic of the Congo (Kinshasa)",
    "Congo, Republic of(Brazzaville)", "Cook Islands", "Costa Rica",
    "Ivory Coast", "Croatia", "Cuba", "Cyprus", "Czech Republic",
    "Denmark", "Djibouti", "Dominica", "Dominican Republic",
    "East Timor (Timor-Leste)", "Ecuador", "Egypt",
    "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia"};

    }
 }
Kung answered 2/8, 2014 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.