open-keychain/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/views/FilterableListView.java
2018-07-04 13:59:21 +02:00

157 lines
5.1 KiB
Java

package com.pchmn.materialchips.views;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.widget.Filter;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.pchmn.materialchips.ChipsInput;
import com.pchmn.materialchips.R;
import com.pchmn.materialchips.adapter.FilterableAdapter;
import com.pchmn.materialchips.model.ChipInterface;
import com.pchmn.materialchips.util.ViewUtil;
import java.util.List;
public class FilterableListView extends RelativeLayout {
private static final String TAG = FilterableListView.class.toString();
private FrameLayout frameLayout;
private Context mContext;
// list
private RecyclerView mRecyclerView;
private FilterableAdapter mAdapter;
private List<? extends ChipInterface> mFilterableList;
// others
private ChipsInput mChipsInput;
private ViewGroup rootView;
public FilterableListView(Context context) {
this(context, null);
}
public FilterableListView(Context context, ViewGroup layout) {
super(context);
this.mContext = context;
this.rootView = layout;
init();
}
private void init() {
// inflate layout
View view = inflate(getContext(), R.layout.list_filterable_view, this);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
// recycler
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
// hide on first
setVisibility(GONE);
}
public void build(List<? extends ChipInterface> filterableList, ChipsInput chipsInput, ColorStateList backgroundColor, ColorStateList textColor) {
mFilterableList = filterableList;
mChipsInput = chipsInput;
// adapter
mAdapter = new FilterableAdapter(mContext, mRecyclerView, filterableList, chipsInput, backgroundColor, textColor);
mRecyclerView.setAdapter(mAdapter);
if(backgroundColor != null)
mRecyclerView.getBackground().setColorFilter(backgroundColor.getDefaultColor(), PorterDuff.Mode.SRC_ATOP);
// listen to change in the tree
mChipsInput.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// position
if(rootView == null){
rootView = (ViewGroup) mChipsInput.getRootView();
}
// size
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
ViewUtil.getWindowWidth(mContext),
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
layoutParams.bottomMargin = ViewUtil.getNavBarHeight(mContext);
}
//If this child view is already added to the parent rootView, then remove it first
ViewGroup parent = (ViewGroup) FilterableListView.this.getParent();
if (parent != null) {
parent.removeView(FilterableListView.this);
}
// add view
rootView.addView(FilterableListView.this, layoutParams);
// remove the listener:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mChipsInput.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mChipsInput.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
public void filterList(CharSequence text) {
mAdapter.getFilter().filter(text, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
// show if there are results
if(mAdapter.getItemCount() > 0)
fadeIn();
else
fadeOut();
}
});
}
/**
* Fade in
*/
public void fadeIn() {
if(getVisibility() == VISIBLE)
return;
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(200);
startAnimation(anim);
setVisibility(VISIBLE);
}
/**
* Fade out
*/
public void fadeOut() {
if(getVisibility() == GONE)
return;
AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(200);
startAnimation(anim);
setVisibility(GONE);
}
}