├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── dictionaries │ └── wangtao.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── libs │ └── gson-2.2.4.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── home │ │ └── labellinkdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── home │ │ │ └── labellinkdemo │ │ │ ├── MyApplication.java │ │ │ ├── ScreenUtils.java │ │ │ ├── ToastUtils.java │ │ │ ├── activity │ │ │ ├── LabelLinkFlowLayoutActivity.java │ │ │ ├── LabelLinkRecyclerActivity.java │ │ │ └── MainActivity.java │ │ │ ├── adapter │ │ │ └── LinkLabelAdapter.java │ │ │ └── bean │ │ │ └── LinkLabelBean.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── circle.xml │ │ ├── circle_bule.xml │ │ ├── circle_fen.xml │ │ ├── circle_ret_orange.xml │ │ ├── dialog_label.xml │ │ ├── dialog_label_text.xml │ │ ├── dialog_label_text_tv.xml │ │ ├── dialog_label_tv.xml │ │ ├── ic_launcher_background.xml │ │ ├── red_sold_round_gay.xml │ │ └── red_sold_round_sel.xml │ │ ├── layout │ │ ├── activity_label_link_flow_layout.xml │ │ ├── activity_label_link_recycler.xml │ │ ├── activity_main.xml │ │ ├── adapter_activity_label_content.xml │ │ ├── adapter_activity_label_title.xml │ │ └── dialog_view_textview.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── home │ └── labellinkdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangtao2855/LabelLinkDemo/620efa7f8c1765b3f36aeada3f2732583d1b113c/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/dictionaries/wangtao.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 项目介绍 2 | ##### 是一款标签选择的demo.有固定的逻辑.适合大部分项目的应用场景 3 | ### 同步简书地址: 4 | #### https://www.jianshu.com/p/d6c4b7c873d3 -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.home.labellinkdemo" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:25.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha9' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | //黄油刀 29 | implementation 'com.jakewharton:butterknife:8.8.1' 30 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 31 | //noinspection GradleCompatible 32 | implementation 'com.android.support:design:25.1.0' 33 | implementation files('libs/gson-2.2.4.jar') 34 | //flowLayout 布局展示 35 | implementation 'com.hyman:flowlayout-lib:1.1.2' 36 | } 37 | -------------------------------------------------------------------------------- /app/libs/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangtao2855/LabelLinkDemo/620efa7f8c1765b3f36aeada3f2732583d1b113c/app/libs/gson-2.2.4.jar -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/home/labellinkdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.home.labellinkdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | public class MyApplication extends Application { 7 | 8 | public static Context sInstance; 9 | 10 | @Override 11 | public void onCreate() { 12 | super.onCreate(); 13 | sInstance = getApplicationContext(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/ScreenUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2016 JustWayward Team 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.home.labellinkdemo; 17 | 18 | import android.annotation.SuppressLint; 19 | import android.content.Context; 20 | import android.content.res.Configuration; 21 | import android.os.Build; 22 | import android.util.DisplayMetrics; 23 | import android.util.TypedValue; 24 | import android.view.Display; 25 | import android.view.WindowManager; 26 | 27 | import java.lang.reflect.Method; 28 | 29 | /** 30 | * 屏幕亮度工具类 31 | * 32 | * @author yuyh. 33 | * @date 16/4/10. 34 | */ 35 | public class ScreenUtils { 36 | 37 | public enum EScreenDensity { 38 | XXHDPI, //超高分辨率 1080×1920 39 | XHDPI, //超高分辨率 720×1280 40 | HDPI, //高分辨率 480×800 41 | MDPI, //中分辨率 320×480 42 | } 43 | 44 | public static EScreenDensity getDisply(Context context) { 45 | EScreenDensity eScreenDensity; 46 | //初始化屏幕密度 47 | DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); 48 | int densityDpi = dm.densityDpi; 49 | 50 | if (densityDpi <= 160) { 51 | eScreenDensity = EScreenDensity.MDPI; 52 | } else if (densityDpi <= 240) { 53 | eScreenDensity = EScreenDensity.HDPI; 54 | } else if (densityDpi < 400) { 55 | eScreenDensity = EScreenDensity.XHDPI; 56 | } else { 57 | eScreenDensity = EScreenDensity.XXHDPI; 58 | } 59 | return eScreenDensity; 60 | } 61 | 62 | /** 63 | * 获取屏幕宽度 64 | * 65 | * @return 66 | */ 67 | public static int getScreenWidth() { 68 | return MyApplication.sInstance.getResources().getDisplayMetrics().widthPixels; 69 | } 70 | 71 | /** 72 | * 获取屏幕高度 73 | * 74 | * @return 75 | */ 76 | public static int getScreenHeight() { 77 | String model = Build.MODEL; 78 | if (model.equals("MIX 2")) { 79 | return getDpi(MyApplication.sInstance); 80 | } else { 81 | return MyApplication.sInstance.getResources().getDisplayMetrics().heightPixels; 82 | } 83 | } 84 | 85 | public static int getDpi(Context context) { 86 | int dpi = 0; 87 | @SuppressLint("WrongConstant") WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 88 | Display display = windowManager.getDefaultDisplay(); 89 | DisplayMetrics displayMetrics = new DisplayMetrics(); 90 | @SuppressWarnings("rawtypes") 91 | Class c; 92 | try { 93 | c = Class.forName("android.view.Display"); 94 | @SuppressWarnings("unchecked") 95 | Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); 96 | method.invoke(display, displayMetrics); 97 | dpi = displayMetrics.heightPixels; 98 | } catch (Exception e) { 99 | e.printStackTrace(); 100 | } 101 | return dpi; 102 | } 103 | 104 | /** 105 | * 将dp转换成px 106 | * 107 | * @param dp 108 | * @return 109 | */ 110 | public static float dpToPx(float dp) { 111 | return dp * MyApplication.sInstance.getResources().getDisplayMetrics().density; 112 | } 113 | 114 | public static int dpToPxInt(float dp) { 115 | return (int) (dpToPx(dp) + 0.5f); 116 | } 117 | 118 | /** 119 | * 将px转换成dp 120 | * 121 | * @param px 122 | * @return 123 | */ 124 | public static float pxToDp(float px) { 125 | return px / MyApplication.sInstance.getResources().getDisplayMetrics().density; 126 | } 127 | 128 | public static int pxToDpInt(float px) { 129 | return (int) (pxToDp(px) + 0.5f); 130 | } 131 | 132 | /** 133 | * 将px值转换为sp值 134 | * 135 | * @param pxValue 136 | * @return 137 | */ 138 | public static float pxToSp(float pxValue) { 139 | return pxValue / MyApplication.sInstance.getResources().getDisplayMetrics().scaledDensity; 140 | } 141 | 142 | /** 143 | * 将sp值转换为px值 144 | * 145 | * @param spValue 146 | * @return 147 | */ 148 | public static float spToPx(float spValue) { 149 | return spValue * MyApplication.sInstance.getResources().getDisplayMetrics().scaledDensity; 150 | } 151 | 152 | public static int getStatusBarHeight(Context context) { 153 | int result = 0; 154 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 155 | if (resourceId > 0) { 156 | result = context.getResources().getDimensionPixelSize(resourceId); 157 | } 158 | return result; 159 | } 160 | 161 | public static int getActionBarSize(Context context) { 162 | TypedValue tv = new TypedValue(); 163 | if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { 164 | int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); 165 | return actionBarHeight; 166 | } 167 | return 0; 168 | } 169 | 170 | /** 171 | * 当前是否是横屏 172 | * 173 | * @param context context 174 | * @return boolean 175 | */ 176 | public static final boolean isLandscape(Context context) { 177 | return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; 178 | } 179 | 180 | /** 181 | * 当前是否是竖屏 182 | * 183 | * @param context context 184 | * @return boolean 185 | */ 186 | public static final boolean isPortrait(Context context) { 187 | return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/ToastUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2016 JustWayward Team 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.home.labellinkdemo; 17 | 18 | import android.content.Context; 19 | import android.text.TextUtils; 20 | import android.widget.Toast; 21 | 22 | /** 23 | * Toast工具类,解决多个Toast同时出现的问题 24 | * 25 | * @author yuyh. 26 | * @date 16/4/9. 27 | */ 28 | public class ToastUtils { 29 | 30 | private static Toast mToast; 31 | private static Context context = MyApplication.sInstance; 32 | 33 | public static void showSingleToast(String text) { 34 | if (!TextUtils.isEmpty(text)) { 35 | getSingleToast(text, Toast.LENGTH_SHORT).show(); 36 | } 37 | } 38 | 39 | public static Toast getSingleToast(String text, int duration) { 40 | if (mToast == null && !TextUtils.isEmpty(text)) { 41 | mToast = Toast.makeText(context, text, duration); 42 | } else { 43 | if (!TextUtils.isEmpty(text)) { 44 | mToast.setText(text); 45 | } 46 | } 47 | return mToast; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/activity/LabelLinkFlowLayoutActivity.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.LinearLayout; 10 | import android.widget.TextView; 11 | 12 | import com.google.gson.Gson; 13 | import com.home.labellinkdemo.R; 14 | import com.home.labellinkdemo.ScreenUtils; 15 | import com.home.labellinkdemo.ToastUtils; 16 | import com.zhy.view.flowlayout.FlowLayout; 17 | import com.zhy.view.flowlayout.TagAdapter; 18 | import com.zhy.view.flowlayout.TagFlowLayout; 19 | 20 | import java.util.ArrayList; 21 | import java.util.HashMap; 22 | import java.util.List; 23 | import java.util.Set; 24 | 25 | import butterknife.BindView; 26 | import butterknife.ButterKnife; 27 | 28 | public class LabelLinkFlowLayoutActivity extends AppCompatActivity { 29 | 30 | @BindView(R.id.tabLayout_man) 31 | TagFlowLayout tabLayoutMan; 32 | 33 | private Object[] strAll = new Object[]{"开始", "男人", "穿越", "军事", "悬疑", "还有谁", "仙侠", "奇幻", "竞技", 34 | "开始1", "神雕侠侣", "现言", "LOL", "婚恋", "穿越", "红尘", "灵异", "女人", "开始2", 35 | "恐怖", "蜜语", "言情", "吃鸡", "校园", "妹子", "荒岛", "其他"}; 36 | 37 | private HashMap> stringHashMap = new HashMap<>(); 38 | private List list = new ArrayList<>(); 39 | private Gson gson = new Gson(); 40 | private List list1 = new ArrayList<>(); 41 | private List list2 = new ArrayList<>(); 42 | private List list3 = new ArrayList<>(); 43 | 44 | 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_label_link_flow_layout); 49 | ButterKnife.bind(this); 50 | 51 | tabLayoutMan.setAdapter(new TagAdapter(strAll) { 52 | @Override 53 | public View getView(FlowLayout flowLayout, int i, Object s) { 54 | if (String.valueOf(s).contains("开始")) { 55 | View inflat = LayoutInflater.from(getApplicationContext()).inflate(R.layout.adapter_activity_label_title, null); 56 | ImageView adapterIv = (ImageView) inflat.findViewById(R.id.adapter_iv); 57 | TextView adapterTitle = (TextView) inflat.findViewById(R.id.adater_title); 58 | if (String.valueOf(s).equals("开始")) { 59 | adapterIv.setImageResource(R.drawable.circle); 60 | adapterTitle.setText("以下推荐男生选择"); 61 | } else if (String.valueOf(s).equals("开始1")) { 62 | adapterIv.setImageResource(R.drawable.circle_fen); 63 | adapterTitle.setText("以下推荐女生选择"); 64 | } else if (String.valueOf(s).equals("开始2")) { 65 | adapterIv.setImageResource(R.drawable.circle_bule); 66 | adapterTitle.setText("以下推荐二次元选择"); 67 | } 68 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 69 | layoutParams.width = ScreenUtils.getScreenWidth(); 70 | inflat.setLayoutParams(layoutParams); 71 | return inflat; 72 | } else { 73 | TextView tv = (TextView) LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_view_textview, 74 | tabLayoutMan, false); 75 | tv.setText(String.valueOf(s)); 76 | return tv; 77 | } 78 | } 79 | }); 80 | 81 | tabLayoutMan.setOnSelectListener(new TagFlowLayout.OnSelectListener() { 82 | @Override 83 | public void onSelected(Set set) { 84 | stringHashMap.clear(); 85 | list.clear(); 86 | list1.clear(); 87 | list2.clear(); 88 | list3.clear(); 89 | for (Integer entry : set) { 90 | if (!String.valueOf(strAll[entry]).contains("开始")) { 91 | list.add(String.valueOf(strAll[entry])); 92 | } 93 | if (entry > 0 && entry < 9) { 94 | if (!String.valueOf(strAll[entry]).contains("开始")) { 95 | list1.add(String.valueOf(strAll[entry])); 96 | } 97 | } else if (entry > 9 && entry < 18) { 98 | if (!String.valueOf(strAll[entry]).contains("开始")) { 99 | list2.add(String.valueOf(strAll[entry])); 100 | } 101 | } else { 102 | if (!String.valueOf(strAll[entry]).contains("开始")) { 103 | list3.add(String.valueOf(strAll[entry])); 104 | } 105 | } 106 | } 107 | if (list1.size() > 0) { 108 | stringHashMap.put("1", list1); 109 | } 110 | if (list2.size() > 0) { 111 | stringHashMap.put("2", list2); 112 | } 113 | if (list3.size() > 0) { 114 | stringHashMap.put("3", list3); 115 | } 116 | String json = gson.toJson(stringHashMap); 117 | ToastUtils.showSingleToast("数据: " + json); 118 | } 119 | }); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/activity/LabelLinkRecyclerActivity.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.View; 8 | import android.widget.Button; 9 | 10 | import com.google.gson.Gson; 11 | import com.home.labellinkdemo.R; 12 | import com.home.labellinkdemo.ToastUtils; 13 | import com.home.labellinkdemo.adapter.LinkLabelAdapter; 14 | import com.home.labellinkdemo.bean.LinkLabelBean; 15 | 16 | import java.util.ArrayList; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import butterknife.BindView; 22 | import butterknife.ButterKnife; 23 | import butterknife.OnClick; 24 | 25 | public class LabelLinkRecyclerActivity extends AppCompatActivity { 26 | 27 | @BindView(R.id.recyclerview) 28 | RecyclerView recyclerView; 29 | @BindView(R.id.email_sign_in_button) 30 | Button btn; 31 | 32 | private List list = new ArrayList<>(); 33 | private List list1 = new ArrayList<>(); 34 | private List list2 = new ArrayList<>(); 35 | private List list3 = new ArrayList<>(); 36 | private Gson gson = new Gson(); 37 | 38 | private String[] str = new String[]{"标题1", "帅哥", "校园", "军事", "悬疑", "科幻", "露露", "奇幻", "韦恩", "标题2", 39 | "古言", "抢女", "总裁", "网红", "穿越", "美女", "灵异", "色魔", "标题3", 40 | "恐怖", "红魔", "言情", "科幻", "诺克萨斯", "人妖", "荒岛", "其他"}; 41 | private Map> hashMap = new HashMap<>(); 42 | private LinkLabelBean linkLabelBean; 43 | 44 | @Override 45 | public void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_label_link_recycler); 48 | ButterKnife.bind(this); 49 | initData(); 50 | initView(); 51 | } 52 | 53 | private void initData() { 54 | for (int i = 0; i < str.length; i++) { 55 | linkLabelBean = new LinkLabelBean(); 56 | linkLabelBean.setStr(str[i]); 57 | list.add(linkLabelBean); 58 | } 59 | } 60 | 61 | 62 | private void initView() { 63 | final GridLayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 4); 64 | mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 65 | @Override 66 | public int getSpanSize(int position) { 67 | int type = recyclerView.getAdapter().getItemViewType(position);//获得返回值 68 | if (type == 99) { 69 | return mLayoutManager.getSpanCount(); 70 | } else { 71 | return 1; 72 | } 73 | } 74 | }); 75 | recyclerView.setLayoutManager(mLayoutManager); 76 | LinkLabelAdapter linkLabelAdapter = new LinkLabelAdapter(getApplicationContext(), list); 77 | recyclerView.setAdapter(linkLabelAdapter); 78 | } 79 | 80 | @OnClick({R.id.email_sign_in_button}) 81 | public void onClick(View view) { 82 | switch (view.getId()) { 83 | case R.id.email_sign_in_button: 84 | //这边其实可以优化 但是时间不够就不再做优化。 85 | boolean zhishao = false; 86 | for (int i = 0; i < list.size(); i++) { 87 | if (list.get(i).isSelect()) { 88 | zhishao = true; 89 | } 90 | } 91 | if (!zhishao) { 92 | ToastUtils.showSingleToast("至少选择一个标签。"); 93 | return; 94 | } 95 | hashMap.clear(); 96 | list1.clear(); 97 | list2.clear(); 98 | list3.clear(); 99 | for (int i = 0; i < list.size(); i++) { 100 | if (list.get(i).isSelect()) { 101 | if (i > 0 && i <= 8) { 102 | list1.add(list.get(i).getStr()); 103 | } else if (i > 8 && i <= 17) { 104 | list2.add(list.get(i).getStr()); 105 | } else { 106 | list3.add(list.get(i).getStr()); 107 | } 108 | } 109 | } 110 | if (list1.size() > 0) { 111 | hashMap.put("1", list1); 112 | } 113 | if (list2.size() > 0) { 114 | hashMap.put("2", list2); 115 | } 116 | if (list3.size() > 0) { 117 | hashMap.put("3", list3); 118 | } 119 | String s = gson.toJson(hashMap); 120 | ToastUtils.showSingleToast("选中数据--" + s); 121 | break; 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo.activity; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import com.home.labellinkdemo.R; 10 | 11 | import butterknife.BindView; 12 | import butterknife.ButterKnife; 13 | import butterknife.OnClick; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | 17 | @BindView(R.id.bt_gridview) 18 | Button btGridview; 19 | @BindView(R.id.bt_flowLayout) 20 | Button btFlowLayout; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | ButterKnife.bind(this); 27 | } 28 | 29 | @OnClick({R.id.bt_gridview, R.id.bt_flowLayout}) 30 | public void onClick(View view) { 31 | switch (view.getId()) { 32 | case R.id.bt_gridview: 33 | startActivity(new Intent(getApplicationContext(), LabelLinkRecyclerActivity.class)); 34 | break; 35 | case R.id.bt_flowLayout: 36 | startActivity(new Intent(getApplicationContext(), LabelLinkFlowLayoutActivity.class)); 37 | break; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/adapter/LinkLabelAdapter.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo.adapter; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.LinearLayout; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import com.home.labellinkdemo.R; 15 | import com.home.labellinkdemo.ToastUtils; 16 | import com.home.labellinkdemo.bean.LinkLabelBean; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * Created by wangtao on 2018/4/18. 22 | */ 23 | 24 | public class LinkLabelAdapter extends RecyclerView.Adapter { 25 | 26 | private Context context; 27 | private final int TITLE = 99; 28 | private final int CONTENT = 100; 29 | private int Zeng = 0; 30 | private List list; 31 | 32 | public LinkLabelAdapter(Context context, List list) { 33 | this.context = context; 34 | this.list = list; 35 | } 36 | 37 | 38 | @Override 39 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 40 | RecyclerView.ViewHolder holder = null; 41 | if (viewType == TITLE) { 42 | holder = new TitleHolder(LayoutInflater.from(context).inflate(R.layout.adapter_activity_label_title, null)); 43 | } else { 44 | holder = new LabelHolder(LayoutInflater.from(context).inflate(R.layout.adapter_activity_label_content, null)); 45 | } 46 | return holder; 47 | } 48 | 49 | @Override 50 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 51 | if (getItemViewType(position) == TITLE) { 52 | ((TitleHolder) holder).setData(position); 53 | } else { 54 | ((LabelHolder) holder).setData(position); 55 | } 56 | } 57 | 58 | @Override 59 | public int getItemCount() { 60 | return list.size(); 61 | } 62 | 63 | @Override 64 | public int getItemViewType(int position) { 65 | if (list.get(position).getStr().contains("标题")) { 66 | return TITLE; 67 | } else { 68 | return CONTENT; 69 | } 70 | } 71 | 72 | class TitleHolder extends RecyclerView.ViewHolder { 73 | 74 | private final TextView adapterTitle; 75 | private final ImageView adapterTitleIv; 76 | 77 | public TitleHolder(View inflate) { 78 | super(inflate); 79 | adapterTitle = (TextView) inflate.findViewById(R.id.adater_title); 80 | adapterTitleIv = (ImageView) inflate.findViewById(R.id.adapter_iv); 81 | } 82 | 83 | @SuppressLint("WrongConstant") 84 | public void setData(int position) { 85 | switch (list.get(position).getStr()) { 86 | case "标题1": 87 | adapterTitleIv.setImageResource(R.drawable.circle); 88 | adapterTitle.setText("以下推荐男生选择"); 89 | break; 90 | case "标题2": 91 | adapterTitleIv.setImageResource(R.drawable.circle_fen); 92 | adapterTitle.setText("以下推荐女生选择"); 93 | break; 94 | case "标题3": 95 | adapterTitleIv.setImageResource(R.drawable.circle_bule); 96 | adapterTitle.setText("以下推荐二次元选择"); 97 | break; 98 | default: 99 | adapterTitleIv.setVisibility(View.GONE); 100 | adapterTitle.setVisibility(View.GONE); 101 | break; 102 | } 103 | } 104 | } 105 | 106 | private class LabelHolder extends RecyclerView.ViewHolder { 107 | private TextView textView; 108 | private final LinearLayout llContent; 109 | 110 | public LabelHolder(View inflate) { 111 | super(inflate); 112 | textView = (TextView) inflate.findViewById(R.id.textViewContent); 113 | llContent = (LinearLayout) inflate.findViewById(R.id.llContent); 114 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 115 | layoutParams.leftMargin = 50; 116 | llContent.setLayoutParams(layoutParams); 117 | } 118 | 119 | public void setData(final int position) { 120 | textView.setText(list.get(position).getStr()); 121 | textView.setOnClickListener(new View.OnClickListener() { 122 | @Override 123 | public void onClick(View v) { 124 | if (list.get(position).isSelect()) { 125 | Zeng--; 126 | } 127 | if (Zeng < 4) { 128 | if (textView.isSelected()) { 129 | textView.setSelected(false); 130 | list.get(position).setSelect(false); 131 | } else { 132 | ++Zeng; 133 | textView.setSelected(true); 134 | list.get(position).setSelect(true); 135 | } 136 | } else { 137 | ToastUtils.showSingleToast("最多只能选择4个标签"); 138 | } 139 | } 140 | }); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /app/src/main/java/com/home/labellinkdemo/bean/LinkLabelBean.java: -------------------------------------------------------------------------------- 1 | package com.home.labellinkdemo.bean; 2 | 3 | /** 4 | * Created by wangtao on 2018/4/18. 5 | */ 6 | 7 | public class LinkLabelBean { 8 | 9 | private boolean select; 10 | 11 | private String str; 12 | 13 | public String getStr() { 14 | return str == null ? "" : str; 15 | } 16 | 17 | public void setStr(String str) { 18 | this.str = str; 19 | } 20 | 21 | public boolean isSelect() { 22 | return select; 23 | } 24 | 25 | public void setSelect(boolean select) { 26 | this.select = select; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/circle.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 14 | 17 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/circle_bule.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 14 | 17 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/circle_fen.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 14 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/circle_ret_orange.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/dialog_label.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/dialog_label_text.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/dialog_label_text_tv.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/dialog_label_tv.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/red_sold_round_gay.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/red_sold_round_sel.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_label_link_flow_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_label_link_recycler.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | 26 | 27 | 31 | 32 |