|
| 1 | +package com.yalantis.fitfilter; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v4.content.ContextCompat; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.support.v7.util.DiffUtil; |
| 7 | +import android.support.v7.widget.LinearLayoutManager; |
| 8 | +import android.support.v7.widget.RecyclerView; |
| 9 | + |
| 10 | +import com.facebook.drawee.backends.pipeline.Fresco; |
| 11 | +import com.facebook.imagepipeline.core.ImagePipelineConfig; |
| 12 | +import com.yalantis.filter.adapter.FilterAdapter; |
| 13 | +import com.yalantis.filter.animator.FiltersListItemAnimator; |
| 14 | +import com.yalantis.filter.listener.FilterListener; |
| 15 | +import com.yalantis.filter.widget.Filter; |
| 16 | +import com.yalantis.filter.widget.FilterItem; |
| 17 | + |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +public class ExampleActivity extends AppCompatActivity implements FilterListener<Tag> { |
| 24 | + |
| 25 | + private RecyclerView mRecyclerView; |
| 26 | + |
| 27 | + private int[] mColors; |
| 28 | + private String[] mTitles; |
| 29 | + private List<Question> mAllQuestions; |
| 30 | + private Filter<Tag> mFilter; |
| 31 | + private QuestionsAdapter mAdapter; |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void onCreate(Bundle savedInstanceState) { |
| 35 | + super.onCreate(savedInstanceState); |
| 36 | + setContentView(R.layout.activity_example); |
| 37 | + |
| 38 | + ImagePipelineConfig config = ImagePipelineConfig |
| 39 | + .newBuilder(this) |
| 40 | + .setDownsampleEnabled(true) |
| 41 | + .build(); |
| 42 | + Fresco.initialize(this, config); |
| 43 | + |
| 44 | + mColors = getResources().getIntArray(R.array.colors); |
| 45 | + mTitles = getResources().getStringArray(R.array.job_titles); |
| 46 | + |
| 47 | + mFilter = (Filter<Tag>) findViewById(R.id.filter); |
| 48 | + mFilter.setAdapter(new Adapter(getTags())); |
| 49 | + mFilter.setListener(this); |
| 50 | + mFilter.setNoSelectedItemText(getString(R.string.str_all_selected)); |
| 51 | + mFilter.build(); |
| 52 | + |
| 53 | + mRecyclerView = (RecyclerView) findViewById(R.id.list); |
| 54 | + mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); |
| 55 | + mRecyclerView.setAdapter(mAdapter = new QuestionsAdapter(this, mAllQuestions = getQuestions())); |
| 56 | + mRecyclerView.setItemAnimator(new FiltersListItemAnimator()); |
| 57 | + } |
| 58 | + |
| 59 | + private void calculateDiff(final List<Question> oldList, final List<Question> newList) { |
| 60 | + DiffUtil.calculateDiff(new DiffUtil.Callback() { |
| 61 | + @Override |
| 62 | + public int getOldListSize() { |
| 63 | + return oldList.size(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int getNewListSize() { |
| 68 | + return newList.size(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { |
| 73 | + return oldList.get(oldItemPosition).equals(newList.get(newItemPosition)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { |
| 78 | + return oldList.get(oldItemPosition).equals(newList.get(newItemPosition)); |
| 79 | + } |
| 80 | + }).dispatchUpdatesTo(mAdapter); |
| 81 | + } |
| 82 | + |
| 83 | + private List<Tag> getTags() { |
| 84 | + List<Tag> tags = new ArrayList<>(); |
| 85 | + |
| 86 | + for (int i = 0; i < mTitles.length; ++i) { |
| 87 | + tags.add(new Tag(mTitles[i], mColors[i])); |
| 88 | + } |
| 89 | + |
| 90 | + return tags; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onNothingSelected() { |
| 95 | + if (mRecyclerView != null) { |
| 96 | + mAdapter.setQuestions(mAllQuestions); |
| 97 | + mAdapter.notifyDataSetChanged(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private List<Question> getQuestions() { |
| 102 | + return new ArrayList<Question>() {{ |
| 103 | + add(new Question("Carol Bell", "Graphic Designer", |
| 104 | + "http://kingofwallpapers.com/girl/girl-011.jpg", "Nov 20, 6:12 PM", |
| 105 | + "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{ |
| 106 | + add(new Tag(mTitles[2], mColors[2])); |
| 107 | + add(new Tag(mTitles[4], mColors[4])); |
| 108 | + }})); |
| 109 | + add(new Question("Melissa Morales", "Project Manager", |
| 110 | + "http://weknowyourdreams.com/images/girl/girl-03.jpg", "Nov 20, 3:48 AM", |
| 111 | + "What is your biggest frustration with taking your business/career (in a corporate) to the next level?", new ArrayList<Tag>() {{ |
| 112 | + add(new Tag(mTitles[1], mColors[1])); |
| 113 | + add(new Tag(mTitles[5], mColors[5])); |
| 114 | + }})); |
| 115 | + add(new Question("Rochelle Yingst", "iOS Developer", |
| 116 | + "http://www.viraldoza.com/wp-content/uploads/2014/03/8876509-lily-pretty-girl.jpg", "Nov 20, 6:12 PM", |
| 117 | + "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{ |
| 118 | + add(new Tag(mTitles[7], mColors[7])); |
| 119 | + add(new Tag(mTitles[8], mColors[8])); |
| 120 | + }})); |
| 121 | + add(new Question("Lacey Barbara", "QA Engineer", |
| 122 | + "http://kingofwallpapers.com/girl/girl-019.jpg", "Nov 20, 6:12 PM", |
| 123 | + "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{ |
| 124 | + add(new Tag(mTitles[3], mColors[3])); |
| 125 | + add(new Tag(mTitles[9], mColors[9])); |
| 126 | + }})); |
| 127 | + add(new Question("Teena Allain", "Android Developer", |
| 128 | + "http://tribzap2it.files.wordpress.com/2014/09/hannah-simone-new-girl-season-4-cece.jpg", "Nov 20, 6:12 PM", |
| 129 | + "What is the first step to transform an idea into an actual project?", new ArrayList<Tag>() {{ |
| 130 | + add(new Tag(mTitles[1], mColors[1])); |
| 131 | + add(new Tag(mTitles[6], mColors[6])); |
| 132 | + }})); |
| 133 | + }}; |
| 134 | + } |
| 135 | + |
| 136 | + private List<Question> findByTags(List<Tag> tags) { |
| 137 | + List<Question> questions = new ArrayList<>(); |
| 138 | + |
| 139 | + for (Question question : mAllQuestions) { |
| 140 | + for (Tag tag : tags) { |
| 141 | + if (question.hasTag(tag.getText()) && !questions.contains(question)) { |
| 142 | + questions.add(question); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return questions; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onFiltersSelected(@NotNull ArrayList<Tag> filters) { |
| 152 | + List<Question> newQuestions = findByTags(filters); |
| 153 | + List<Question> oldQuestions = mAdapter.getQuestions(); |
| 154 | + mAdapter.setQuestions(newQuestions); |
| 155 | + calculateDiff(oldQuestions, newQuestions); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onFilterSelected(Tag item) { |
| 160 | + if (item.getText().equals(mTitles[0])) { |
| 161 | + mFilter.deselectAll(); |
| 162 | + mFilter.collapse(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onFilterDeselected(Tag item) { |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + class Adapter extends FilterAdapter<Tag> { |
| 172 | + |
| 173 | + Adapter(@NotNull List<? extends Tag> items) { |
| 174 | + super(items); |
| 175 | + } |
| 176 | + |
| 177 | + @NotNull |
| 178 | + @Override |
| 179 | + public FilterItem createView(int position, Tag item) { |
| 180 | + FilterItem filterItem = new FilterItem(ExampleActivity.this); |
| 181 | + |
| 182 | + filterItem.setStrokeColor(mColors[0]); |
| 183 | + filterItem.setTextColor(mColors[0]); |
| 184 | + filterItem.setCheckedTextColor(ContextCompat.getColor(ExampleActivity.this, android.R.color.white)); |
| 185 | + filterItem.setColor(ContextCompat.getColor(ExampleActivity.this, android.R.color.white)); |
| 186 | + filterItem.setCheckedColor(mColors[position]); |
| 187 | + filterItem.setText(item.getText()); |
| 188 | + filterItem.deselect(); |
| 189 | + |
| 190 | + return filterItem; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments