Getting started with version 0.4

Tonic Artos edited this page Apr 1, 2015 · 2 revisions

These are instructions for version 0.4 only.

Adding to your project

Lastest Packaged Version
Maven central version

Gradle

The easiest way to add SuperSLiM to your project is to register it as a dependency in your build.gradle file.

dependencies {
    compile 'com.tonicartos:superslim:0.4.+'
}

Legacy Maven

For people using maven as a build system the following may be useful. Substitute VERSION with the latest version number displayed in the maven badge above.

<dependency>
    <groupId>com.tonicartos</groupId>
    <artifactId>superslim</artifactId>
    <version>VERSION</version>
</dependency>

Other

Manual AAR with Gradle
The .aar file for SuperSLiM is available from Maven Central and can be added manually to your libs folder inside your project. Add the following to your build.gradle file to compile .aar packages in your lib folder.

dependencies {
     compile fileTree(include ['*.aar'], dir: 'libs')
}

Basic usage

Create your recycler views in the usual way, create a LayoutManager, register one or more SectionLayoutManagers, and attach an adapter for the data to be displayed:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Add layout manager to recycler view.
    RecyclerView recyclerView = 
        (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LayoutManager(getActivity()));
    recyclerView.setAdapter(mAdapter);
}

The adapter has to do a little more work than a regular RecyclerView.Adapter. The adapter must ensure configuration details for sections are embedded in bound item views using layout parameters:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Item item = mItems.get(position);

    // A content binding implementation.
    holder.bindItem(item);

    /** Embed section configuration. **/
    final LayoutParams params = holder.getLayoutParams();

    params.setSlm(LinearSLM.ID); 

    // Position of the first item in the section. This doesn't have to
    // be a header. However, if an item is a header, it must then be the
    // first item in a section.
    params.setFirstPosition(item.sectionFirstPosition);
    holder.setLayoutParams(params);
}

It is important to note that setting the section manager and first position of each section is required for all views provided by the adapter.

As shown here with the section headers, you can embed any configuration using XML attributes rather than using LayoutParams directly:

<!-- TextView as a section header. -->
<!-- Add android:textDirection="locale" to support RTL languages. -->
<TextView
    android:id="@+id/text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textDirection="locale"
    app:slm_headerDisplay="start|sticky"
    app:slm_isHeader="true"
    app:slm_section_headerMarginStart="56dip"
    tools:text="Header Item"
    />

Make sure you have the right number of view types, and be sure to inflate the header correctly when creating your ViewHolders:

@Override
public int getItemViewType(int position) {
    return mItems.get(position).isHeader ? 
        VIEW_TYPE_HEADER : VIEW_TYPE_CONTENT;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view;
    if (viewType == VIEW_TYPE_HEADER) {
        view = inflator.inflate(R.layout.header_item, parent, false);
    } else {
        view = inflater.inflate(R.layout.content_item, parent, false);
    }
    return new ViewHolder(view);
}

Read the [User's Guide](User's guide for version 0.4) for more information.

Versioned Pages

0.4

[Getting Started](Getting started with version 0.4)
[User's Guide](User's guide for version 0.4)

version5 (WIP)

User documentation
[Basic usage](A simple tutorial)
[Advanced usage](All the cool things)
[SuperSLiM and RxJava](Using SuperSLiM with RxJava)

Developers documentation
Glossary
[The section graph](The section graph)
[Tracking data changes](Tracking data changes)
[Configuration transformations](Configuration transformations)
[Layout helpers](Layout helpers)
[Section configuration](Section configuration)
[Section state](Section state)
[Section layout managers](Section layout managers)
[Header layout managers](Header layout managers)

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.