PtrListViewFragment

Introduction: 集成 Ultra-Pull-To-Refresh 项目,并且添加支持加载更多的功能
More: Author   ReportBugs   DemoAPK   
Tags:
listview-下拉刷新-加载更多-

这个是一个站在巨人肩膀上的项目。。。。

自从下拉刷新在移动段火起来了之后,在 Android 方面,大家一般使用的下拉刷新框架就是那个几个主流(大神自己写就不要来打脸了),选择那个大家可以参考下这个项目---Android-Ptr-Comparison,这位对这些框架从技术方面进行了详细的比较。

也是在这个项目中,认识了 PTR---android-Ultra-Pull-To-Refresh

但是,作者在该项目中是没有支持上拉加载更多模块。在很多项目中,自然会比较蛋疼。

为了解决下痛点。自己在使用这个过程中,总结的一些经验写成了这个项目。感谢巨人.

示例

默认加载更多 自定义加载更多底部布局 方便开发提供的下拉刷新头部 1 方便开发提供的下拉刷新头部 2

如果想要更换其他刷新头部,那么 PTR 这个项目,还是需要自己去琢磨琢磨的,因为每个应用的需求是不一样的。

compile

    com.zengcanxiang:ptrlistviewfragment:1.0.2

使用

使用步骤是很简单的,在 DEMO 中有。

首先,继承 ListViewFragment,重写和实现一些方法。


public class ExampleFragment extends ListViewFragment {
    private List<String> mList;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            mList.add(i + "");
        }
//        默认开启了刷新功能
//        installRefresh(true);
        installLoadMore(true);
        //初始化工作完成调用该方法
        initOk();
    }

    @Override
    public void install() {
//      material 风格 head
//        materialHeadInstall();
//      文字头部 head
        valueHeadInstall("zengcanxiang");
    }

    @Override
    protected void refresh() {
        Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_SHORT).show();
        refreshComplete();
    }

    @Override
    protected void loadMore() {
        Toast.makeText(getActivity(), "加载更多", Toast.LENGTH_SHORT).show();
        loadMoreComplete();
    }

    @Override
    protected BaseAdapter bindAdapter() {
        return new MyAdapter(mList, getActivity(), R.layout.test_list_item, R.layout.test_list_item2);
    }

    //listView 的适配器
    class MyAdapter extends HelperAdapter<String> {
        public MyAdapter(List data, Context context, int... layoutIds) {
            super(data, context, layoutIds);
        }

        @Override
        public int checkLayout(int position, String item) {
            if (position % 2 == 0) {
                return 1;
            }
            return 0;
        }

        @Override
        public void HelpConvert(HelperHolder helperHolder, int i, String s) {
            TextView testText = helperHolder.getView(R.id.testText);
            testText.setText(i + "");
        }
    }
}

refresh()方法和 loadMore()方法是重写的,继承该 fragment 不会强制要求实现,因为可能某些情况是不需要的,就不强制子类重写了。需要的话,自行在子类重写。

对了,这个里面继承的 Adapter 是我的另一个项目---BaseAdapter,但是只是在示例中导入了,不会在 PtrListViewFragment 中出现,如果需要,请自行导入。不为大家的方法数加负担了。

然后在 Activity 中像平常一样的使用该 fragment 就行了。


public class MainActivity extends AppCompatActivity {
    private ExampleFragment fragment;

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

    public void findViews() {
        //实例化 fragment
        fragment = new ExampleFragment();
    }

    public void setViewsContent() {
//        设置自定义的加载更多布局
//        fragment.setLoadMore_LayoutId(R.layout.test_load_more);
//        fragment.setLoadMore_viewId(R.id.test_loadMoreView);
//        将 fragment 加载到 activity 中
        //因为继承的 fragment 是 v4 包中的,管理需要使用 getSupportFragmentManager
        getSupportFragmentManager().beginTransaction().add(R.id.testFragment,fragment).commit();
    }
}

在上面代码中可以看见,这个 fragment 是可以设置自己的加载更多布局的。就像普通的对象赋值一样。

好了,源码也就一个类,有注释,欢迎大家指点,批评!希望能帮到一些需要的人。

License

This library is licensed under the Apache Software License, Version 2.0.

See LICENSE for full of the license text.

Copyright (C) 2015 [Hanks](https://github.com/ZengcxAperson)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools