From bea803f61ea578100f3dc35af978042650050638 Mon Sep 17 00:00:00 2001 From: Reza Amuzadeh Date: Sun, 10 Mar 2019 16:04:16 +0330 Subject: [PATCH] added Custom text color + text size --- build.gradle | 4 +- .../common/view/breadcrumbs/MainActivity.java | 4 +- demo/src/main/res/layout/activity_main.xml | 54 +-- demo/src/main/res/values/colors.xml | 2 + .../view/breadcrumbs/BreadcrumbsAdapter.java | 401 ++++++++++-------- .../view/breadcrumbs/BreadcrumbsView.java | 375 ++++++++-------- .../layout-v21/breadcrumbs_view_item_text.xml | 1 - .../res/layout/breadcrumbs_view_item_text.xml | 3 +- library/src/main/res/values/attrs.xml | 9 +- 9 files changed, 452 insertions(+), 401 deletions(-) diff --git a/build.gradle b/build.gradle index c738373..25708c2 100644 --- a/build.gradle +++ b/build.gradle @@ -3,8 +3,8 @@ ext { minSdkVersion = 17 targetSdkVersion = 28 - versionCode = 7 - versionName = "0.2.2" + versionCode = 8 + versionName = "0.2.3" } buildscript { diff --git a/demo/src/main/java/moe/feng/common/view/breadcrumbs/MainActivity.java b/demo/src/main/java/moe/feng/common/view/breadcrumbs/MainActivity.java index fd744ca..55211ff 100644 --- a/demo/src/main/java/moe/feng/common/view/breadcrumbs/MainActivity.java +++ b/demo/src/main/java/moe/feng/common/view/breadcrumbs/MainActivity.java @@ -39,9 +39,7 @@ protected void onCreate(Bundle savedInstanceState) { mBreadcrumbsView = findViewById(R.id.breadcrumbs_view); mBreadcrumbsView.setItems(new ArrayList<>(Arrays.asList( - BreadcrumbItem.createSimpleItem("Root Path"), - createItem("سلام 1"), - createItem("سلام 2") + BreadcrumbItem.createSimpleItem("Root Path") ))); mBreadcrumbsView.setCallback(new DefaultBreadcrumbsCallback() { @Override diff --git a/demo/src/main/res/layout/activity_main.xml b/demo/src/main/res/layout/activity_main.xml index 6fdb022..19770c1 100644 --- a/demo/src/main/res/layout/activity_main.xml +++ b/demo/src/main/res/layout/activity_main.xml @@ -1,34 +1,36 @@ - + - + - + - + - + - + diff --git a/demo/src/main/res/values/colors.xml b/demo/src/main/res/values/colors.xml index 9865995..31132d7 100644 --- a/demo/src/main/res/values/colors.xml +++ b/demo/src/main/res/values/colors.xml @@ -3,4 +3,6 @@ #3F51B5 #303F9F #FF4081 + #FFFFFF + #777475 diff --git a/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsAdapter.java b/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsAdapter.java index c91f695..c7e6434 100644 --- a/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsAdapter.java +++ b/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsAdapter.java @@ -2,211 +2,250 @@ import android.content.Context; import android.graphics.drawable.Drawable; +import android.os.Build; import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v7.widget.RecyclerView; -import android.view.*; -import android.widget.*; -import moe.feng.common.view.breadcrumbs.model.IBreadcrumbItem; +import android.util.TypedValue; +import android.view.ContextThemeWrapper; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.widget.AdapterView; +import android.widget.ImageButton; +import android.widget.ListAdapter; +import android.widget.ListPopupWindow; +import android.widget.SimpleAdapter; +import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import moe.feng.common.view.breadcrumbs.model.IBreadcrumbItem; + class BreadcrumbsAdapter extends RecyclerView.Adapter { - private final int DROPDOWN_OFFSET_Y_FIX; + private final int DROPDOWN_OFFSET_Y_FIX; + + private List items = new ArrayList<>(); + private BreadcrumbsCallback callback; + + private BreadcrumbsView parent; - private List items = new ArrayList<>(); - private BreadcrumbsCallback callback; + private int mPopupThemeId = -1; + private int mTextColorSelected = -1; + private int mtextColorUnSelected = -1; + private int mTextSize = -1; - private BreadcrumbsView parent; + public BreadcrumbsAdapter(BreadcrumbsView parent) { + this(parent, new ArrayList()); + } - private int mPopupThemeId = -1; + public BreadcrumbsAdapter(BreadcrumbsView parent, int mTextColorSelected, int mtextColorUnSelected, int mTextSize) { + this(parent, new ArrayList()); + this.mTextColorSelected = mTextColorSelected; + this.mtextColorUnSelected = mtextColorUnSelected; + this.mTextSize = mTextSize; + } - public BreadcrumbsAdapter(BreadcrumbsView parent) { - this(parent, new ArrayList()); - } + public BreadcrumbsAdapter(BreadcrumbsView parent, ArrayList items) { + this.parent = parent; + this.items = items; + DROPDOWN_OFFSET_Y_FIX = parent.getResources().getDimensionPixelOffset(R.dimen.dropdown_offset_y_fix_value); + } - public BreadcrumbsAdapter(BreadcrumbsView parent, ArrayList items) { - this.parent = parent; - this.items = items; - DROPDOWN_OFFSET_Y_FIX = parent.getResources().getDimensionPixelOffset(R.dimen.dropdown_offset_y_fix_value); - } + public @NonNull + List getItems() { + return (List) this.items; + } - public @NonNull List getItems() { - return (List) this.items; - } + public void setItems(@NonNull List items) { + this.items = (List) items; + } - public void setItems(@NonNull List items) { - this.items = (List) items; - } + public void setCallback(@Nullable BreadcrumbsCallback callback) { + this.callback = callback; + } - public void setCallback(@Nullable BreadcrumbsCallback callback) { - this.callback = callback; - } + public @Nullable + BreadcrumbsCallback getCallback() { + return this.callback; + } - public @Nullable BreadcrumbsCallback getCallback() { - return this.callback; - } + public void setPopupThemeId(@IdRes int popupThemeId) { + this.mPopupThemeId = popupThemeId; + } - public void setPopupThemeId(@IdRes int popupThemeId) { - this.mPopupThemeId = popupThemeId; - } + @NonNull + @Override + public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + if (viewType == R.layout.breadcrumbs_view_item_arrow) { + return new ArrowIconHolder(inflater.inflate(viewType, parent, false)); + } else if (viewType == R.layout.breadcrumbs_view_item_text) { + return new BreadcrumbItemHolder(inflater.inflate(viewType, parent, false)); + } else { + throw new IllegalArgumentException("Unknown view type:" + viewType); + } + } + + @Override + public void onBindViewHolder(@NonNull ItemHolder holder, int position) { + int viewType = getItemViewType(position); + int truePos = viewType == R.layout.breadcrumbs_view_item_arrow ? ((position - 1) / 2) + 1 : position / 2; + holder.setItem(items.get(truePos)); + } + + @Override + public int getItemCount() { + return (items != null && !items.isEmpty()) ? (items.size() * 2 - 1) : 0; + } - @NonNull @Override - public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - LayoutInflater inflater = LayoutInflater.from(parent.getContext()); - if (viewType == R.layout.breadcrumbs_view_item_arrow) { - return new ArrowIconHolder(inflater.inflate(viewType, parent, false)); - } else if (viewType == R.layout.breadcrumbs_view_item_text) { - return new BreadcrumbItemHolder(inflater.inflate(viewType, parent, false)); - } else { - throw new IllegalArgumentException("Unknown view type:" + viewType); - } - } - - @Override - public void onBindViewHolder(@NonNull ItemHolder holder, int position) { - int viewType = getItemViewType(position); - int truePos = viewType == R.layout.breadcrumbs_view_item_arrow ? ((position - 1) / 2) + 1 : position / 2; - holder.setItem(items.get(truePos)); - } - - @Override - public int getItemCount() { - return (items != null && !items.isEmpty()) ? (items.size() * 2 - 1) : 0; - } - - @Override - public int getItemViewType(int position) { - return position % 2 == 1 ? R.layout.breadcrumbs_view_item_arrow : R.layout.breadcrumbs_view_item_text; - } - - class BreadcrumbItemHolder extends ItemHolder { - - TextView button; - - BreadcrumbItemHolder(View itemView) { - super(itemView); - button = (TextView) itemView; - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (callback != null) { - callback.onItemClick(parent, getAdapterPosition() / 2); - } - } - }); - } - - @Override - public void setItem(@NonNull IBreadcrumbItem item) { - super.setItem(item); - button.setText(item.getSelectedItem().toString()); - button.setTextColor( - ViewUtils.getColorFromAttr(getContext(), - getAdapterPosition() == getItemCount() - 1 - ? android.R.attr.textColorPrimary : android.R.attr.textColorSecondary) - ); - } - - } - - class ArrowIconHolder extends ItemHolder { - - ImageButton imageButton; - ListPopupWindow popupWindow; - - ArrowIconHolder(View itemView) { - super(itemView); - Drawable normalDrawable = getContext().getResources().getDrawable(R.drawable.ic_chevron_right_black_24dp); - Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable); - DrawableCompat.setTint(wrapDrawable, ViewUtils.getColorFromAttr(getContext(), android.R.attr.textColorSecondary)); - imageButton = (ImageButton) itemView; - imageButton.setImageDrawable(wrapDrawable); - imageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (item.hasMoreSelect()) { - try { - popupWindow.show(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - }); - createPopupWindow(); - } - - @Override - public void setItem(@NonNull IBreadcrumbItem item) { - super.setItem(item); - imageButton.setClickable(item.hasMoreSelect()); - if (item.hasMoreSelect()) { - List> list = new ArrayList<>(); - for (Object obj : item.getItems()) { - Map map = new HashMap<>(); - map.put("text", obj.toString()); - list.add(map); - } - ListAdapter adapter = new SimpleAdapter(getPopupThemedContext(), list, R.layout.breadcrumbs_view_dropdown_item, new String[] {"text"}, new int[] {android.R.id.text1}); - popupWindow.setAdapter(adapter); - popupWindow.setWidth(ViewUtils.measureContentWidth(getPopupThemedContext(), adapter)); - imageButton.setOnTouchListener(popupWindow.createDragToOpenListener(imageButton)); - } else { - imageButton.setOnTouchListener(null); - } - } - - private void createPopupWindow() { - popupWindow = new ListPopupWindow(getPopupThemedContext()); - popupWindow.setAnchorView(imageButton); - popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView adapterView, View view, int i, long l) { - if (callback != null) { - callback.onItemChange(parent, getAdapterPosition() / 2, getItems().get(getAdapterPosition() / 2 + 1).getItems().get(i)); - popupWindow.dismiss(); - } - } - }); - imageButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - popupWindow.setVerticalOffset(-imageButton.getMeasuredHeight() + DROPDOWN_OFFSET_Y_FIX); - imageButton.getViewTreeObserver().removeOnGlobalLayoutListener(this); - } - }); - } - - } - - class ItemHolder extends RecyclerView.ViewHolder { - - T item; - - ItemHolder(View itemView) { - super(itemView); - } - - public void setItem(@NonNull T item) { - this.item = item; - } - - Context getContext() { - return itemView.getContext(); - } - - Context getPopupThemedContext() { - return mPopupThemeId != -1 ? new ContextThemeWrapper(getContext(), mPopupThemeId) : getContext(); - } - - } + public int getItemViewType(int position) { + return position % 2 == 1 ? R.layout.breadcrumbs_view_item_arrow : R.layout.breadcrumbs_view_item_text; + } + + class BreadcrumbItemHolder extends ItemHolder { + + TextView button; + + BreadcrumbItemHolder(View itemView) { + super(itemView); + button = (TextView) itemView; + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (callback != null) { + callback.onItemClick(parent, getAdapterPosition() / 2); + } + } + }); + } + + @Override + public void setItem(@NonNull IBreadcrumbItem item) { + super.setItem(item); + button.setText(item.getSelectedItem().toString()); + button.setTextColor(ViewUtils.getColorFromAttr(getContext(), getAdapterPosition() == getItemCount() - 1 ? android.R.attr.textColorPrimary : android.R.attr.textColorSecondary)); + + if (mTextColorSelected != -1) + button.setTextColor(ViewUtils.getColorFromAttr(getContext(), + getAdapterPosition() == getItemCount() - 1 + ? mTextColorSelected : mtextColorUnSelected)); + else + button.setTextColor(ViewUtils.getColorFromAttr(getContext(), + getAdapterPosition() == getItemCount() - 1 + ? android.R.attr.textColorPrimary : android.R.attr.textColorSecondary) + ); + + if (mTextSize != -1) + button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); + } + + public float convertSpToPixels(float sp, Context context) { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); + } + + } + + class ArrowIconHolder extends ItemHolder { + + ImageButton imageButton; + ListPopupWindow popupWindow; + + ArrowIconHolder(View itemView) { + super(itemView); + Drawable normalDrawable = getContext().getResources().getDrawable(R.drawable.ic_chevron_right_black_24dp); + Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable); + DrawableCompat.setTint(wrapDrawable, ViewUtils.getColorFromAttr(getContext(), android.R.attr.textColorSecondary)); + imageButton = (ImageButton) itemView; + imageButton.setImageDrawable(wrapDrawable); + imageButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (item.hasMoreSelect()) { + try { + popupWindow.show(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }); + createPopupWindow(); + } + + @RequiresApi(api = Build.VERSION_CODES.KITKAT) + @Override + public void setItem(@NonNull IBreadcrumbItem item) { + super.setItem(item); + imageButton.setClickable(item.hasMoreSelect()); + if (item.hasMoreSelect()) { + List> list = new ArrayList<>(); + for (Object obj : item.getItems()) { + Map map = new HashMap<>(); + map.put("text", obj.toString()); + list.add(map); + } + ListAdapter adapter = new SimpleAdapter(getPopupThemedContext(), list, R.layout.breadcrumbs_view_dropdown_item, new String[]{"text"}, new int[]{android.R.id.text1}); + popupWindow.setAdapter(adapter); + popupWindow.setWidth(ViewUtils.measureContentWidth(getPopupThemedContext(), adapter)); + imageButton.setOnTouchListener(popupWindow.createDragToOpenListener(imageButton)); + } else { + imageButton.setOnTouchListener(null); + } + } + + private void createPopupWindow() { + popupWindow = new ListPopupWindow(getPopupThemedContext()); + popupWindow.setAnchorView(imageButton); + popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + if (callback != null) { + callback.onItemChange(parent, getAdapterPosition() / 2, getItems().get(getAdapterPosition() / 2 + 1).getItems().get(i)); + popupWindow.dismiss(); + } + } + }); + imageButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + popupWindow.setVerticalOffset(-imageButton.getMeasuredHeight() + DROPDOWN_OFFSET_Y_FIX); + imageButton.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + } + + } + + class ItemHolder extends RecyclerView.ViewHolder { + + T item; + + ItemHolder(View itemView) { + super(itemView); + } + + public void setItem(@NonNull T item) { + this.item = item; + } + + Context getContext() { + return itemView.getContext(); + } + + Context getPopupThemedContext() { + return mPopupThemeId != -1 ? new ContextThemeWrapper(getContext(), mPopupThemeId) : getContext(); + } + + } } diff --git a/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsView.java b/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsView.java index 9a502a5..76643f3 100644 --- a/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsView.java +++ b/library/src/main/java/moe/feng/common/view/breadcrumbs/BreadcrumbsView.java @@ -12,207 +12,216 @@ import android.view.ViewGroup; import android.widget.FrameLayout; -import moe.feng.common.view.breadcrumbs.model.IBreadcrumbItem; - import java.util.ArrayList; import java.util.List; +import moe.feng.common.view.breadcrumbs.model.IBreadcrumbItem; + public class BreadcrumbsView extends FrameLayout { - /** - * Internal implement of BreadcrumbsView - */ - private RecyclerView mRecyclerView; - private BreadcrumbsAdapter mAdapter; + /** + * Internal implement of BreadcrumbsView + */ + private RecyclerView mRecyclerView; + private BreadcrumbsAdapter mAdapter; - /** - * Popup Menu Theme Id - */ - private int mPopupThemeId = -1; + /** + * Popup Menu Theme Id + */ + private int mPopupThemeId = -1; + private int mTextColorSelected = -1; + private int mtextColorUnSelected = -1; + private int mTextSizeCustom = -1; private static final String KEY_SUPER_STATES = BuildConfig.APPLICATION_ID + ".superStates"; private static final String KEY_BREADCRUMBS = BuildConfig.APPLICATION_ID + ".breadcrumbs"; - public BreadcrumbsView(Context context) { - this(context, null); - } - - public BreadcrumbsView(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } - - public BreadcrumbsView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - - if (attrs != null) { - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BreadcrumbsView, defStyleAttr, 0); - mPopupThemeId = a.getResourceId(R.styleable.BreadcrumbsView_popupTheme, -1); - a.recycle(); - } - - init(); - } - - /** - * Init BreadcrumbsView - */ - private void init() { - // Init RecyclerView - if (mRecyclerView == null) { - ViewGroup.LayoutParams rvLayoutParams = new ViewGroup.LayoutParams(-1, -1); - rvLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; - mRecyclerView = new RecyclerView(getContext()); - - // Create Horizontal LinearLayoutManager - LinearLayoutManager layoutManager = new LinearLayoutManager( - getContext(), LinearLayoutManager.HORIZONTAL, ViewUtils.isRtlLayout(getContext())); - mRecyclerView.setLayoutManager(layoutManager); - mRecyclerView.setOverScrollMode(OVER_SCROLL_NEVER); - - // Add RecyclerView - addView(mRecyclerView, rvLayoutParams); - } - // Init Adapter - if (mAdapter == null) { - mAdapter = new BreadcrumbsAdapter(this); - if (mPopupThemeId != -1) { - mAdapter.setPopupThemeId(mPopupThemeId); - } - } - mRecyclerView.setAdapter(mAdapter); - } - - /** - * Get breadcrumb items list - * - * @return Breadcrumb Items - */ - public @NonNull List getItems() { - return mAdapter.getItems(); - } - - /** - * Get current breadcrumb item - * - * @return Current item - */ - public @NonNull E getCurrentItem() { - return mAdapter.getItems().get(mAdapter.getItems().size() - 1); - } - - /** - * Set breadcrumb items list - * - * @param items Target list - */ - public void setItems(@NonNull List items) { - mAdapter.setItems(items); - mAdapter.notifyDataSetChanged(); - postDelayed(new Runnable() { - @Override - public void run() { - mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1); - } - }, 500); - } - - /** - * Notify which item has been changed to update text view - * - * @param index The item position - */ - public void notifyItemChanged(int index) { - mAdapter.notifyItemChanged(index * 2); - } - - /** - * Add a new item - * - * @param item New item - */ - public void addItem(@NonNull E item) { - int oldSize = mAdapter.getItemCount(); - mAdapter.getItems().add(item); - mAdapter.notifyItemRangeInserted(oldSize, 2); - mAdapter.notifyItemChanged(oldSize - 1); - postDelayed(new Runnable() { - @Override - public void run() { - mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1); - } - }, 500); - } - - /** - * Remove items after a position - * - * @param afterPos The first position of the removing range - */ - public void removeItemAfter(final int afterPos) { - if (afterPos <= mAdapter.getItems().size() - 1) { - int oldSize = mAdapter.getItemCount(); - while (mAdapter.getItems().size() > afterPos) { - mAdapter.getItems().remove(mAdapter.getItems().size() - 1); - } - mAdapter.notifyItemRangeRemoved(afterPos * 2 - 1, oldSize - afterPos); - /* Add delay time to fix animation */ - postDelayed(new Runnable() { - @Override - public void run() { - int currentPos = afterPos * 2 - 1 - 1; - mAdapter.notifyItemChanged(currentPos); - mRecyclerView.smoothScrollToPosition(currentPos); - } - }, 100); - } - } - - /** - * Remove last item - */ - public void removeLastItem() { - removeItemAfter(mAdapter.getItems().size() - 1); - } - - /** - * Set BreadcrumbsView callback (Recommend to use DefaultBreadcrumbsCallback) - * - * @param callback Callback should be set - * @see BreadcrumbsCallback - * @see DefaultBreadcrumbsCallback - */ - public void setCallback(@Nullable BreadcrumbsCallback callback) { - mAdapter.setCallback(callback); - } - - /** - * Get callback - * - * @return Callback - * @see BreadcrumbsCallback - */ - public @Nullable BreadcrumbsCallback getCallback() { - return mAdapter.getCallback(); - } - - // Save/Restore View Instance State - @Override - public Parcelable onSaveInstanceState() { + public BreadcrumbsView(Context context) { + this(context, null); + } + + public BreadcrumbsView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public BreadcrumbsView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + if (attrs != null) { + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BreadcrumbsView, defStyleAttr, 0); + mPopupThemeId = a.getResourceId(R.styleable.BreadcrumbsView_popupTheme, -1); + mTextColorSelected = a.getColor(R.styleable.BreadcrumbsView_textColorSelected, -1); + mtextColorUnSelected = a.getColor(R.styleable.BreadcrumbsView_textColorUnSelected, -1); + mTextSizeCustom = a.getDimensionPixelSize(R.styleable.BreadcrumbsView_textSizeCustom, -1); + a.recycle(); + } + + init(); + } + + /** + * Init BreadcrumbsView + */ + private void init() { + // Init RecyclerView + if (mRecyclerView == null) { + ViewGroup.LayoutParams rvLayoutParams = new ViewGroup.LayoutParams(-1, -1); + rvLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; + mRecyclerView = new RecyclerView(getContext()); + + // Create Horizontal LinearLayoutManager + LinearLayoutManager layoutManager = new LinearLayoutManager( + getContext(), LinearLayoutManager.HORIZONTAL, ViewUtils.isRtlLayout(getContext())); + mRecyclerView.setLayoutManager(layoutManager); + mRecyclerView.setOverScrollMode(OVER_SCROLL_NEVER); + + // Add RecyclerView + addView(mRecyclerView, rvLayoutParams); + } + // Init Adapter + if (mAdapter == null) { + mAdapter = new BreadcrumbsAdapter(this, mTextColorSelected, mtextColorUnSelected, mTextSizeCustom); + if (mPopupThemeId != -1) { + mAdapter.setPopupThemeId(mPopupThemeId); + } + } + mRecyclerView.setAdapter(mAdapter); + } + + /** + * Get breadcrumb items list + * + * @return Breadcrumb Items + */ + public @NonNull + List getItems() { + return mAdapter.getItems(); + } + + /** + * Get current breadcrumb item + * + * @return Current item + */ + public @NonNull + E getCurrentItem() { + return mAdapter.getItems().get(mAdapter.getItems().size() - 1); + } + + /** + * Set breadcrumb items list + * + * @param items Target list + */ + public void setItems(@NonNull List items) { + mAdapter.setItems(items); + mAdapter.notifyDataSetChanged(); + postDelayed(new Runnable() { + @Override + public void run() { + mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1); + } + }, 500); + } + + /** + * Notify which item has been changed to update text view + * + * @param index The item position + */ + public void notifyItemChanged(int index) { + mAdapter.notifyItemChanged(index * 2); + } + + /** + * Add a new item + * + * @param item New item + */ + public void addItem(@NonNull E item) { + int oldSize = mAdapter.getItemCount(); + mAdapter.getItems().add(item); + mAdapter.notifyItemRangeInserted(oldSize, 2); + mAdapter.notifyItemChanged(oldSize - 1); + postDelayed(new Runnable() { + @Override + public void run() { + mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1); + } + }, 500); + } + + /** + * Remove items after a position + * + * @param afterPos The first position of the removing range + */ + public void removeItemAfter(final int afterPos) { + if (afterPos <= mAdapter.getItems().size() - 1) { + int oldSize = mAdapter.getItemCount(); + while (mAdapter.getItems().size() > afterPos) { + mAdapter.getItems().remove(mAdapter.getItems().size() - 1); + } + mAdapter.notifyItemRangeRemoved(afterPos * 2 - 1, oldSize - afterPos); + /* Add delay time to fix animation */ + postDelayed(new Runnable() { + @Override + public void run() { + int currentPos = afterPos * 2 - 1 - 1; + mAdapter.notifyItemChanged(currentPos); + mRecyclerView.smoothScrollToPosition(currentPos); + } + }, 100); + } + } + + /** + * Remove last item + */ + public void removeLastItem() { + removeItemAfter(mAdapter.getItems().size() - 1); + } + + /** + * Set BreadcrumbsView callback (Recommend to use DefaultBreadcrumbsCallback) + * + * @param callback Callback should be set + * @see BreadcrumbsCallback + * @see DefaultBreadcrumbsCallback + */ + public void setCallback(@Nullable BreadcrumbsCallback callback) { + mAdapter.setCallback(callback); + } + + /** + * Get callback + * + * @return Callback + * @see BreadcrumbsCallback + */ + public @Nullable + BreadcrumbsCallback getCallback() { + return mAdapter.getCallback(); + } + + // Save/Restore View Instance State + @Override + public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(KEY_SUPER_STATES, super.onSaveInstanceState()); bundle.putParcelableArrayList(KEY_BREADCRUMBS, new ArrayList<>(mAdapter.getItems())); return bundle; - } + } - @Override - public void onRestoreInstanceState(Parcelable state) { - if (state instanceof Bundle) { + @Override + public void onRestoreInstanceState(Parcelable state) { + if (state instanceof Bundle) { Bundle bundle = (Bundle) state; super.onRestoreInstanceState(bundle.getParcelable(KEY_SUPER_STATES)); setItems(bundle.getParcelableArrayList(KEY_BREADCRUMBS)); return; - } - super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); - } + } + super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE); + } } diff --git a/library/src/main/res/layout-v21/breadcrumbs_view_item_text.xml b/library/src/main/res/layout-v21/breadcrumbs_view_item_text.xml index fac0155..f2dd219 100644 --- a/library/src/main/res/layout-v21/breadcrumbs_view_item_text.xml +++ b/library/src/main/res/layout-v21/breadcrumbs_view_item_text.xml @@ -10,5 +10,4 @@ android:gravity="center_vertical" android:padding="16dp" android:textAppearance="?android:attr/textAppearanceMedium" - android:textColor="?android:attr/textColorPrimary" tools:text="Example" /> \ No newline at end of file diff --git a/library/src/main/res/layout/breadcrumbs_view_item_text.xml b/library/src/main/res/layout/breadcrumbs_view_item_text.xml index 64ff325..637ae8a 100644 --- a/library/src/main/res/layout/breadcrumbs_view_item_text.xml +++ b/library/src/main/res/layout/breadcrumbs_view_item_text.xml @@ -8,7 +8,6 @@ android:focusable="true" android:fontFamily="@font/app_font" android:gravity="center_vertical" - android:padding="16dp" + android:padding="8dp" android:textAppearance="?android:attr/textAppearanceMedium" - android:textColor="?android:attr/textColorPrimary" tools:text="Example" /> \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 77f945e..30da8c2 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,8 +1,11 @@ - - - + + + + + + \ No newline at end of file