web-dev-qa-db-fra.com

Comment implémenter "Glissez vers le bas pour actualiser" comme dans la nouvelle application GMail

Google publie la nouvelle application Gmail avec une autre façon de gérer le menu déroulant pour l'actualiser.

Au lieu d'afficher la ligne cachée commencée qui est tirée vers le bas. Gmail affiche un message animé au-dessus de la barre d'action.

Le message comprend une ligne horizontale animée.

Est-ce une fonctionnalité standard du SDK Android Android? Je ne trouve rien dans l'API de la barre d'action qui le ferait.

enter image description here

33
Reactgular

la bibliothèque ActionBar-PullToRefresh de Chris Banes sur GitHub offre probablement une fonctionnalité de pull-to-refresh la plus proche de l'application GMail.

Voir aussi: analyse par Juhani Lehtimäki du GMail pull-to-refresh .

22
laalto

Google a publié la prise en charge de cela directement dans le SDK. Je ne sais pas quelle version vous devez prendre en charge (cela peut être un problème).

Consultez les informations officielles sur les fonctionnalités du SDK ici: http://developer.Android.com/reference/Android/support/v4/widget/SwipeRefreshLayout.html

Si vous pouvez utiliser celui du SDK, vous serez mieux, même Chris Banes a écrit un post , suggérant la même chose.

48
Booger

essayez ça ... c'est du travail pour moi.

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

        }
   }
10
Rakesh Rangani

Chris Banes (le même gars qui a implémenté le meilleur composant pull to refresh pour Android) a également implémenté le Gmail comme Pull To Refresh.

Vous pouvez le trouver ici: https://github.com/chrisbanes/ActionBar-PullToRefresh

Notez que ce projet est toujours en cours de développement, donc l'API actuelle peut changer.

3
sonida
fragment_main.xml

<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:paddingBottom="@dimen/activity_vertical_margin"
    Android:paddingLeft="@dimen/activity_horizontal_margin"
    Android:paddingRight="@dimen/activity_horizontal_margin"
    Android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testloading.MainActivity$PlaceholderFragment" >

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

</RelativeLayout>    


counteries.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical" >

    <TextView 
        Android:id="@+id/text_view"
        Android:layout_width="match_parent"
        Android:layout_height="47dp"
        Android:gravity="center_vertical"
        Android:textStyle="bold"/>
</LinearLayout>



package com.example.testloading;

import Java.util.ArrayList;
import Java.util.List;

import Android.content.Context;
import Android.os.Bundle;
import Android.support.v4.app.Fragment;
import Android.support.v7.app.ActionBarActivity;
import Android.util.Log;
import Android.view.LayoutInflater;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.AbsListView;
import Android.widget.AbsListView.OnScrollListener;
import Android.widget.BaseAdapter;
import Android.widget.ListView;
import Android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        private ListView listView;
        private static final int LIST_ITEM_COUNT = 20;
        private int mOffset = 0;
        private boolean flag_loading;

        private MyAdapter adapter;
        private List<String> list;

        public PlaceholderFragment() {
        }

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

            listView = (ListView) rootView.findViewById(R.id.list);
            list = getCountries(mOffset);
            adapter = new MyAdapter(list, getActivity());
            listView.setAdapter(adapter);
            listView.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view,
                        int scrollState) {
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    Log.d(TAG, "firstVisibleItem : " + firstVisibleItem
                            + " , visibleItemCount : " + visibleItemCount
                            + " , totalItemCount : " + totalItemCount);
                    if (firstVisibleItem + visibleItemCount == totalItemCount) {
                        Log.d(TAG, "ZZZ offSet : " + mOffset);
                        if (COUNTRIES.length > mOffset) {
                            if (flag_loading == false) {
                                Log.d(TAG, "ZZZ inside : ");
                                flag_loading = true;
                                additems();
                            }
                        }
                    }

                }
            });
            return rootView;
        }

        protected void additems() {
            list.addAll(getCountries(mOffset));
            adapter.notifyDataSetChanged();
            listView.invalidate();
            flag_loading = false;
        }

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

            mOffset = offset + LIST_ITEM_COUNT;
            return countriesList;
        }

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

    }
}

class MyAdapter extends BaseAdapter {

    private List<String> list;
    private LayoutInflater layoutInflater;

    public MyAdapter(List<String> list, Context context) {
        this.list = list;
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.counteries, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView
                    .findViewById(R.id.text_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(list.get(position));
        return convertView;
    }

    class ViewHolder {
        public TextView textView;
    }
}
1
App Kart

Essaye-le. Utilisation: swipeRefreshLayout.setRotation (180f);

Et dans votre adaptateur ListView, méthode getView: view.setRotation (180f);

inverser l'ordre de vos articles dans la liste.

Ou en utilisant directement Android: rotation = "180" en xml.