├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable │ │ │ │ └── item_bg.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── item_child.xml │ │ │ │ ├── item_normal.xml │ │ │ │ ├── item_group.xml │ │ │ │ ├── sub_edit.xml │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── zyw │ │ │ │ └── horrarndoo │ │ │ │ └── nestingrecyclerviewdemo │ │ │ │ ├── bean │ │ │ │ ├── NormalItemBean.java │ │ │ │ ├── ChildItemBean.java │ │ │ │ ├── GroupItemBean.java │ │ │ │ └── DemoItemBean.java │ │ │ │ ├── adapter │ │ │ │ ├── OnCheckChangeListener.java │ │ │ │ ├── ViewHolder │ │ │ │ │ ├── NormalViewHolder.java │ │ │ │ │ ├── ChildViewHolder.java │ │ │ │ │ └── GroupViewHolder.java │ │ │ │ └── DemoAdapter.java │ │ │ │ ├── MyApplication.java │ │ │ │ ├── utils │ │ │ │ ├── ToastUtils.java │ │ │ │ └── AppUtils.java │ │ │ │ ├── helper │ │ │ │ └── ParseHelper.java │ │ │ │ └── ui │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── zyw │ │ │ └── horrarndoo │ │ │ └── nestingrecyclerviewdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── zyw │ │ └── horrarndoo │ │ └── nestingrecyclerviewdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── assets └── GIF.gif ├── .idea ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /assets/GIF.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/assets/GIF.gif -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | NestingRecyclerViewDemo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/item_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/bean/NormalItemBean.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.bean; 2 | 3 | /** 4 | * Created by Horrarndoo on 2017/11/22. 5 | *

6 | */ 7 | 8 | public class NormalItemBean extends DemoItemBean { 9 | } 10 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #FFFAFA 8 | 9 | #ffe599 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NestingRecyclerViewDemo 2 | 使用单RecyclerView实现RecyclerView嵌套RecyclerView(购物车)效果demo,包括增删选中等演示功能 3 | 4 | ![效果图](https://raw.githubusercontent.com/Horrarndoo/NestingRecyclerViewDemo/master/assets/GIF.gif) 5 | 6 | 详细介绍: 7 | 8 | [CSDN-换种思路实现RecyclerView嵌套RecyclerView(购物车)复杂效果](http://blog.csdn.net/oQinYou/article/details/78630655) 9 | 10 | [简书-换种思路实现RecyclerView嵌套RecyclerView(购物车)复杂效果](http://www.jianshu.com/p/92bcf382d24d) 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/bean/ChildItemBean.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.bean; 2 | 3 | /** 4 | * Created by Horrarndoo on 2017/11/22. 5 | *

