Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create only one instance of ChipsInputEditText per ChipsInput #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions library/src/main/java/com/pchmn/materialchips/ChipsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.pchmn.materialchips.adapter.ChipsAdapter;
Expand Down Expand Up @@ -91,7 +90,7 @@ private void init(AttributeSet attrs) {
ButterKnife.bind(this, rootView);

// attributes
if(attrs != null) {
if (attrs != null) {
TypedArray a = mContext.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ChipsInput,
Expand Down Expand Up @@ -125,14 +124,13 @@ private void init(AttributeSet attrs) {
// filterable list
mFilterableListBackgroundColor = a.getColorStateList(R.styleable.ChipsInput_filterable_list_backgroundColor);
mFilterableListTextColor = a.getColorStateList(R.styleable.ChipsInput_filterable_list_textColor);
}
finally {
} finally {
a.recycle();
}
}

// adapter
mChipsAdapter = new ChipsAdapter(mContext, this, mRecyclerView);
mChipsAdapter = new ChipsAdapter(mContext, this, setupEditText(), mRecyclerView);
ChipsLayoutManager chipsLayoutManager = ChipsLayoutManager.newBuilder(mContext)
.setOrientation(ChipsLayoutManager.HORIZONTAL)
.build();
Expand All @@ -143,7 +141,7 @@ private void init(AttributeSet attrs) {
// set window callback
// will hide DetailedOpenView and hide keyboard on touch outside
Activity activity = ActivityUtil.scanForActivity(mContext);
if(activity == null)
if (activity == null)
throw new ClassCastException("android.view.Context cannot be cast to android.app.Activity");

android.view.Window.Callback mCallBack = (activity).getWindow().getCallback();
Expand Down Expand Up @@ -212,10 +210,14 @@ public ChipView getChipView() {
}

public ChipsInputEditText getEditText() {
return mChipsAdapter.getEditText();
}

private ChipsInputEditText setupEditText() {
ChipsInputEditText editText = new ChipsInputEditText(mContext);
if(mHintColor != null)
if (mHintColor != null)
editText.setHintTextColor(mHintColor);
if(mTextColor != null)
if (mTextColor != null)
editText.setTextColor(mTextColor);

return editText;
Expand All @@ -236,25 +238,25 @@ public void addChipsListener(ChipsListener chipsListener) {
}

public void onChipAdded(ChipInterface chip, int size) {
for(ChipsListener chipsListener: mChipsListenerList) {
for (ChipsListener chipsListener : mChipsListenerList) {
chipsListener.onChipAdded(chip, size);
}
}

public void onChipRemoved(ChipInterface chip, int size) {
for(ChipsListener chipsListener: mChipsListenerList) {
for (ChipsListener chipsListener : mChipsListenerList) {
chipsListener.onChipRemoved(chip, size);
}
}

public void onTextChanged(CharSequence text) {
if(mChipsListener != null) {
for(ChipsListener chipsListener: mChipsListenerList) {
if (mChipsListener != null) {
for (ChipsListener chipsListener : mChipsListenerList) {
chipsListener.onTextChanged(text);
}
// show filterable list
if(mFilterableListView != null) {
if(text.length() > 0)
if (mFilterableListView != null) {
if (text.length() > 0)
mFilterableListView.filterList(text);
else
mFilterableListView.fadeOut();
Expand Down Expand Up @@ -315,15 +317,15 @@ public void setChipBackgroundColor(ColorStateList mBackgroundColor) {
this.mChipBackgroundColor = mBackgroundColor;
}

public boolean isShowChipDetailed() {
return mShowChipDetailed;
}

public ChipsInput setShowChipDetailed(boolean mShowChipDetailed) {
this.mShowChipDetailed = mShowChipDetailed;
return this;
}

public boolean isShowChipDetailed() {
return mShowChipDetailed;
}

public void setChipDetailedTextColor(ColorStateList mChipDetailedTextColor) {
this.mChipDetailedTextColor = mChipDetailedTextColor;
}
Expand All @@ -336,17 +338,17 @@ public void setChipDetailedBackgroundColor(ColorStateList mChipDetailedBackgroun
this.mChipDetailedBackgroundColor = mChipDetailedBackgroundColor;
}

public List<? extends ChipInterface> getFilterableList() {
return mChipList;
}

public void setFilterableList(List<? extends ChipInterface> list) {
mChipList = list;
mFilterableListView = new FilterableListView(mContext);
mFilterableListView.build(mChipList, this, mFilterableListBackgroundColor, mFilterableListTextColor);
mChipsAdapter.setFilterableListView(mFilterableListView);
}

public List<? extends ChipInterface> getFilterableList() {
return mChipList;
}

public ChipValidator getChipValidator() {
return mChipValidator;
}
Expand All @@ -357,7 +359,9 @@ public void setChipValidator(ChipValidator mChipValidator) {

public interface ChipsListener {
void onChipAdded(ChipInterface chip, int newSize);

void onChipRemoved(ChipInterface chip, int newSize);

void onTextChanged(CharSequence text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -18,10 +17,9 @@
import com.pchmn.materialchips.ChipView;
import com.pchmn.materialchips.ChipsInput;
import com.pchmn.materialchips.model.ChipInterface;
import com.pchmn.materialchips.util.ViewUtil;
import com.pchmn.materialchips.views.ChipsInputEditText;
import com.pchmn.materialchips.views.DetailedChipView;
import com.pchmn.materialchips.model.Chip;
import com.pchmn.materialchips.util.ViewUtil;
import com.pchmn.materialchips.views.FilterableListView;

import java.util.ArrayList;
Expand All @@ -41,38 +39,23 @@ public class ChipsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
private ChipsInputEditText mEditText;
private RecyclerView mRecycler;

public ChipsAdapter(Context context, ChipsInput chipsInput, RecyclerView recycler) {
public ChipsAdapter(Context context, ChipsInput chipsInput,
ChipsInputEditText chipsInputEditText, RecyclerView recycler) {
mContext = context;
mChipsInput = chipsInput;
mRecycler = recycler;
mHintLabel = mChipsInput.getHint();
mEditText = mChipsInput.getEditText();
this.mEditText = chipsInputEditText;
initEditText();
}

private class ItemViewHolder extends RecyclerView.ViewHolder {

private final ChipView chipView;

ItemViewHolder(View view) {
super(view);
chipView = (ChipView) view;
}
}

private class EditTextViewHolder extends RecyclerView.ViewHolder {

private final EditText editText;

EditTextViewHolder(View view) {
super(view);
editText = (EditText) view;
}
public ChipsInputEditText getEditText() {
return mEditText;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_EDIT_TEXT)
if (viewType == TYPE_EDIT_TEXT)
return new EditTextViewHolder(mEditText);
else
return new ItemViewHolder(mChipsInput.getChipView());
Expand All @@ -82,15 +65,15 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
// edit text
if(position == mChipList.size()) {
if(mChipList.size() == 0)
if (position == mChipList.size()) {
if (mChipList.size() == 0)
mEditText.setHint(mHintLabel);

// auto fit edit text
autofitEditText();
}
// chip
else if(getItemCount() > 1) {
else if (getItemCount() > 1) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.chipView.inflate(getItem(position));
// handle click
Expand Down Expand Up @@ -137,10 +120,10 @@ private void initEditText() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// backspace
if(event.getAction() == KeyEvent.ACTION_DOWN
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// remove last chip
if(mChipList.size() > 0 && mEditText.getText().toString().length() == 0)
if (mChipList.size() > 0 && mEditText.getText().toString().length() == 0)
removeChip(mChipList.size() - 1);
}
return false;
Expand Down Expand Up @@ -210,7 +193,7 @@ public void onClick(View v) {
});

// show detailed chip
if(mChipsInput.isShowChipDetailed()) {
if (mChipsInput.isShowChipDetailed()) {
chipView.setOnChipClicked(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -248,13 +231,13 @@ private void setDetailedChipViewPosition(DetailedChipView detailedChipView, int[
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

// align left window
if(coord[0] <= 0) {
if (coord[0] <= 0) {
layoutParams.leftMargin = 0;
layoutParams.topMargin = coord[1] - ViewUtil.dpToPx(13);
detailedChipView.alignLeft();
}
// align right
else if(coord[0] + ViewUtil.dpToPx(300) > windowWidth + ViewUtil.dpToPx(13)) {
else if (coord[0] + ViewUtil.dpToPx(300) > windowWidth + ViewUtil.dpToPx(13)) {
layoutParams.leftMargin = windowWidth - ViewUtil.dpToPx(300);
layoutParams.topMargin = coord[1] - ViewUtil.dpToPx(13);
detailedChipView.alignRight();
Expand All @@ -271,12 +254,12 @@ else if(coord[0] + ViewUtil.dpToPx(300) > windowWidth + ViewUtil.dpToPx(13)) {
}

public void setFilterableListView(FilterableListView filterableListView) {
if(mEditText != null)
if (mEditText != null)
mEditText.setFilterableListView(filterableListView);
}

public void addChip(ChipInterface chip) {
if(!listContains(mChipList, chip)) {
if (!listContains(mChipList, chip)) {
mChipList.add(chip);
// notify listener
mChipsInput.onChipAdded(chip, mChipList.size());
Expand Down Expand Up @@ -371,21 +354,40 @@ public List<ChipInterface> getChipList() {

private boolean listContains(List<ChipInterface> contactList, ChipInterface chip) {

if(mChipsInput.getChipValidator() != null) {
for(ChipInterface item: contactList) {
if(mChipsInput.getChipValidator().areEquals(item, chip))
if (mChipsInput.getChipValidator() != null) {
for (ChipInterface item : contactList) {
if (mChipsInput.getChipValidator().areEquals(item, chip))
return true;
}
}
else {
for(ChipInterface item: contactList) {
if(chip.getId() != null && chip.getId().equals(item.getId()))
} else {
for (ChipInterface item : contactList) {
if (chip.getId() != null && chip.getId().equals(item.getId()))
return true;
if(chip.getLabel().equals(item.getLabel()))
if (chip.getLabel().equals(item.getLabel()))
return true;
}
}

return false;
}

private class ItemViewHolder extends RecyclerView.ViewHolder {

private final ChipView chipView;

ItemViewHolder(View view) {
super(view);
chipView = (ChipView) view;
}
}

private class EditTextViewHolder extends RecyclerView.ViewHolder {

private final EditText editText;

EditTextViewHolder(View view) {
super(view);
editText = (EditText) view;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
public class FilterableListView extends RelativeLayout {

private static final String TAG = FilterableListView.class.toString();
private Context mContext;
// list
@BindView(R2.id.recycler_view) RecyclerView mRecyclerView;
@BindView(R2.id.recycler_view)
RecyclerView mRecyclerView;
private Context mContext;
private FilterableAdapter mAdapter;
private List<? extends ChipInterface> mFilterableList;
// others
Expand Down Expand Up @@ -65,7 +66,7 @@ public void build(List<? extends ChipInterface> filterableList, ChipsInput chips
// adapter
mAdapter = new FilterableAdapter(mContext, mRecyclerView, filterableList, chipsInput, backgroundColor, textColor);
mRecyclerView.setAdapter(mAdapter);
if(backgroundColor != null)
if (backgroundColor != null)
mRecyclerView.getBackground().setColorFilter(backgroundColor.getDefaultColor(), PorterDuff.Mode.SRC_ATOP);

// listen to change in the tree
Expand All @@ -85,7 +86,7 @@ public void onGlobalLayout() {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
layoutParams.bottomMargin = ViewUtil.getNavBarHeight(mContext);
}

Expand All @@ -109,7 +110,7 @@ public void filterList(CharSequence text) {
@Override
public void onFilterComplete(int count) {
// show if there are results
if(mAdapter.getItemCount() > 0)
if (mAdapter.getItemCount() > 0)
fadeIn();
else
fadeOut();
Expand All @@ -121,7 +122,7 @@ public void onFilterComplete(int count) {
* Fade in
*/
public void fadeIn() {
if(getVisibility() == VISIBLE)
if (getVisibility() == VISIBLE)
return;

// get visible window (keyboard shown)
Expand All @@ -147,7 +148,7 @@ public void fadeIn() {
* Fade out
*/
public void fadeOut() {
if(getVisibility() == GONE)
if (getVisibility() == GONE)
return;

AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
Expand Down
Loading