Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Nov 26, 2016
1 parent e9027ab commit ca7e477
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@
*/
public class FilterRightAdapter extends BaseListAdapter<FilterEntity> {

private FilterEntity selectedEntity;

public FilterRightAdapter(Context context) {
super(context);
}

public FilterRightAdapter(Context context, List<FilterEntity> list) {
super(context, list);
}

public void setSelectedEntity(FilterEntity filterEntity) {
this.selectedEntity = filterEntity;
for (FilterEntity entity : getData()) {
entity.setSelected(entity.getKey().equals(selectedEntity.getKey()));
entity.setSelected(filterEntity != null && entity.getKey().equals(filterEntity.getKey()));
}
notifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class FilterTwoEntity implements Serializable {
private String type;
private List<FilterEntity> list;
private boolean isSelected;
private FilterEntity selectedFilterEntity;

public FilterTwoEntity() {
}
Expand All @@ -29,14 +28,6 @@ public void setType(String type) {
this.type = type;
}

public FilterEntity getSelectedFilterEntity() {
return selectedFilterEntity;
}

public void setSelectedFilterEntity(FilterEntity selectedFilterEntity) {
this.selectedFilterEntity = selectedFilterEntity;
}

public boolean isSelected() {
return isSelected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ public void onFilterClick(int position) {
// 分类Item点击
filterView.setOnItemCategoryClickListener(new FilterView.OnItemCategoryClickListener() {
@Override
public void onItemCategoryClick(FilterTwoEntity entity) {
fillAdapter(ModelUtil.getCategoryTravelingData(entity));
public void onItemCategoryClick(FilterTwoEntity leftEntity, FilterEntity rightEntity) {
fillAdapter(ModelUtil.getCategoryTravelingData(leftEntity, rightEntity));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,12 @@ public static List<FilterEntity> getFilterData() {
}

// ListView分类数据
public static List<TravelingEntity> getCategoryTravelingData(FilterTwoEntity entity) {
public static List<TravelingEntity> getCategoryTravelingData(FilterTwoEntity leftEntity, FilterEntity rightEntity) {
List<TravelingEntity> list = getTravelingData();
List<TravelingEntity> travelingList = new ArrayList<>();
int size = list.size();
for (int i=0; i<size; i++) {
if (list.get(i).getType().equals(entity.getType()) &&
list.get(i).getFrom().equals(entity.getSelectedFilterEntity().getKey())) {
if (list.get(i).getType().equals(leftEntity.getType()) && list.get(i).getFrom().equals(rightEntity.getKey())) {
travelingList.add(list.get(i));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
*/
public class FilterView extends LinearLayout implements View.OnClickListener {

@BindView(R.id.tv_category)
TextView tvCategory;
@BindView(R.id.tv_category_title)
TextView tvCategoryTitle;
@BindView(R.id.iv_category_arrow)
ImageView ivCategoryArrow;
@BindView(R.id.tv_sort)
TextView tvSort;
@BindView(R.id.tv_sort_title)
TextView tvSortTitle;
@BindView(R.id.iv_sort_arrow)
ImageView ivSortArrow;
@BindView(R.id.tv_filter)
TextView tvFilter;
@BindView(R.id.tv_filter_title)
TextView tvFilterTitle;
@BindView(R.id.iv_filter_arrow)
ImageView ivFilterArrow;
@BindView(R.id.ll_category)
Expand Down Expand Up @@ -79,7 +79,8 @@ public class FilterView extends LinearLayout implements View.OnClickListener {
private FilterOneAdapter sortAdapter;
private FilterOneAdapter filterAdapter;

private FilterTwoEntity selectedCategoryEntity; // 被选择的分类项
private FilterTwoEntity leftSelectedCategoryEntity; // 被选择的分类项左侧数据
private FilterEntity rightSelectedCategoryEntity; // 被选择的分类项右侧数据
private FilterEntity selectedSortEntity; // 被选择的排序项
private FilterEntity selectedFilterEntity; // 被选择的筛选项

Expand Down Expand Up @@ -149,13 +150,13 @@ public void onClick(View v) {

// 复位筛选的显示状态
public void resetFilterStatus() {
tvCategory.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
tvCategoryTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow);

tvSort.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
tvSortTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivSortArrow.setImageResource(R.mipmap.home_down_arrow);

tvFilter.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
tvFilterTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivFilterArrow.setImageResource(R.mipmap.home_down_arrow);
}

Expand All @@ -167,101 +168,85 @@ public void resetAllStatus() {

// 设置分类数据
private void setCategoryAdapter() {
tvCategory.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow_red);
lvLeft.setVisibility(VISIBLE);
lvRight.setVisibility(VISIBLE);

if (selectedCategoryEntity == null) {
selectedCategoryEntity = filterData.getCategory().get(0);
}

// 左边列表视图
leftAdapter = new FilterLeftAdapter(mContext, filterData.getCategory());
lvLeft.setAdapter(leftAdapter);
leftAdapter.setSelectedEntity(selectedCategoryEntity);
if (leftSelectedCategoryEntity == null) {
leftSelectedCategoryEntity = filterData.getCategory().get(0);
}
leftAdapter.setSelectedEntity(leftSelectedCategoryEntity);

lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCategoryEntity = filterData.getCategory().get(position);
leftAdapter.setSelectedEntity(selectedCategoryEntity);
leftSelectedCategoryEntity = filterData.getCategory().get(position);
leftAdapter.setSelectedEntity(leftSelectedCategoryEntity);

// 右边列表视图
rightAdapter = new FilterRightAdapter(mContext, selectedCategoryEntity.getList());
rightAdapter = new FilterRightAdapter(mContext, leftSelectedCategoryEntity.getList());
lvRight.setAdapter(rightAdapter);
lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCategoryEntity.setSelectedFilterEntity(selectedCategoryEntity.getList().get(position));
rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectedFilterEntity());
hide();
if (onItemCategoryClickListener != null) {
onItemCategoryClickListener.onItemCategoryClick(selectedCategoryEntity);
}
}
});
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
}
});

// 如果右边有选中的数据,设置
if (selectedCategoryEntity.getSelectedFilterEntity() != null) {
rightAdapter = new FilterRightAdapter(mContext, selectedCategoryEntity.getList());
} else {
rightAdapter = new FilterRightAdapter(mContext, filterData.getCategory().get(0).getList());
}
// 右边列表视图
rightAdapter = new FilterRightAdapter(mContext, leftSelectedCategoryEntity.getList());
lvRight.setAdapter(rightAdapter);
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCategoryEntity.setSelectedFilterEntity(selectedCategoryEntity.getList().get(position));
rightAdapter.setSelectedEntity(selectedCategoryEntity.getSelectedFilterEntity());
hide();
rightSelectedCategoryEntity = leftSelectedCategoryEntity.getList().get(position);
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
if (onItemCategoryClickListener != null) {
onItemCategoryClickListener.onItemCategoryClick(selectedCategoryEntity);
onItemCategoryClickListener.onItemCategoryClick(leftSelectedCategoryEntity, rightSelectedCategoryEntity);
}
hide();
}
});
}

// 设置排序数据
private void setSortAdapter() {
tvSort.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivSortArrow.setImageResource(R.mipmap.home_down_arrow_red);
lvLeft.setVisibility(GONE);
lvRight.setVisibility(VISIBLE);

sortAdapter = new FilterOneAdapter(mContext, filterData.getSorts());
lvRight.setAdapter(sortAdapter);

lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedSortEntity = filterData.getSorts().get(position);
sortAdapter.setSelectedEntity(selectedSortEntity);
hide();
if (onItemSortClickListener != null) {
onItemSortClickListener.onItemSortClick(selectedSortEntity);
}
hide();
}
});
}

// 设置筛选数据
private void setFilterAdapter() {
tvFilter.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivFilterArrow.setImageResource(R.mipmap.home_down_arrow_red);
lvLeft.setVisibility(GONE);
lvRight.setVisibility(VISIBLE);

filterAdapter = new FilterOneAdapter(mContext, filterData.getFilters());
lvRight.setAdapter(filterAdapter);

lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedFilterEntity = filterData.getFilters().get(position);
filterAdapter.setSelectedEntity(selectedFilterEntity);
hide();
if (onItemFilterClickListener != null) {
onItemFilterClickListener.onItemFilterClick(selectedFilterEntity);
}
hide();
}
});
}
Expand All @@ -276,13 +261,19 @@ public void show(int position) {
rotateArrowUp(position);

switch (position) {
case 0:
case POSITION_CATEGORY:
tvCategoryTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow_red);
setCategoryAdapter();
break;
case 1:
case POSITION_SORT:
tvSortTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivSortArrow.setImageResource(R.mipmap.home_down_arrow_red);
setSortAdapter();
break;
case 2:
case POSITION_FILTER:
tvFilterTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivFilterArrow.setImageResource(R.mipmap.home_down_arrow_red);
setFilterAdapter();
break;
}
Expand Down Expand Up @@ -413,7 +404,7 @@ public void setOnItemCategoryClickListener(OnItemCategoryClickListener onItemCat
this.onItemCategoryClickListener = onItemCategoryClickListener;
}
public interface OnItemCategoryClickListener {
void onItemCategoryClick(FilterTwoEntity entity);
void onItemCategoryClick(FilterTwoEntity leftEntity, FilterEntity rightEntity);
}

// 排序Item点击
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/view_filter_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
android:orientation="horizontal">

<TextView
android:id="@+id/tv_category"
android:id="@+id/tv_category_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_image"
Expand Down Expand Up @@ -62,7 +62,7 @@
android:orientation="horizontal">

<TextView
android:id="@+id/tv_sort"
android:id="@+id/tv_sort_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_image"
Expand Down Expand Up @@ -95,7 +95,7 @@
android:orientation="horizontal">

<TextView
android:id="@+id/tv_filter"
android:id="@+id/tv_filter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_image"
Expand Down

0 comments on commit ca7e477

Please sign in to comment.