RendererRecyclerViewAdapter

简介:A single adapter with multiple view types for the whole project
更多:作者   提 Bug   官网   
标签:

Wiki

https://github.com/vivchar/RendererRecyclerViewAdapter/wiki

Example

Example

Gradle

dependencies {
    compile 'com.github.vivchar:RendererRecyclerViewAdapter:+'
}

Motivation

https://vivchar.github.io/RendererRecyclerViewAdapter

Usage

  • Step 1. Add the ViewModel interface to your UI model
public class SomeModel implements ViewModel {
    ...
}
  • Step 2. Implement SomeViewHolder
public class SomeViewHolder extends RecyclerView.ViewHolder {

    public TextView mTitle;

    public SomeViewHolder(View itemView) {
        super(itemView);
        mTitle = (TextView) itemView.findViewById(R.id.title);
        ...
    }
}
  • Step 3. Implement SomeViewRenderer
public class SomeViewRenderer extends ViewRenderer<SomeModel, SomeViewHolder> {

    public SomeViewRenderer(Context context) {
        super(SomeModel.class, context);
    }

    @Override
    public void bindView(SomeModel model, SomeViewHolder holder) {
        holder.mTitle.setText(model.getTitle());
        ...
    }

    @Override
    public SomeViewHolder createViewHolder(ViewGroup parent) {
        return new SomeViewHolder(inflate(R.layout.some_item, parent));
    }
}
  • Step 4. Initialize Adapter and register the SomeViewRenderer
public class SomeActivity extends AppCompatActivity {

    private RendererRecyclerViewAdapter mRecyclerViewAdapter;
    private RecyclerView mRecyclerView;

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

        mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
        mRecyclerViewAdapter.registerRenderer(new SomeViewRenderer(this));
        mRecyclerViewAdapter.registerRenderer(...); /* you can use several types of cells */

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mRecyclerViewAdapter);

        mRecyclerViewAdapter.setItems(getItems());
        mRecyclerViewAdapter.notifyDataSetChanged();
    }
    ...
}

Migration to v2.0

//TODO

License

Copyright 2017 Vitaly Vivchar

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.
随手点击下面广告,支持我维护本站吧
随手点击下面广告支持我免费维护吧
Android 开发经验分享
随手点击下面广告支持本站吧
 
Android 开发经验分享