Skip to content

Commit

Permalink
Added ViewHolder resolver by object type
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Sep 6, 2016
1 parent d8d9b32 commit 6521d1c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;

import com.neurospeech.hypercube.HyperCubeApplication;
import com.neurospeech.hypercubesample.adapters.NavigationViewHolder;

/**
* Created by akash.kava on 22-04-2016.
Expand All @@ -14,5 +15,7 @@ public void onCreate() {
super.onCreate();

HyperCubeApplication.init(this);

HyperCubeApplication.registerViewHolderType(NavigationViewHolder.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,16 @@
* Created by on 13-08-2016.
*/
public class NavigationListAdapter
extends HeaderedAdapter<MenuModel,NavigationListAdapter.NavigationViewHolder> {
extends HeaderedAdapter<MenuModel,RecyclerView.ViewHolder> {

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

@Override
public Object getHeader(MenuModel item) {
return null;
return item.header;
}

@Override
protected void onBind(NavigationViewHolder holder, MenuModel item) {

}

@Override
protected NavigationViewHolder createViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
return null;
}

static class NavigationViewHolder extends RecyclerView.ViewHolder{


public NavigationViewHolder(View itemView) {
super(itemView);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.neurospeech.hypercubesample.adapters;

import android.view.Menu;
import android.view.View;

import com.neurospeech.hypercube.ui.HyperItemViewHolder;
import com.neurospeech.hypercube.ui.HyperViewHolder;
import com.neurospeech.hypercubesample.R;
import com.neurospeech.hypercubesample.model.MenuModel;

/**
* Created by akash.kava on 06-09-2016.
*/

@HyperItemViewHolder(R.layout.item_menu)
public class NavigationViewHolder extends HyperViewHolder<MenuModel> {

public NavigationViewHolder(View itemView) {
super(itemView);
}

@Override
protected void bind(MenuModel item) {

}
}
4 changes: 2 additions & 2 deletions hypercube/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1413
versionName "1.4.13"
versionCode 1414
versionName "1.4.14"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;


Expand Down Expand Up @@ -44,10 +46,22 @@ public class HyperCubeApplication {
= new HashMap<>();


public static void registerViewHolderType(Class<HyperViewHolder<?>> viewHolderClass){
public static void registerViewHolderType(Class<? extends HyperViewHolder<?>> viewHolderClass){
HyperItemViewHolder ivh = (HyperItemViewHolder) viewHolderClass
.getAnnotation(HyperItemViewHolder.class);
modelLayouts.put(ivh.modelType(),ivh.value());
if(ivh==null)
throw new IllegalArgumentException("HyperItemViewHolder annotation is missing on ViewHolder class");

// try to fetch modelType...

Class modelType = null;

Type t = viewHolderClass.getGenericSuperclass();

ParameterizedType pt = (ParameterizedType)t;
modelType = (Class)pt.getActualTypeArguments()[0];

modelLayouts.put(modelType,ivh.value());
layoutViewHolders.put(ivh.value(),viewHolderClass);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface HyperItemViewHolder {
int value () ;
Class modelType();
}

0 comments on commit 6521d1c

Please sign in to comment.