Java tutorial
/** * Copyright 2015 bingoogolapple * <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 cn.bingoogolapple.androidcommon.adapter; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; import android.support.annotation.IdRes; import android.support.annotation.StringRes; import android.support.v4.util.SparseArrayCompat; import android.support.v7.widget.RecyclerView; import android.text.Html; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Checkable; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; /** * : :bingoogolapple@gmail.com * :15/5/26 17:06 * ??:AdapterViewRecyclerViewitem?? */ public class BGAViewHolderHelper implements View.OnLongClickListener, CompoundButton.OnCheckedChangeListener, View.OnTouchListener { protected final SparseArrayCompat<View> mViews; protected BGAOnItemChildClickListener mOnItemChildClickListener; protected BGAOnItemChildLongClickListener mOnItemChildLongClickListener; protected BGAOnItemChildCheckedChangeListener mOnItemChildCheckedChangeListener; protected BGAOnRVItemChildTouchListener mOnRVItemChildTouchListener; protected View mConvertView; protected Context mContext; protected int mPosition; protected BGARecyclerViewHolder mRecyclerViewHolder; protected RecyclerView mRecyclerView; protected AdapterView mAdapterView; /** * ?? */ protected Object mObj; public BGAViewHolderHelper(ViewGroup adapterView, View convertView) { mViews = new SparseArrayCompat<>(); mAdapterView = (AdapterView) adapterView; mConvertView = convertView; mContext = convertView.getContext(); } public BGAViewHolderHelper(RecyclerView recyclerView, BGARecyclerViewHolder recyclerViewHolder) { mViews = new SparseArrayCompat<>(); mRecyclerView = recyclerView; mRecyclerViewHolder = recyclerViewHolder; mConvertView = mRecyclerViewHolder.itemView; mContext = mConvertView.getContext(); } public BGARecyclerViewHolder getRecyclerViewHolder() { return mRecyclerViewHolder; } public void setPosition(int position) { mPosition = position; } public int getPosition() { if (mRecyclerViewHolder != null) { return mRecyclerViewHolder.getAdapterPositionWrapper(); } return mPosition; } /** * item?? * * @param onItemChildClickListener */ public void setOnItemChildClickListener(BGAOnItemChildClickListener onItemChildClickListener) { mOnItemChildClickListener = onItemChildClickListener; } /** * idviewIditem?? * * @param viewId */ public void setItemChildClickListener(@IdRes int viewId) { View view = getView(viewId); if (view != null) { view.setOnClickListener(new BGAOnNoDoubleClickListener() { @Override public void onNoDoubleClick(View v) { if (mOnItemChildClickListener != null) { if (mRecyclerView != null) { mOnItemChildClickListener.onItemChildClick(mRecyclerView, v, getPosition()); } else if (mAdapterView != null) { mOnItemChildClickListener.onItemChildClick(mAdapterView, v, getPosition()); } } } }); } } /** * item?? * * @param onItemChildLongClickListener */ public void setOnItemChildLongClickListener(BGAOnItemChildLongClickListener onItemChildLongClickListener) { mOnItemChildLongClickListener = onItemChildLongClickListener; } /** * idviewIditem?? * * @param viewId */ public void setItemChildLongClickListener(@IdRes int viewId) { View view = getView(viewId); if (view != null) { view.setOnLongClickListener(this); } } /** * RecyclerView item ?? * * @param onRVItemChildTouchListener */ public void setOnRVItemChildTouchListener(BGAOnRVItemChildTouchListener onRVItemChildTouchListener) { mOnRVItemChildTouchListener = onRVItemChildTouchListener; } /** * id viewId RecyclerView item ?? * * @param viewId */ public void setRVItemChildTouchListener(@IdRes int viewId) { View view = getView(viewId); if (view != null) { view.setOnTouchListener(this); } } /** * item???? * * @param onItemChildCheckedChangeListener */ public void setOnItemChildCheckedChangeListener( BGAOnItemChildCheckedChangeListener onItemChildCheckedChangeListener) { mOnItemChildCheckedChangeListener = onItemChildCheckedChangeListener; } /** * idviewIditem???? * * @param viewId */ public void setItemChildCheckedChangeListener(@IdRes int viewId) { View view = getView(viewId); if (view != null && view instanceof CompoundButton) { ((CompoundButton) view).setOnCheckedChangeListener(this); } } @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mOnRVItemChildTouchListener != null && mRecyclerView != null) { return mOnRVItemChildTouchListener.onRvItemChildTouch(mRecyclerViewHolder, view, motionEvent); } return false; } @Override public boolean onLongClick(View v) { if (mOnItemChildLongClickListener != null) { if (mRecyclerView != null) { return mOnItemChildLongClickListener.onItemChildLongClick(mRecyclerView, v, getPosition()); } else if (mAdapterView != null) { return mOnItemChildLongClickListener.onItemChildLongClick(mAdapterView, v, getPosition()); } } return false; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (mOnItemChildCheckedChangeListener != null) { if (mRecyclerView != null) { BGARecyclerViewAdapter recyclerViewAdapter; RecyclerView.Adapter adapter = mRecyclerView.getAdapter(); if (adapter instanceof BGAHeaderAndFooterAdapter) { recyclerViewAdapter = (BGARecyclerViewAdapter) ((BGAHeaderAndFooterAdapter) adapter) .getInnerAdapter(); } else { recyclerViewAdapter = (BGARecyclerViewAdapter) adapter; } if (!recyclerViewAdapter.isIgnoreCheckedChanged()) { mOnItemChildCheckedChangeListener.onItemChildCheckedChanged(mRecyclerView, buttonView, getPosition(), isChecked); } } else if (mAdapterView != null) { if (!((BGAAdapterViewAdapter) mAdapterView.getAdapter()).isIgnoreCheckedChanged()) { mOnItemChildCheckedChangeListener.onItemChildCheckedChanged(mAdapterView, buttonView, getPosition(), isChecked); } } } } /** * Id?mViewsitem?mViews * * @param viewId * @return */ public <T extends View> T getView(@IdRes int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * ImageViewId?ImageView * * @param viewId * @return */ public ImageView getImageView(@IdRes int viewId) { return getView(viewId); } /** * TextViewId?TextView * * @param viewId * @return */ public TextView getTextView(@IdRes int viewId) { return getView(viewId); } /** * ?item * * @return */ public View getConvertView() { return mConvertView; } public void setObj(Object obj) { mObj = obj; } public Object getObj() { return mObj; } /** * id * * @param viewId * @param text * @return */ public BGAViewHolderHelper setText(@IdRes int viewId, CharSequence text) { if (text == null) { text = ""; } getTextView(viewId).setText(text); return this; } /** * id * * @param viewId * @param stringResId ?id * @return */ public BGAViewHolderHelper setText(@IdRes int viewId, @StringRes int stringResId) { getTextView(viewId).setText(stringResId); return this; } /** * id??? sp * * @param viewId * @param size ??? sp * @return */ public BGAViewHolderHelper setTextSizeSp(@IdRes int viewId, float size) { getTextView(viewId).setTextSize(TypedValue.COMPLEX_UNIT_SP, size); return this; } /** * id? * * @param viewId * @param isBold ? * @return */ public BGAViewHolderHelper setIsBold(@IdRes int viewId, boolean isBold) { getTextView(viewId).getPaint().setTypeface(isBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); return this; } /** * idhtml * * @param viewId * @param source html * @return */ public BGAViewHolderHelper setHtml(@IdRes int viewId, String source) { if (source == null) { source = ""; } getTextView(viewId).setText(Html.fromHtml(source)); return this; } /** * id? * * @param viewId * @param checked * @return */ public BGAViewHolderHelper setChecked(@IdRes int viewId, boolean checked) { Checkable view = getView(viewId); view.setChecked(checked); return this; } public BGAViewHolderHelper setTag(@IdRes int viewId, Object tag) { View view = getView(viewId); view.setTag(tag); return this; } public BGAViewHolderHelper setTag(@IdRes int viewId, int key, Object tag) { View view = getView(viewId); view.setTag(key, tag); return this; } public BGAViewHolderHelper setVisibility(@IdRes int viewId, int visibility) { View view = getView(viewId); view.setVisibility(visibility); return this; } public BGAViewHolderHelper setImageBitmap(@IdRes int viewId, Bitmap bitmap) { ImageView view = getView(viewId); view.setImageBitmap(bitmap); return this; } public BGAViewHolderHelper setImageDrawable(@IdRes int viewId, Drawable drawable) { ImageView view = getView(viewId); view.setImageDrawable(drawable); return this; } /** * @param viewId * @param textColorResId ?id * @return */ public BGAViewHolderHelper setTextColorRes(@IdRes int viewId, @ColorRes int textColorResId) { getTextView(viewId).setTextColor(mContext.getResources().getColor(textColorResId)); return this; } /** * @param viewId * @param textColor * @return */ public BGAViewHolderHelper setTextColor(@IdRes int viewId, int textColor) { getTextView(viewId).setTextColor(textColor); return this; } /** * @param viewId * @param backgroundResId ?id * @return */ public BGAViewHolderHelper setBackgroundRes(@IdRes int viewId, int backgroundResId) { View view = getView(viewId); view.setBackgroundResource(backgroundResId); return this; } /** * @param viewId * @param color * @return */ public BGAViewHolderHelper setBackgroundColor(@IdRes int viewId, int color) { View view = getView(viewId); view.setBackgroundColor(color); return this; } /** * @param viewId * @param colorResId ?id * @return */ public BGAViewHolderHelper setBackgroundColorRes(@IdRes int viewId, @ColorRes int colorResId) { View view = getView(viewId); view.setBackgroundColor(mContext.getResources().getColor(colorResId)); return this; } /** * @param viewId * @param imageResId ??id * @return */ public BGAViewHolderHelper setImageResource(@IdRes int viewId, @DrawableRes int imageResId) { ImageView view = getView(viewId); view.setImageResource(imageResId); return this; } /** * ? * * @param viewId * @param isBold * @return */ public BGAViewHolderHelper setBold(@IdRes int viewId, boolean isBold) { getTextView(viewId).getPaint().setTypeface(isBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); return this; } }