Code Monkey home page Code Monkey logo

techrecyclerview's Introduction

RefreshRecyclerView

  • RecyclerAdapter : 支持下拉刷新,上拉加载,添加Header,Footer
  • MultiTypeAdapter : 针对复杂数据类型的数据列表

使用方法

  • gradle依赖
   compile 'com.github.goEcar:EcarRecyclerView:1.0.0'

  • xml布局文件
    <com.ecar.recycler.RefreshRecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:refresh_able="true"
        app:load_more_able="false"/>
  • java代码
   mRecyclerView = (RefreshRecyclerView) findViewById(R.id.recycler_view);
        mAdapter = new CardRecordAdapter(this);
        listViewManager = new ListViewManager(this, mRecyclerView, mAdapter);
//        添加Header
        final TextView textView = new TextView(this);
        textView.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
        textView.setTextSize(16);
        textView.setGravity(Gravity.CENTER);
        textView.setText("我是header");
        listViewManager.setHeader(textView);

        //添加footer
        final TextView footer = new TextView(this);
        footer.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
        footer.setTextSize(16);
        footer.setGravity(Gravity.CENTER);
        footer.setText("我是Footer");
        listViewManager.setFooter(footer);

        //顶部颜色
        listViewManager.setTopColor(getResources().getColor(R.color.blue_light),
                getResources().getColor(R.color.blue_light),
                getResources().getColor(R.color.grey));

        //下拉刷新
        listViewManager.setTopRefresh(new Action() {
            @Override
            public void onAction() {
                listViewManager.getData(true, Arrays.asList(getVirtualData()));
            }
        });

        //上拉加载
        listViewManager.setMoreData(new Action() {
            @Override
            public void onAction() {
                listViewManager.getData(false, Arrays.asList(getVirtualData()));
                listViewManager.page++;
            }
        });

        //是否允许上拉加载
        listViewManager.setEnableMore(false);
        //是否允许下拉刷新
        listViewManager.setEnableRefresh(true);

        // 设置listview为空的时候显示的view
        listViewManager.setEmptyView(findViewById(R.id.iv_empty));


        //初始化数据
        listViewManager.initData(Arrays.asList(getVirtualData()));

###RecyclerAdapter

针对相同数据类型列表,可添加Header,Footer

自定义Adapter应该继承RecyclerAdapter,如:

class CardRecordAdapter extends RecyclerAdapter<Consumption> {

    public CardRecordAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder<Consumption> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new CardRecordHolder(parent);
    }
}

###修改提示上拉布局需要重写view_status_last.xml布局,id保持不变

   <LinearLayout
        android:id="@+id/load_more_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="8dp"
        android:visibility="gone">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="正在加载,这个修改过..."
            android:textSize="16sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/no_more_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="没有更多了~这个修改过"
        android:textSize="16sp"
        android:visibility="gone" />

###ViewHolder

自定义ViewHolder需继承BaseViewHolder,如:

class CardRecordHolder extends BaseViewHolder<Consumption> {

    //当使用MultiTypeAdapter时,务必加上此构造方法
    public CardRecordHolder(ViewGroup parent) {
        super(parent, R.layout.holder_consume);
    }

    @Override
    public void setData(Consumption object) {
        super.setData(object);
        name.setText("Demo");
        //UI绑定数据
    }

    @Override
    public void onInitializeView() {
        super.onInitializeView();
        name = findViewById(R.id.name);
        //初始化View
    }

    @Override
    public void onItemViewClick(Consumption object) {
        super.onItemViewClick(object);
        //点击事件
    }
}

注意:MultiTypeAdapter的ViewHolder的构造函数保证能反射时获取,应该写成静态或者public的单独类

注意事项

  • 依赖了其他库
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:support-annotations:23.4.0'

techrecyclerview's People

Contributors

jinzheng519 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.