6 | */ 7 | 8 | public class ChildItemBean extends DemoItemBean{ 9 | private int groupId; 10 | 11 | public int getGroupId() { 12 | return groupId; 13 | } 14 | 15 | public void setGroupId(int groupId) { 16 | this.groupId = groupId; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/adapter/OnCheckChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.adapter; 2 | 3 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.bean.DemoItemBean; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by Horrarndoo on 2017/11/24. 9 | *

10 | */ 11 | 12 | public interface OnCheckChangeListener { 13 | void onCheckedChanged(Listbeans, int position, boolean isChecked, int itemType); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/bean/GroupItemBean.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.bean; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by Horrarndoo on 2017/11/22. 7 | *

8 | */ 9 | 10 | public class GroupItemBean extends DemoItemBean{ 11 | private List childs; 12 | 13 | public List getChilds() { 14 | return childs; 15 | } 16 | 17 | public void setChilds(List childs) { 18 | this.childs = childs; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/test/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Administrator\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo; 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 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zyw.horrarndoo.nestingrecyclerviewdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "26.0.2" 6 | defaultConfig { 7 | applicationId "com.zyw.horrarndoo.nestingrecyclerviewdemo" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile "com.android.support:recyclerview-v7:25.3.1" 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/bean/DemoItemBean.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.bean; 2 | 3 | /** 4 | * Created by Horrarndoo on 2017/11/22. 5 | *

6 | */ 7 | 8 | public class DemoItemBean { 9 | public static final int TYPE_NORMAL = 0; 10 | public static final int TYPE_GROUP = 1; 11 | public static final int TYPE_CHILD = 2; 12 | 13 | private String title; 14 | private boolean isChecked; 15 | private int itemType; 16 | private int itemId; 17 | 18 | public boolean isChecked() { 19 | return isChecked; 20 | } 21 | 22 | public void setChecked(boolean checked) { 23 | isChecked = checked; 24 | } 25 | 26 | public int getItemId() { 27 | return itemId; 28 | } 29 | 30 | public void setItemId(int itemId) { 31 | this.itemId = itemId; 32 | } 33 | 34 | public String getTitle() { 35 | return title; 36 | } 37 | 38 | public void setTitle(String title) { 39 | this.title = title; 40 | } 41 | 42 | public int getItemType() { 43 | return itemType; 44 | } 45 | 46 | public void setItemType(int itemType) { 47 | this.itemType = itemType; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.os.Handler; 6 | 7 | /** 8 | * Created by Horrarndoo on 2017/11/24. 9 | *

10 | */ 11 | 12 | public class MyApplication extends Application{ 13 | protected static Context context; 14 | protected static Handler handler; 15 | protected static int mainThreadId; 16 | 17 | @Override 18 | public void onCreate() { 19 | super.onCreate(); 20 | context = getApplicationContext(); 21 | handler = new Handler(); 22 | mainThreadId = android.os.Process.myTid(); 23 | } 24 | 25 | /** 26 | * 获取上下文对象 27 | * 28 | * @return context 29 | */ 30 | public static Context getContext() { 31 | return context; 32 | } 33 | 34 | /** 35 | * 获取全局handler 36 | * 37 | * @return 全局handler 38 | */ 39 | public static Handler getHandler() { 40 | return handler; 41 | } 42 | 43 | /** 44 | * 获取主线程id 45 | * 46 | * @return 主线程id 47 | */ 48 | public static int getMainThreadId() { 49 | return mainThreadId; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_child.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 20 | 21 | 29 | 30 | 31 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_normal.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | 23 | 24 | 30 | 31 | 32 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_group.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | 19 | 20 | 28 | 29 | 30 | 34 | 35 | 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/adapter/ViewHolder/NormalViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.adapter.ViewHolder; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | import android.widget.CheckBox; 6 | import android.widget.LinearLayout; 7 | import android.widget.TextView; 8 | 9 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.R; 10 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.bean.NormalItemBean; 11 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.utils.ToastUtils; 12 | 13 | /** 14 | * Created by Horrarndoo on 2017/11/24. 15 | *

16 | */ 17 | 18 | public class NormalViewHolder extends RecyclerView.ViewHolder { 19 | private NormalItemBean bean; 20 | public TextView tvNormal; 21 | public LinearLayout llNormal; 22 | public CheckBox cbNormal; 23 | 24 | public NormalViewHolder(View itemView) { 25 | super(itemView); 26 | tvNormal = (TextView) itemView.findViewById(R.id.tv_normal); 27 | llNormal = (LinearLayout) itemView.findViewById(R.id.ll_normal); 28 | cbNormal = (CheckBox) itemView.findViewById(R.id.cb_normal); 29 | llNormal.setOnClickListener(new OnClickListener()); 30 | } 31 | 32 | /** 33 | * 绑定item数据 34 | * @param bean item数据 35 | */ 36 | public void bindData(NormalItemBean bean){ 37 | this.bean = bean; 38 | } 39 | 40 | private class OnClickListener implements View.OnClickListener { 41 | @Override 42 | public void onClick(View v) { 43 | switch (v.getId()) { 44 | case R.id.ll_normal: 45 | ToastUtils.showToast(bean.getTitle() + " is clicked."); 46 | break; 47 | } 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/adapter/ViewHolder/ChildViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.adapter.ViewHolder; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | import android.widget.CheckBox; 6 | import android.widget.LinearLayout; 7 | import android.widget.TextView; 8 | 9 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.R; 10 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.bean.ChildItemBean; 11 | import com.zyw.horrarndoo.nestingrecyclerviewdemo.utils.ToastUtils; 12 | 13 | /** 14 | * Created by Horrarndoo on 2017/11/24. 15 | *

16 | */ 17 | 18 | public class ChildViewHolder extends RecyclerView.ViewHolder { 19 | private ChildItemBean bean; 20 | public TextView tvChild; 21 | public CheckBox cbChild; 22 | public LinearLayout llChild; 23 | 24 | public ChildViewHolder(View itemView) { 25 | super(itemView); 26 | 27 | tvChild = (TextView) itemView.findViewById(R.id.tv_child); 28 | cbChild = (CheckBox) itemView.findViewById(R.id.cb_child); 29 | llChild = (LinearLayout) itemView.findViewById(R.id.ll_child); 30 | 31 | llChild.setOnClickListener(new OnClickListener()); 32 | } 33 | 34 | /** 35 | * 绑定item数据 36 | * 37 | * @param bean item数据 38 | */ 39 | public void bindData(ChildItemBean bean) { 40 | this.bean = bean; 41 | } 42 | 43 | private class OnClickListener implements View.OnClickListener { 44 | @Override 45 | public void onClick(View v) { 46 | switch (v.getId()) { 47 | case R.id.ll_child: 48 | ToastUtils.showToast(bean.getTitle() + " is clicked."); 49 | break; 50 | } 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /app/src/main/java/com/zyw/horrarndoo/nestingrecyclerviewdemo/utils/ToastUtils.java: -------------------------------------------------------------------------------- 1 | package com.zyw.horrarndoo.nestingrecyclerviewdemo.utils; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | /** 7 | * Created by Horrarndoo on 2017/4/5. 8 | *

9 | * toast工具类封装 10 | */ 11 | public class ToastUtils { 12 | private static Toast mToast = null; 13 | 14 | /** 15 | * 显示一个toast提示 16 | * 17 | * @param resouceId toast字符串资源id 18 | */ 19 | public static void showToast(int resouceId) { 20 | showToast(AppUtils.getString(resouceId)); 21 | } 22 | 23 | /** 24 | * 显示一个toast提示 25 | * 26 | * @param text toast字符串 27 | */ 28 | public static void showToast(String text) { 29 | showToast(text, Toast.LENGTH_SHORT); 30 | } 31 | 32 | /** 33 | * 显示一个toast提示 34 | * 35 | * @param text toast字符串 36 | * @param duration toast显示时间 37 | */ 38 | public static void showToast(String text, int duration) { 39 | showToast(AppUtils.getContext(), text, duration); 40 | } 41 | 42 | /** 43 | * 显示一个toast提示 44 | * 45 | * @param context context 上下文对象 46 | * @param text toast字符串 47 | * @param duration toast显示时间 48 | */ 49 | public static void showToast(final Context context, final String text, final int duration) { 50 | /** 51 | * 保证运行在主线程 52 | */ 53 | AppUtils.runOnUIThread(new Runnable() { 54 | @Override 55 | public void run() { 56 | if (mToast == null) { 57 | mToast = Toast.makeText(context, text, duration); 58 | } else { 59 | mToast.setText(text); 60 | mToast.setDuration(duration); 61 | } 62 | mToast.show(); 63 | } 64 | }); 65 | } 66 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/sub_edit.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 17 | 18 | 22 | 23 | 32 | 33 | 37 | 38 | 47 | 48 | 52 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 18 | 24 | 25 |