The UniqueAdapter With DataBinding

The UniqueAdapter With DataBinding.

License

License

MIT
GroupId

GroupId

com.github.captain-miao
ArtifactId

ArtifactId

uniqueadapter
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

aar
Description

Description

The UniqueAdapter With DataBinding
The UniqueAdapter With DataBinding.
Project URL

Project URL

https://github.com/captain-miao/UniqueAdapter
Source Code Management

Source Code Management

https://github.com/captain-miao/UniqueAdapter

Download uniqueadapter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.captain-miao/uniqueadapter/ -->
<dependency>
    <groupId>com.github.captain-miao</groupId>
    <artifactId>uniqueadapter</artifactId>
    <version>1.1.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.captain-miao/uniqueadapter/
implementation 'com.github.captain-miao:uniqueadapter:1.1.0'
// https://jarcasting.com/artifacts/com.github.captain-miao/uniqueadapter/
implementation ("com.github.captain-miao:uniqueadapter:1.1.0")
'com.github.captain-miao:uniqueadapter:aar:1.1.0'
<dependency org="com.github.captain-miao" name="uniqueadapter" rev="1.1.0">
  <artifact name="uniqueadapter" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.captain-miao', module='uniqueadapter', version='1.1.0')
)
libraryDependencies += "com.github.captain-miao" % "uniqueadapter" % "1.1.0"
[com.github.captain-miao/uniqueadapter "1.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
com.android.support » recyclerview-v7 jar 25.3.1
com.android.databinding » library jar 1.3.1
com.android.databinding : baseLibrary jar 2.3.3
com.android.databinding » adapters jar 1.3.1

Project Modules

There are no modules declared in this project.

UniqueAdapter

The UniqueAdapter With DataBinding...

Gradle

Get library from oss.sonatype.org.io

repositories {

    maven { url 'https://oss.sonatype.org/content/repositories/releases' }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

}

dependencies {
    compile 'com.github.captain-miao:uniqueadapter:1.1.0'
}

// if use in library, compile exclude group: 'com.android.support'
dependencies {
    compile("com.github.captain-miao:uniqueadapter:1.1.0") {
        exclude group: 'com.android.support'
    }
    compile("com.android.support:recyclerview-v7:${this.supportLibrariesVersion}")
}

Step 1: define data model

public class TextModel extends BaseViewModel implements ItemModel {

    public String text;

    public TextModel(String text) {
        this.text = text;
    }

    // the layoutId == RecyclerView.Adapter.ItemViewType
    @Override
    public int getItemViewLayoutId() {
        return R.layout.rv_item_view_text;
    }
}

Step 2: define item view layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="com.github.captain_miao.uniqueadapter.model.TextModel"/>

        <import type="com.github.captain_miao.uniqueadapter.library.OnClickPresenter"/>

        <variable
            name="viewModel"
            type="TextModel"/>

        <variable
            name="onClickPresenter"
            type="OnClickPresenter"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{(v) -> onClickPresenter.onClick(v, viewModel)}"
        android:orientation="horizontal">

        <TextView
            android:paddingTop="@dimen/activity_horizontal_margin"
            android:paddingBottom="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:gravity="center_horizontal"
            android:text="@{viewModel.text}"/>


    </LinearLayout>


</layout>

Step 3: create UniqueAdapter for RecycleView

public class MainActivity extends AppCompatActivity implements OnClickPresenter<ItemModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_example);

        List<ItemModel> dataList = getMockData();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(new UniqueAdapter(dataList, this));
    }

    @Override
    public void onClick(View view, ItemModel item) {
        if(item instanceof TextModel){
            Toast.makeText(this, ((TextModel)item).text, Toast.LENGTH_SHORT).show();
        } else if(item instanceof ImageModel){
            Toast.makeText(this, ((ImageModel)item).url, Toast.LENGTH_SHORT).show();
        }
    }


    private List<ItemModel> getMockData(){
        List<ItemModel> dataList = new ArrayList<>();
        dataList.add(new TextModel("Photo 1"));
        dataList.add(new ImageModel("http://ww2.sinaimg.cn/bmiddle/7a8aed7bjw1f340c8jrk4j20j60srgpf.jpg"));
        dataList.add(new TextModel("Photo 2"));
        dataList.add(new ImageModel("http://ww1.sinaimg.cn/bmiddle/610dc034jw1f7ef7i5m1zj20u011hdjm.jpg"));

        return dataList;
    }
}

Step 4: run

unique-adapter-recycle-view

License

This project is licensed under the terms of the MIT license.

Versions

Version
1.1.0
1.0.3
1.0.2