公司项目用到了搜索中的筛选,常规View,开发后整理了一下
- 综合排序 RingView
- 全部价格 PriceView
- 全部时间 TimeView
- 筛选领域 ClassifyView
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e6802606b1548a9048f07cfeba5c75ee.gif)
涉及主要的类
public class RinkingView extends FrameLayout {
private String selectType = "param9";//默认
private RinkBean temp;
private LinearLayout bottom;
private ConstraintLayout container;
private RecyclerView mRecyclerView;
private RinkAdapter rinkAdapter;
private List<RinkBean> rinkBeanList = new ArrayList<>();
private Context mContext;
private String[] zhibo_sorts = new String[]{"综合排序", "热门", "最新"};
private String[] circle_sorts = new String[]{"综合排序", "成员最多", "主题最多"};
private String[] zhuti_sorts = new String[]{"综合排序", "热门", "最新"};
private String[] article_sorts = new String[]{"综合排序", "热门", "最新"};
private String[] wenda_sorts = new String[]{"综合排序", "最新回答", "最多Get"};
private String[] dazhu_sorts = new String[]{"综合排序", "热门排序", "回答最多", "被 Get 最多", "粉丝最多"};
private View view;
public RinkingView(Context context) {
super(context);
}
public RinkingView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
view = inflate(getContext(), R.layout.rink_view, this);
initView();
}
public void setVisibility(int visibility, RinkBean temp, String selectType) {
super.setVisibility(visibility);
this.temp = temp;
this.selectType = selectType;
initData();
}
private void initView() {
initLayout();
initData();
initEvent();
}
private void initEvent() {
container.setOnClickListener(view -> {
viewGone(temp);
});
bottom.setOnClickListener(view -> {
viewGone(temp);
});
}
//点击tab时传入type,加载 不同数据
private void initData() {
rinkBeanList.clear();
if (!TextUtils.isEmpty(selectType)) {
switch (selectType) {
case ZHITI://主题
rinkBeanList = initZhuTiData();
break;
case ZHIKA://答主
rinkBeanList = initDaZhuData();
break;
case QUESTION://问答
rinkBeanList = initWenDaData();
break;
case ARTICLE://文章
rinkBeanList = initArticleData();
break;
case CIRCLE://圈子
rinkBeanList = initCircleData();
break;
case COLUMN_LIVE://直播
rinkBeanList = initZhiBoData();
break;
default:
}
}
rinkAdapter.setNewData(rinkBeanList);
}
private List<RinkBean> initZhuTiData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < zhuti_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(zhuti_sorts[i]);
if (null != temp) {
if (temp.getName().equals(zhuti_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
//如果temp为null,则默认取数据的第一条为选中
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initDaZhuData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < dazhu_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(dazhu_sorts[i]);
if (null != temp) {
if (temp.getName().equals(dazhu_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
//如果temp为null,则默认取数据的第一条为选中
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initWenDaData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < wenda_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(wenda_sorts[i]);
if (null != temp) {
if (temp.getName().equals(wenda_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initCircleData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < circle_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(circle_sorts[i]);
if (null != temp) {
if (temp.getName().equals(circle_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initZhiBoData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < zhibo_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(zhibo_sorts[i]);
if (null != temp) {
if (temp.getName().equals(zhibo_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initArticleData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < article_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(article_sorts[i]);
if (null != temp) {
if (temp.getName().equals(article_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private void initLayout() {
bottom = view.findViewById(R.id.bottom);
container = view.findViewById(R.id.container);
mRecyclerView = view.findViewById(R.id.recycler);
// 设置布局管理器
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
// 设置adapter
rinkAdapter = new RinkAdapter(rinkBeanList);
mRecyclerView.setAdapter(rinkAdapter);
// 设置Item添加和移除的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
class RinkAdapter extends BaseQuickAdapter<RinkBean, BaseViewHolder> {
public RinkAdapter(@Nullable List<RinkBean> data) {
super(R.layout.rink_item_view, data);
}
@Override
protected void convert(BaseViewHolder helper, RinkBean item) {
ImageView image = helper.getView(R.id.imageView);
TextView textView = helper.getView(R.id.text);
textView.setText(item.getName());
if (item.isSelect) {
textView.setTextColor(getResources().getColor(R.color.YellowGray));
image.setVisibility(VISIBLE);
} else {
textView.setTextColor(getResources().getColor(R.color.Black));
image.setVisibility(GONE);
}
helper.itemView.setOnClickListener(v -> {
item.setSelect(true);
viewGone(item);
});
}
}
private void viewGone(RinkBean bean) {
setVisibility(GONE);
if (missListener != null) {
missListener.backBean(bean);
}
}
public static class RinkBean implements Serializable {
private String name;
private boolean isSelect;
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public RinkBean() {}
public RinkBean(String name, boolean isSelect) {
this.name = name;
this.isSelect = isSelect;
}
public RinkBean(String name, boolean isSelect,String desc) {
this.name = name;
this.isSelect = isSelect;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
}
public interface DissMissListener {
void backBean(RinkBean context);
}
//view 消失时 将数据对象 返回
private DissMissListener missListener;
public void setDissMissListener(DissMissListener missListener) {
this.missListener = missListener;
}
}
public class TimeView extends FrameLayout {
private String selectType = "param9";//默认
private RinkBean temp;
private LinearLayout bottom;
private ConstraintLayout container;
private RecyclerView mRecyclerView;
private RinkAdapter rinkAdapter;
private List<RinkBean> rinkBeanList = new ArrayList<>();
private Context mContext;
private String[] time_sorts = new String[]{ "全部时间", "最近一天", "最近一周", "最近一月", "最近半年", "最近一年"};
private View view;
public TimeView(Context context) {
super(context);
}
public TimeView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
view = inflate(getContext(), R.layout.rink_view, this);
}
//在每个Fragment中点击显示时 -- 将type 传入 从而 设置数据
public void setVisibility(int visibility, RinkBean temp, String selectType) {
super.setVisibility(visibility);
this.temp = temp;
this.selectType = selectType;
initLayout();
initData();
initEvent();
}
private void initView() {
initLayout();
initData();
initEvent();
}
private void initEvent() {
container.setOnClickListener(view -> {
viewGone(temp);
});
bottom.setOnClickListener(view -> {
viewGone(temp);
});
}
//点击tab时传入type,加载 数据
private void initData() {
rinkBeanList.clear();
if (!TextUtils.isEmpty(selectType)) {
switch (selectType) {
case ZHITI:
rinkBeanList = initDaZhuData();
break;
case ZHIKA:
rinkBeanList = initDaZhuData();
break;
case QUESTION:
rinkBeanList = initWenDaData();
break;
case ARTICLE:
rinkBeanList = initArticleData();
break;
case CIRCLE:
rinkBeanList = initCircleData();
break;
case COLUMN_LIVE:
rinkBeanList = initZhiBoData();
break;
default:
}
}
rinkAdapter.setNewData(rinkBeanList);
}
private List<RinkBean> initDaZhuData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < time_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(time_sorts[i]);
if (null != temp) {
if (temp.getName().equals(time_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
//如果temp为null,则默认取数据的第一条为选中
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initWenDaData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < time_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(time_sorts[i]);
if (null != temp) {
if (temp.getName().equals(time_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initCircleData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < time_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(time_sorts[i]);
if (null != temp) {
if (temp.getName().equals(time_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initZhiBoData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < time_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(time_sorts[i]);
if (null != temp) {
if (temp.getName().equals(time_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private List<RinkBean> initArticleData() {
List<RinkBean> tempList = new ArrayList<>();
for (int i = 0; i < time_sorts.length; i++) {
RinkBean rinkBean = new RinkBean();
rinkBean.setName(time_sorts[i]);
if (null != temp) {
if (temp.getName().equals(time_sorts[i])) {
rinkBean.setSelect(true);
}
} else {
if (0 == i) {
rinkBean.setSelect(true);
temp = rinkBean;
}
}
tempList.add(rinkBean);
}
return tempList;
}
private void initLayout() {
bottom = view.findViewById(R.id.bottom);
container = view.findViewById(R.id.container);
mRecyclerView = view.findViewById(R.id.recycler);
// 设置布局管理器
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
// 设置adapter
rinkAdapter = new RinkAdapter(rinkBeanList);
mRecyclerView.setAdapter(rinkAdapter);
// 设置Item添加和移除的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
class RinkAdapter extends BaseQuickAdapter<RinkBean, BaseViewHolder> {
public RinkAdapter(@Nullable List<RinkBean> data) {
super(R.layout.rink_item_view, data);
}
@Override
protected void convert(BaseViewHolder helper, RinkBean item) {
ImageView image = helper.getView(R.id.imageView);
TextView textView = helper.getView(R.id.text);
textView.setText(item.getName());
if (item.isSelect) {
textView.setTextColor(getResources().getColor(R.color.YellowGray));
image.setVisibility(VISIBLE);
} else {
textView.setTextColor(getResources().getColor(R.color.Black));
image.setVisibility(GONE);
}
helper.itemView.setOnClickListener(v -> {
item.setSelect(true);
viewGone(item);
});
}
}
//综合排序消失回调
private void viewGone(RinkBean bean) {
setVisibility(GONE);
if (missListener != null) {
missListener.backBean(bean);
}
}
public static class RinkBean implements Serializable {
private String name;
private boolean isSelect;
public RinkBean() {
}
public RinkBean(String name, boolean isSelect) {
this.name = name;
this.isSelect = isSelect;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
}
public interface DissMissListener {
void backBean(RinkBean context);
}
private DissMissListener missListener;
public void setDissMissListener(DissMissListener missListener) {
this.missListener = missListener;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 一般的黑色的 40% 不透明度 -->
<View
android:background="#66000000"
android:layout_width="match_parent"
android:layout_height="match_parent"></View>
<View
android:id="@+id/view1"
android:visibility="visible"
app:layout_constraintTop_toTopOf="parent"
android:background="#F5F6F8"
android:layout_width="match_parent"
android:layout_height="1px"></View>
<android.support.v7.widget.RecyclerView
android:overScrollMode="never"
app:layout_constraintTop_toBottomOf="@+id/view1"
android:id="@+id/recycler"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="@color/white"
app:layout_constraintTop_toBottomOf="@+id/recycler"
android:id="@+id/bottom"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="52dp">
<ImageView
android:scaleType="centerCrop"
android:src="@mipmap/sort_up1"
android:layout_width="12dp"
android:layout_height="7.5dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
- ClassifyView 领域筛选
- 设置4种消失类型,View关闭时设置不同的消失类型
public class ClassifyView extends FrameLayout {
public static final int ItemCLick = 1;
public static final int QingKongCLick = 2;
public static final int BlankCLick = 3;
public static final int OutSideCLick = 4;
private TextView qingkong;
private IRecyclerView irc_left;
private IRecyclerView irc_right;
//① 左侧集合数据 -- 数据类可自行项目实现
private List<HomeClassifyListBean.DataBean.RowsBean> leftList = new ArrayList<>();
//① 右侧集合数据 -- 数据类可自行项目实现
private List<List<HomeClassifyListBean.DataBean.RowsBean>> rightList = new ArrayList<>();
private ChooseClassifyLeftListAdapter leftListAdapter;
private ChooseClassifyRightListAdapter rightListAdapter;
private int leftPos = 0;
private String tag = "";
private String selectType = "";
//② 传入到View 中的临时对象
private RinkBean temp;
private Context mContext;
private View view;
public ClassifyView(Context context) {
super(context);
}
public ClassifyView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
view = inflate(getContext(), R.layout.classify_view, this);
}
public void setVisibility(int visibility, RinkBean temp, String selectType) {
super.setVisibility(visibility);
this.temp = temp;
this.selectType = selectType;
initLayout();
requestLeftData();
initEvent();
}
public void requestLeftData() {
Map<String, String> map = new HashMap<>();
map.put("pid",String.valueOf(0));
OkGoUtils.GET(0, Url.logURL + Url.getClassifyLists, map, "leftData", new OkGoUtilsInterFace() {
@Override
public void onStart(Request<String, ? extends Request> request) {
}
@Override
public void onSuccess(Response<String> response, int requestCode) {
HomeClassifyListBean bean = new Gson().fromJson(response.body(),new TypeToken<HomeClassifyListBean>() {}.getType());
if (null!= bean ){
if ( bean.getCode() == 0 && null!= bean.getData() && null!= bean.getData().getRows()){
if (null!= leftList){
leftList.clear();
}
leftList = bean.getData().getRows();
if(temp == null){
leftPos = 0;
}else{
leftPos = temp.getLeftPos();
}
initRightData();
initPresenterRight(leftPos);
}else {
if (null!= bean.getMsg()){
ToastUtils.showShort(bean.getMsg());
}
}
}else {
ToastUtils.showShort("数据为空");
}
}
@Override
public void Error(Response<String> response) {
ToastUtils.showShort(response.message());
}
@Override
public void onFinsh() {
}
@Override
public void onNetWorkError() {
ToastUtils.showShort("网络异常");
}
});
}
private void initRightData() {
if (null!= leftList && leftList.size()>0){
for (int i = 0; i < leftList.size(); i++) {
rightList.add(i,null);
}
}
}
private void initPresenterRight(int position) {
String id = "",name = "";
if (null!= leftList && leftList.size()>position && null!= leftList.get(position)){
if (null!= leftList.get(position).getId()){
id = leftList.get(position).getId();
}
if (null!= leftList.get(position).getName()){
name = leftList.get(position).getName();
}
}
if (!TextUtils.isEmpty(id)){
requestRightData(id,name);
}
}
private List<HomeClassifyListBean.DataBean.RowsBean> dataList = new ArrayList<>();
public void requestRightData(String id,String name) {
Map<String, String> map = new HashMap<>();
map.put("pid",id);
OkGoUtils.GET(0, Url.logURL + Url.getClassifyLists, map, "rightData", new OkGoUtilsInterFace() {
@Override
public void onStart(Request<String, ? extends Request> request) {
}
@Override
public void onSuccess(Response<String> response, int requestCode) {
HomeClassifyListBean bean = new Gson().fromJson(response.body(),new TypeToken<HomeClassifyListBean>() {}.getType());
if (null!= bean ){
if ( bean.getCode() == 0 && null!= bean.getData() && null!= bean.getData().getRows()){
List<HomeClassifyListBean.DataBean.RowsBean> data = bean.getData().getRows();
if (null!= dataList){
dataList.clear();
}
//让第二个横向的 ItemView 不显示,但占据一块位置
if (null!= id){
HomeClassifyListBean.DataBean.RowsBean rowsBean = new HomeClassifyListBean.DataBean.RowsBean();
rowsBean.setId(id);
rowsBean.setName(name);
dataList.add(rowsBean);
dataList.add(null);
}
//过滤下数据
if(dataList != null){
if(temp != null && dataList.get(0).getId().equals(temp.getId())){
dataList.get(0).setSearchChoosed(true);
}
if(null != data && data.size() > 0){
for (HomeClassifyListBean.DataBean.RowsBean bean1 : data) {
if(bean1 != null && temp != null && bean1.getId().equals(temp.getId())){
bean1.setSearchChoosed(true);
}
dataList.add(bean1);
}
}
}
if (null!= rightList && rightList.size()>leftPos){
if (null!= rightList.get(leftPos) && rightList.get(leftPos).size()>0){
//已经加载过的页面数据不做处理
}else {
rightList.set(leftPos,dataList);
}
}
initRightAdapter();
initLeftAdapter();
leftListAdapter.setRefreshData(leftPos);
irc_right.setVisibility(VISIBLE);
initListener();
}else {
if (null!= bean.getMsg()){
ToastUtils.showShort(bean.getMsg());
}
}
}else {
ToastUtils.showShort("数据为空");
}
}
@Override
public void Error(Response<String> response) {
ToastUtils.showShort(response.message());
}
@Override
public void onFinsh() {
}
@Override
public void onNetWorkError() {
ToastUtils.showShort("网络异常");
}
});
}
private void initLeftAdapter(){
irc_left.setRefreshEnabled(false);
irc_left.setLoadMoreEnabled(false);
irc_left.setLayoutManager(new LinearLayoutManager(mContext));
if (null == leftListAdapter){
leftListAdapter = new ChooseClassifyLeftListAdapter(mContext,leftList,tag);
}else{
leftListAdapter.setRefreshData(leftPos);
leftListAdapter.setRefreshDataV1(leftList);
}
irc_left.setAdapter(leftListAdapter);
}
private void initRightAdapter(){
irc_right.setLayoutManager(new FullyGridLayoutManager(mContext,2));
if(rightListAdapter == null){
rightListAdapter = new ChooseClassifyRightListAdapter(mContext,rightList.get(leftPos),tag);
}
irc_right.setAdapter(rightListAdapter);
rightListAdapter.setRefreshChooseData(rightList.get(leftPos));
}
private HomeClassifyListBean.DataBean.RowsBean itemBean;
private void initListener() {
if (null!= leftListAdapter){
leftListAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(ViewGroup parent, View view, Object o, int position) {
updateLeftData(position);
}
@Override
public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
return false;
}
});
}
if (null!= rightListAdapter){
rightListAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(ViewGroup parent, View view, Object o, int position) {
String id = "",name = "";
if (null!= rightList && rightList.size()>leftPos
&& null!= rightList.get(leftPos)
&& rightList.get(leftPos).size() > position
&& null!= rightList.get(leftPos).get(position)){
if (null!= rightList.get(leftPos).get(position).getId()){
id = rightList.get(leftPos).get(position).getId();
}
if (null!= rightList.get(leftPos).get(position).getName()){
name = rightList.get(leftPos).get(position).getName();
}
}
if (position!= DataConstants.ONE_INTEGER && !TextUtils.isEmpty(id)){
itemBean = rightListAdapter.get(position);
setVisibility_Type(GONE,ItemCLick);
}
}
@Override
public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
return false;
}
});
}
}
private void updateLeftData(int position) {
leftPos = position;
initPresenterRight(position);
}
float scrollX,scrollY;
@SuppressLint("ClickableViewAccessibility")
private void initEvent() {
qingkong.setOnClickListener(view ->{
setVisibility_Type(GONE,QingKongCLick);
});
//RecyclerView 空白区域的点击事件
irc_right.setOnTouchListener((v, event) -> {
if(event.getAction() == MotionEvent.ACTION_DOWN){
scrollX = event.getX();
scrollY = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() != 0 && Math.abs(scrollX - event.getX()) <= 5 && Math.abs(scrollY - event.getY()) <= 5) {
//recyclerView空白处点击事件
setVisibility_Type(GONE,BlankCLick);
}
}
return false;
});
}
private static final String TAG = "ClassifyView";
private void initLayout() {
qingkong = view.findViewById(R.id.qingkong);
irc_left = view.findViewById(R.id.irc_left);
irc_right = view.findViewById(R.id.irc_right);
irc_right.setVisibility(View.GONE);
}
public void setVisibility_Type(int visibility,int type){
super.setVisibility(visibility);
if(visibility == View.GONE && OkGo.getInstance() != null){
OkGo.getInstance().cancelTag("leftData");
OkGo.getInstance().cancelTag("rightData");
viewGone(type);
}
}
//消失的主要的逻辑
private void viewGone(int type) {
RinkBean bean = null;
//③ 消失类型分别为 1 item点击消失 2 清空按钮消失 3 空白处消失 4 外界消失
if(1 == type){
if(itemBean != null){
bean = new RinkBean();
bean.setName(itemBean.getName());
bean.setLeftPos(leftPos);
bean.setId(itemBean.getId());
}
}else if(2 == type){
bean = new RinkBean();
bean.setRequest(true);
bean.setLeftPos(0);
}else if(3 == type){
if(temp != null){
bean = temp;
bean.setRequest(false);
bean.setLeftPos(leftPos);
}
}else if(4 == type){
if(temp != null){
bean = temp;
bean.setRequest(false);
bean.setLeftPos(leftPos);
}
}
if (missListener != null) {
missListener.backBean(bean);
}
}
public static class RinkBean implements Serializable {
private String id;
private String name;
private int leftPos;
private boolean isSelect;
private boolean isRequest;//点击清空按钮时 同时请求数据
public boolean isRequest() {
return isRequest;
}
public void setRequest(boolean request) {
isRequest = request;
}
public RinkBean() {
}
//默认设置左边第一个
public RinkBean(int leftPos) {
this.leftPos = leftPos;
}
public int getLeftPos() {
return leftPos;
}
public void setLeftPos(int leftPos) {
this.leftPos = leftPos;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
}
public interface DissMissListener {
void backBean(RinkBean context);
}
private DissMissListener missListener;
public void setDissMissListener(DissMissListener missListener) {
this.missListener = missListener;
}
}
//设置筛选 iew 是否正在展示
public static boolean isShowView = false;
private RinkingView.RinkBean sort_bean ; //综合排序
private PriceView.RinkBean price_bean; //价格排序
private TimeView.RinkBean time_bean; //时间排序
private ClassifyView.RinkBean classify_bean; //领域排序
@Override
public void onClick(View v) {
switch (v.getId()) {
//综合排序
case R.id.comprehensive_ranking:
setPriceViewStatus();
setTimeViewStatus();
setClassifyViewStatus();
if (rinkingView.getVisibility() == View.VISIBLE) {
rinkingView.setVisibility(View.GONE);
//恢复 refreshLayout 的下拉刷新
refreshLayout.setEnableRefresh(true);
} else {
if(sort_bean == null){
//默认综合排序为选中状态
sort_bean = new RinkingView.RinkBean("综合排序",true);
}
rinkingView.setVisibility(View.VISIBLE, sort_bean, selectType);
setShowAction();
}
setDrawableRight(rinkingView);
break;
//价格排序
case R.id.all_price:
setRinkingViewStatus();
setTimeViewStatus();
setClassifyViewStatus();
if (priceView.getVisibility() == View.VISIBLE) {
priceView.setVisibility(View.GONE);
refreshLayout.setEnableRefresh(true);
} else {
if(price_bean == null){
price_bean = new PriceView.RinkBean("全部价格",true);
}
priceView.setVisibility(View.VISIBLE, price_bean, selectType);
setShowAction();
}
setDrawableRight_Price(priceView);
break;
//时间排序
case R.id.all_time:
setRinkingViewStatus();
setPriceViewStatus();
setClassifyViewStatus();
if (timeView.getVisibility() == View.VISIBLE) {
timeView.setVisibility(View.GONE);
refreshLayout.setEnableRefresh(true);
} else {
if(time_bean == null){
time_bean = new TimeView.RinkBean("全部时间",true);
}
timeView.setVisibility(View.VISIBLE, time_bean, selectType);
setShowAction();
}
setDrawableRight_Time(timeView);
break;
//筛选排序
case R.id.choose_part:
setRinkingViewStatus();
setPriceViewStatus();
setTimeViewStatus();
if (classifyView.getVisibility() == View.VISIBLE) {
classifyView.setVisibility_Type(View.GONE,ClassifyView.OutSideCLick);
refreshLayout.setEnableRefresh(true);
} else {
if(classify_bean == null){
classify_bean = new ClassifyView.RinkBean(0);
}
classifyView.setVisibility(View.VISIBLE, classify_bean, selectType);
setShowAction();
}
setDrawableRight_Classify(classifyView);
break;
default:
}
}
private void setPriceViewStatus(){
priceView.setVisibility(View.GONE);
setDrawableRight_Price(priceView);
}
private void setTimeViewStatus(){
timeView.setVisibility(View.GONE);
setDrawableRight_Time(timeView);
}
private void setClassifyViewStatus(){
classifyView.setVisibility(View.GONE);
setDrawableRight_Classify(classifyView);
}
private void setRinkingViewStatus(){
rinkingView.setVisibility(View.GONE);
setDrawableRight(rinkingView);
}
//设置文本后边 小图片
private void setDrawableRight(RinkingView rinkingView) {
Drawable nav_up;
if (rinkingView.getVisibility() == View.VISIBLE) {
nav_up = getResources().getDrawable(R.mipmap.screen_up);
} else {
nav_up = getResources().getDrawable(R.mipmap.screen_down);
}
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
comprehensive_ranking_tv.setCompoundDrawables(null, null, nav_up, null);
}
//① 这部逻辑比较重要
//isShowView 全部变量 设置了展示
//refreshLayout 设置SmartRefreshLayout 不能下拉刷新
//ShowFuGaiViewEvent 设置了SearchActivity 中的覆盖布局显示 ② --> 就是用一层View盖在 搜索 和 tabLayout 上面
private void setShowAction() {
isShowView = true;
refreshLayout.setEnableRefresh(false);
EventBus.getDefault().post(new ShowFuGaiViewEvent(1));
}
//ShowFuGaiViewEvent 设置了SearchActivity 中的覆盖布局隐藏
private void setHideAction() {
isShowView = false;
refreshLayout.setEnableRefresh(true);
EventBus.getDefault().post(new ShowFuGaiViewEvent(2));
}
private void initEvent() {
comprehensive_ranking.setOnClickListener(this);
all_time.setOnClickListener(this);
all_price.setOnClickListener(this);
send_time.setOnClickListener(this);
choose_part.setOnClickListener(this);
rinkingView.setDissMissListener(backBean -> {
//如果没有任何操作,不做请求操作
if(!sort_bean.getName().equals(backBean.getName())){
sort_bean = backBean;
sort_zonghe = getDescBySortZongHeName(backBean.getName());
//TODO 请求接口的数据
fetchData();
}
setDrawableRight(rinkingView);
comprehensive_ranking_tv.setText(backBean.getName());
setHideAction();
});
priceView.setDissMissListener(backBean -> {
if(!price_bean.getName().equals(backBean.getName())){
price_bean = backBean;
sort_price = getDescBySortPriceName(backBean.getName());
fetchData();
}
setDrawableRight_Price(priceView);
all_price_tv.setText(backBean.getName());
setHideAction();
});
timeView.setDissMissListener(backBean -> {
if(!time_bean.getName().equals(backBean.getName())){
time_bean = backBean;
sort_time = getDescBySortTimeName(backBean.getName());
fetchData();
}
setDrawableRight_Time(timeView);
all_time_tv.setText(backBean.getName());
setHideAction();
});
classifyView.setDissMissListener(backBean -> {
if(backBean != null){
//返回的id是"" 说明没选 相反已选择了右侧数据
if(TextUtils.isEmpty(backBean.getId())){
classify_bean = backBean;
choose_part_tv.setText("筛选领域");
choose_part_tv.setTextColor(getResources().getColor(R.color.black_33));
sort_lingyu = "";
//点击清空请求数据
if(backBean.isRequest()){
fetchData();
}
}else{
if(!backBean.getId().equals(classify_bean.getId())){
classify_bean = backBean;
choose_part_tv.setText(backBean.getName());
Log.d(TAG, "返回的内容是 " + backBean.getName() + " 左边的位置 " + backBean.getLeftPos());
sort_lingyu = backBean.getId();
fetchData();
}
choose_part_tv.setTextColor(getResources().getColor(R.color.YellowGray));
}
setDrawableRight_Classify(classifyView);
setHideAction();
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onCloseViewEvent(CloseViewEvent msg) {
setAllViewStatus();
}
private void setAllViewStatus(){
rinkingView.setVisibility(View.GONE);
setDrawableRight(rinkingView);
//这里是通过外部让View消失的
classifyView.setVisibility_Type(View.GONE,ClassifyView.OutSideCLick);
setDrawableRight_Classify(classifyView);
timeView.setVisibility(View.GONE);
setDrawableRight_Time(timeView);
priceView.setVisibility(View.GONE);
setDrawableRight_Price(priceView);
setHideAction();
}
public final static String SELECT_TYPE = "selectType";
public static final String All = "param0";//问答
public static final String QUESTION = "param1";//问答
public static final String ARTICLE = "param2";//文章
public static final String USER = "param3";//用户
public static final String CIRCLE = "param4";//圈子
public static final String COLUMN_COLLECTION = "param5";//专栏
public static final String COLUMN_CLASS = "param6";//精品课
public static final String COLUMN_LIVE = "param7";//直播
public static final String ACTIVITY = "param8";//活动
public static final String ZHIKA = "param9";//答主
public static final String ZHITI = "param10";//主题
private String selectType;//当前选择的 ab
.......
//通过名称来设置不同的过滤字段
private String getDescBySortZongHeName(String name) {
String orderDesc = "";
switch (name){
case "综合排序":
orderDesc = "";
break;
case "最新回答":
case "最新":
orderDesc = "time_desc";
break;
case "热门排序":
case "热门":
orderDesc = "heat_desc";
break;
case "回答最多":
orderDesc = "answer_desc";
break;
case "成员最多":
orderDesc = "member_desc";
break;
case "主题最多":
orderDesc = "dynamic_desc";
break;
case "粉丝最多":
orderDesc = "fans_desc";
break;
case "被 Get 最多":
case "最多Get":
orderDesc = "geted_desc";
break;
default:
}
return orderDesc;
}
private void initListener() {
//覆盖View 的点击事件
part_view.setOnClickListener(view ->{
part_view.setVisibility(View.GONE);
if(SearchFragment.isShowView){
EventBus.getDefault().post(new CloseViewEvent());
return;
}
if(tabChooseView != null && tabChooseView.getVisibility() == View.VISIBLE){
tabChooseView.setVisibility(View.GONE);
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onShowFuGaiViewEvent(ShowFuGaiViewEvent event) {
if(1 == event.getType() && part_view != null && part_view.getVisibility() == View.GONE){
part_view.setVisibility(View.VISIBLE);
}
if(2 == event.getType() && part_view != null && part_view.getVisibility() == View.VISIBLE){
part_view.setVisibility(View.GONE);
}
}