Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Level filter view
public class LevelFilterView extends TextView implements View.OnClickListener {
private LevelFilterPopup mFilterPopup;
private OnLevelFilteredListener mOnLevelFilteredListener;
private int mItemBackgroundId;
private String[] mEntries;
public LevelFilterView(Context context) {
super(context);
initialize();
}
public LevelFilterView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LevelFilterView);
mItemBackgroundId = ta.getResourceId(R.styleable.LevelFilterView_itemBackground,
android.R.drawable.btn_default);
ta.recycle();
initialize();
}
public LevelFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
private void initialize() {
setGravity(Gravity.CENTER);
setClickable(true);
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
setOnClickListener(this);
}
public void setEntries(String[] entries) {
mEntries = entries;
reset();
}
public void reset() {
if (mEntries != null) {
setText(mEntries[0]);
}
if (mFilterPopup != null) {
mFilterPopup.reset();
}
}
@Override
public void onClick(View v) {
if (mFilterPopup == null) {
mFilterPopup = new LevelFilterPopup(getContext(), mItemBackgroundId);
mFilterPopup.setDataSet(mEntries);
mFilterPopup.setOnLevelFilteredListener(new OnLevelFilteredListener() {
@Override
public void onFiltered(String level, String levelText) {
setText(levelText);
if (mOnLevelFilteredListener != null) {
mOnLevelFilteredListener.onFiltered(level, levelText);
}
mFilterPopup.dismiss();
}
});
}
mFilterPopup.showAsDropDown(this, 0, 20);
}
public void setOnLevelFilteredListener(OnLevelFilteredListener listener) {
mOnLevelFilteredListener = listener;
}
public interface OnLevelFilteredListener {
/**
* @param level null if the position is 0,otherwise index value
* @param levelText the level text for the index
*/
void onFiltered(String level, String levelText);
}
private static class LevelFilterPopup extends PopupWindow {
private RecyclerView mRecyclerView;
private LevelFilterAdapter mFilterAdapter;
private OnLevelFilteredListener mOnLevelFilteredListener;
private String[] mDataSet;
private int mItemBackgroundResId;
public LevelFilterPopup(Context context, int itemBackgroundResId) {
super(context);
mItemBackgroundResId = itemBackgroundResId;
mRecyclerView = new RecyclerView(context);
GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(10, 10, 10, 10);
}
});
mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
mFilterAdapter = new LevelFilterAdapter();
mRecyclerView.setAdapter(mFilterAdapter);
mRecyclerView.setBackgroundResource(R.drawable.shape_level_select_bg);
setContentView(mRecyclerView);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setTouchable(true);
setFocusable(true);
setOutsideTouchable(true);
setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent));
}
public void setDataSet(String[] dataSet) {
mDataSet = dataSet;
}
public void reset() {
mFilterAdapter.notifyDataSetChangedAtPosition(0);
}
public void setOnLevelFilteredListener(OnLevelFilteredListener onLevelFilteredListener) {
this.mOnLevelFilteredListener = onLevelFilteredListener;
}
private class LevelFilterAdapter extends RecyclerView.Adapter<LevelFilterAdapter.ViewHolder>
implements OnClickListener {
private int mCheckedPosition = 0;
public LevelFilterAdapter() {
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(new CheckBox(parent.getContext()));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(mDataSet[position]);
holder.text.setBackgroundResource(mItemBackgroundResId);
holder.text.setTag(position);
holder.text.setOnClickListener(this);
holder.text.setChecked(position == mCheckedPosition);
}
@Override
public int getItemCount() {
return mDataSet == null ? 0 : mDataSet.length;
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
if (mOnLevelFilteredListener != null) {
mOnLevelFilteredListener.onFiltered(position == 0 ? null : String.valueOf(position),
((TextView) v).getText().toString());
}
notifyDataSetChangedAtPosition(position);
}
void notifyDataSetChangedAtPosition(int position) {
mCheckedPosition = position;
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder {
public CheckBox text;
public ViewHolder(View itemView) {
super(itemView);
text = (CheckBox) itemView;
text.setButtonDrawable(android.R.color.transparent);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
// Gravity not working properly cause of the width or height not specific defined?
text.setGravity(Gravity.CENTER);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.