-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
491 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
presentation/src/main/java/com/crazysunj/crazydaily/base/BaseAdapter.java
This file was deleted.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
presentation/src/main/java/com/crazysunj/crazydaily/base/BaseHelperAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Copyright 2017 Sun Jian | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package com.crazysunj.crazydaily.base; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.crazysunj.multitypeadapter.entity.MultiTypeEntity; | ||
import com.crazysunj.multitypeadapter.helper.RecyclerViewAdapterHelper; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Modifier; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* author: sunjian | ||
* created on: 2017/8/3 下午5:18 | ||
* description:https://github.com/crazysunj/CrazyDaily | ||
*/ | ||
public abstract class BaseHelperAdapter<T extends MultiTypeEntity, VH extends BaseViewHolder, H extends RecyclerViewAdapterHelper<T>> extends RecyclerView.Adapter<VH> { | ||
|
||
protected Context mContext; | ||
protected LayoutInflater mLayoutInflater; | ||
protected H mHelper; | ||
|
||
public BaseHelperAdapter(H helper) { | ||
helper.bindAdapter(this); | ||
mHelper = helper; | ||
} | ||
|
||
@Override | ||
public int getItemViewType(int position) { | ||
return mHelper.getItemViewType(position); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull VH holder, int position) { | ||
convert(holder, mHelper.getData().get(position)); | ||
} | ||
|
||
protected abstract void convert(VH holder, T item); | ||
|
||
@NonNull | ||
@Override | ||
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
return createBaseViewHolder(parent, mHelper.getLayoutId(viewType)); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return mHelper.getData().size(); | ||
} | ||
|
||
protected VH createBaseViewHolder(ViewGroup parent, int layoutResId) { | ||
if (mContext == null) { | ||
mContext = parent.getContext(); | ||
} | ||
if (mLayoutInflater == null) { | ||
mLayoutInflater = LayoutInflater.from(mContext); | ||
} | ||
return createBaseViewHolder(mLayoutInflater.inflate(layoutResId, parent, false)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected VH createBaseViewHolder(View view) { | ||
Class temp = getClass(); | ||
Class z = null; | ||
while (z == null && null != temp) { | ||
z = getInstancedGenericKClass(temp); | ||
temp = temp.getSuperclass(); | ||
} | ||
VH vh = createGenericKInstance(z, view); | ||
return null != vh ? vh : (VH) new BaseViewHolder(view); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private VH createGenericKInstance(Class z, View view) { | ||
try { | ||
Constructor constructor; | ||
String buffer = Modifier.toString(z.getModifiers()); | ||
String className = z.getName(); | ||
if (className.contains("$") && !buffer.contains("static")) { | ||
constructor = z.getDeclaredConstructor(getClass(), View.class); | ||
return (VH) constructor.newInstance(this, view); | ||
} else { | ||
constructor = z.getDeclaredConstructor(View.class); | ||
return (VH) constructor.newInstance(view); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private Class getInstancedGenericKClass(Class z) { | ||
Type type = z.getGenericSuperclass(); | ||
if (type instanceof ParameterizedType) { | ||
Type[] types = ((ParameterizedType) type).getActualTypeArguments(); | ||
for (Type temp : types) { | ||
if (temp instanceof Class) { | ||
Class tempClass = (Class) temp; | ||
if (BaseViewHolder.class.isAssignableFrom(tempClass)) { | ||
return tempClass; | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
presentation/src/main/java/com/crazysunj/crazydaily/base/BaseViewHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2017 Sun Jian | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package com.crazysunj.crazydaily.base; | ||
|
||
import android.support.v7.widget.RecyclerView; | ||
import android.util.SparseArray; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
/** | ||
* author: sunjian | ||
* created on: 2017/8/3 下午5:18 | ||
* description:https://github.com/crazysunj/CrazyDaily | ||
*/ | ||
public class BaseViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private SparseArray<View> mViews; | ||
|
||
public BaseViewHolder(View view) { | ||
super(view); | ||
if (mViews == null) { | ||
mViews = new SparseArray<>(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends View> T getView(int viewId) { | ||
View view = mViews.get(viewId); | ||
if (view == null) { | ||
view = itemView.findViewById(viewId); | ||
mViews.put(viewId, view); | ||
} | ||
return (T) view; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends View> T getView(int viewId, Class<T> clazz) { | ||
View view = mViews.get(viewId); | ||
if (view == null) { | ||
view = itemView.findViewById(viewId); | ||
mViews.put(viewId, view); | ||
} | ||
return (T) view; | ||
} | ||
|
||
public void setText(int viewId, CharSequence text) { | ||
getTextView(viewId).setText(text); | ||
} | ||
|
||
public void setImageResource(int viewId, int resId) { | ||
getImageView(viewId).setImageResource(resId); | ||
} | ||
|
||
public void setVisible(int viewId, boolean visible) { | ||
getView(viewId).setVisibility(visible ? View.VISIBLE : View.GONE); | ||
} | ||
|
||
public TextView getTextView(int viewId) { | ||
return getView(viewId, TextView.class); | ||
} | ||
|
||
public ImageView getImageView(int viewId) { | ||
return getView(viewId, ImageView.class); | ||
} | ||
|
||
} |
Oops, something went wrong.