【问题标题】:Android PullToRefesh library customize loading pulling/refreshing imageAndroid PullToRefesh 库自定义加载拉取/刷新图像
【发布时间】:2014-08-12 05:49:46
【问题描述】:

我正在使用 com.handmark.pulltorefresh.library.PullToRefreshListView。默认显示:

但我希望 pulltorefresh 图像/设计如下:

这是我正在使用的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/lv_Inbox"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:divider="#19000000"
    android:dividerHeight="4dp"
    android:fadingEdge="none"
    android:fastScrollEnabled="false"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false"
    android:scrollbars="none"
    android:smoothScrollbar="true"
    ptr:ptrAnimationStyle="flip"/>

<ProgressBar
    android:id="@+id/progress_LoadingList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:visibility="visible" />

    <TextView
    android:id="@+id/lbl_NoMessagesFound"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:visibility="gone" />
 </RelativeLayout>

【问题讨论】:

    标签: android pull-to-refresh


    【解决方案1】:

    动画样式控制如何向用户呈现“下拉刷新”功能。不同的选项是:

    你想要翻转,所以在你的 xml 视图中

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_to_refresh_listview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        ptr:ptrAnimationStyle="rotate"
        />  
    

    改变

    ptr:ptrAnimationStyle="rotate"
    

    ptr:ptrAnimationStyle="flip"
    

    并在com.handmark.pulltorefresh.library.PullToRefreshListView标签中添加xmlns:ptr="http://schemas.android.com/apk/res-auto"

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/lv_Inbox"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:scrollbars="none"
        android:smoothScrollbar="true"
        ptr:ptrAnimationStyle="flip"/>
    

    更多信息请见android-pull-to-refresh Customization

    【讨论】:

    • 我正在尝试使用 ptr:ptrAnimationStyle="flip" 但它给了我错误:解析 XML 时出错:未绑定前缀。你有什么解决办法吗?
    • 感谢链接,但已经知道如何更改命名空间和使用它。我已经更新了我的问题并添加了 xml 文件。您能否指定要更改的内容。谢谢
    • @NoumanBhatti 在com.handmark.pulltorefresh.library.PullToRefreshListView 中添加xmlns:ptr="http://schemas.android.com/apk/res-auto",查看答案。
    【解决方案2】:

    高级下拉刷新将帮助您在应用中提供modern approach。 实现起来非常smooth and easy。它还将支持较低版本(从 v2.3 开始)。下面的屏幕截图会让人想起新的刷新。

    界面部分

    从您的 Eclipse 或 Android Studio 创建一个 Android。

    在您的活动布局中添加 SwipeRefreshLayout。

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
        <TextView
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"/>
    </ScrollView>
    

    注意:布局内只有视图。在 SwipeRefresh 布局内添加 scrollView 以支持拉动刷新。对于 ListView 和 GridView,无需在 SwipeRefreshLayout 中添加 ScrollView。

    代码部分

    在 Activity 的 onCreate 方法中添加以下行。

    公共类 SwipeActivity 扩展 Activity 实现 OnRefreshListener { SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe);
         swipeLayout = (SwipeRefreshLayout) 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); }

    @Override
        public void onRefresh() {
            // TODO Auto-generated method stub
            new Handler().postDelayed(new Runnable() {
                @Override public void run() {
                    swipeLayout.setRefreshing(false);
                }
            }, 5000);
        }
    

    }
    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);
    To stop the loading progress swipeLayout.setRefreshing(false);.
    

    就是这样。运行你的应用程序,你也做了新的拉动来刷新。

    使用列表视图滑动刷新

    这里我添加了一个使用 listview 拉取刷新的示例。然后你就会知道集成是多么容易。

    在您的应用程序中创建一个新的 Activity,在您的 SwipeRefreshLayout 中添加列表视图。

    <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/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>
    

    为列表视图、适配器、列表项的字符串数组声明和初始化变量。当您拉动刷新时,下面的代码将添加数组中的列表项。

    public class SwipeActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_swipe);
    
    
            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.swipe, 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 implements OnRefreshListener {
            SwipeRefreshLayout swipeLayout; 
            ListView listView;
            ArrayAdapter adapter;
            ArrayList< String> arrayList;
            String [] array = new String[]{"Apple","Batman","captain America","darkknight"};
    
            public PlaceholderFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);
                swipeLayout = (SwipeRefreshLayout) rootView.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= (ListView) rootView.findViewById(R.id.listview);
    
    
                adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, appendData());
                listView.setAdapter(adapter);
    
    
                return rootView;
            }
    
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                new Handler().postDelayed(new Runnable() {
                    @Override public void run() {
                        appendData();
                        adapter.notifyDataSetChanged();
                        swipeLayout.setRefreshing(false);
                    }
                }, 5000);
            }
    
    
    
            private ArrayList appendData(){
                if(arrayList==null)
                    arrayList =  new ArrayList();
    
                for (String items : array) {
                    arrayList.add(items);
                }
                return arrayList;
            }
        }
    
    }
    

    注意:我使用了 appcompat 库来支持较低版本的操作栏。所以我的活动扩展了 ActionBarActivity。在 eclipse 和 studio 的更新 ADT 版本中,您的应用程序将使用片段概念创建。因此,应用程序是使用即时片段概念创建的。

    我猜你喜欢这个例子。自己尝试并练习。

    编码愉快:-)。

    结果屏幕,看这个:

    对于您现有的程序,请使用this 链接

    【讨论】:

    • 能否提供你的例子的示例应用程序,plz
    • 对于您的旧版本,您可以使用这个:jmsliu.com/1529/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2018-01-25
    • 2018-09-17
    • 2018-09-20
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    相关资源
    最近更新 更多