├── 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 │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── activity_sm.xml │ │ │ │ ├── activity_sme.xml │ │ │ │ ├── item.xml │ │ │ │ ├── activity_smr.xml │ │ │ │ ├── activity_smre.xml │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── chy │ │ │ │ └── swipemenuandpulltorefresh │ │ │ │ ├── Entity │ │ │ │ └── StringEntity.java │ │ │ │ ├── adapter │ │ │ │ ├── StringDataAdapter.java │ │ │ │ └── StringDataExpandAdapter.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── ExpandViewActivity │ │ │ │ ├── SMExpandListViewActivity.java │ │ │ │ ├── RExpandListViewActivity.java │ │ │ │ ├── SMExpandListViewActivity2.java │ │ │ │ ├── SMRExpandListViewActivity.java │ │ │ │ └── SMRExpandListViewActivity2.java │ │ │ │ └── ListViewAcitvity │ │ │ │ ├── SMListViewActivity.java │ │ │ │ ├── SMRListViewActivity.java │ │ │ │ ├── RListViewActivity.java │ │ │ │ ├── SMListViewActivity2.java │ │ │ │ └── SMRListViewActivity2.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── chy │ │ │ └── swipemenuandpulltorefresh │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── chy │ │ └── swipemenuandpulltorefresh │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── srlibrary ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ ├── loading.png │ │ │ │ ├── load_failed.png │ │ │ │ ├── refreshing.png │ │ │ │ ├── load_succeed.png │ │ │ │ ├── pull_icon_big.png │ │ │ │ ├── pullup_icon_big.png │ │ │ │ ├── refresh_failed.png │ │ │ │ └── refresh_succeed.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ └── strings.xml │ │ │ ├── anim │ │ │ │ ├── rotating.xml │ │ │ │ └── reverse_anim.xml │ │ │ └── layout │ │ │ │ ├── refresh_head.xml │ │ │ │ └── refresh_load.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── chy │ │ │ │ └── srlibrary │ │ │ │ ├── interfaceutil │ │ │ │ ├── SwipeMenuCreatorInterfaceUtil.java │ │ │ │ └── PullableUtil.java │ │ │ │ ├── SwipeMenu.java │ │ │ │ ├── slistview │ │ │ │ ├── SMRListView.java │ │ │ │ ├── SMView.java │ │ │ │ ├── SMAdapter.java │ │ │ │ ├── SMListView.java │ │ │ │ └── SMLayout.java │ │ │ │ ├── expandview │ │ │ │ ├── SMRExpandView.java │ │ │ │ ├── SMExpandView.java │ │ │ │ ├── SMExpandableAdapter.java │ │ │ │ ├── SMLayoutView.java │ │ │ │ └── SMExpandableView.java │ │ │ │ ├── SwipeMenuItem.java │ │ │ │ └── PTRLayoutView.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── chy │ │ │ └── srlibrary │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── chy │ │ └── srlibrary │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── ScreenShot ├── 1.gif ├── 2.gif └── 3.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── gradlew ├── README.md └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /srlibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':srlibrary' 2 | -------------------------------------------------------------------------------- /ScreenShot/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/ScreenShot/1.gif -------------------------------------------------------------------------------- /ScreenShot/2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/ScreenShot/2.gif -------------------------------------------------------------------------------- /ScreenShot/3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/ScreenShot/3.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SwipeMenuAndPullToRefresh 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/loading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/loading.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/load_failed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/load_failed.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/refreshing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/refreshing.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/load_succeed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/load_succeed.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/pull_icon_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/pull_icon_big.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/pullup_icon_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/pullup_icon_big.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/refresh_failed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/refresh_failed.png -------------------------------------------------------------------------------- /srlibrary/src/main/res/drawable/refresh_succeed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heynchy/SwipeMenuAndPullToRefresh/HEAD/srlibrary/src/main/res/drawable/refresh_succeed.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | /.idea 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/interfaceutil/SwipeMenuCreatorInterfaceUtil.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.interfaceutil; 2 | 3 | 4 | import com.chy.srlibrary.SwipeMenu; 5 | 6 | public interface SwipeMenuCreatorInterfaceUtil { 7 | 8 | void create(SwipeMenu menu); 9 | } 10 | -------------------------------------------------------------------------------- /srlibrary/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #FFFFFF 7 | #eeeeee 8 | 9 | -------------------------------------------------------------------------------- /srlibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #FFFFFF 7 | #000000 8 | #808080 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /srlibrary/src/main/res/anim/rotating.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | -------------------------------------------------------------------------------- /srlibrary/src/main/res/anim/reverse_anim.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /srlibrary/src/test/java/com/chy/srlibrary/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary; 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 | } -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/interfaceutil/PullableUtil.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.interfaceutil; 2 | 3 | /** 4 | * PullableUtil 5 | * 6 | * @author CHY 2016/7/25. 7 | * 刷新接口 8 | */ 9 | public interface PullableUtil 10 | { 11 | /** 12 | * 判断是否可以下拉,如果不需要下拉功能可以直接return false 13 | * 14 | * @return true如果可以下拉否则返回false 15 | */ 16 | boolean canPullDown(); 17 | 18 | /** 19 | * 判断是否可以上拉,如果不需要上拉功能可以直接return false 20 | * 21 | * @return true如果可以上拉否则返回false 22 | */ 23 | boolean canPullUp(); 24 | } 25 | -------------------------------------------------------------------------------- /app/src/test/java/com/chy/swipemenuandpulltorefresh/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh; 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 | } -------------------------------------------------------------------------------- /srlibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SwipeAndRefreshLibrary 3 | 4 | 下拉刷新 5 | 释放立即刷新 6 | 正在刷新... 7 | 刷新成功 8 | 刷新失败 9 | 上拉加载更多 10 | 释放立即加载 11 | 正在加载... 12 | 加载成功 13 | 加载失败 14 | 15 | -------------------------------------------------------------------------------- /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 D:\Android Studio\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 | -------------------------------------------------------------------------------- /srlibrary/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 D:\Android Studio\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 | -------------------------------------------------------------------------------- /srlibrary/src/androidTest/java/com/chy/srlibrary/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary; 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.chy.srlibrary.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_sm.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Wed May 03 18:02:50 CST 2017 16 | systemProp.http.proxyHost=mirrors.neusoft.edu.cn 17 | org.gradle.jvmargs=-Xmx1536m 18 | systemProp.http.proxyPort=80 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_sme.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/chy/swipemenuandpulltorefresh/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh; 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.chy.swipemenuandpulltorefresh", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/Entity/StringEntity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.Entity; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * StringEntity 7 | * 8 | * @author lenovo 2017/5/4. 9 | * Function Describe 10 | * @modify lenovo 2017/5/4. 11 | * Function Describe 12 | */ 13 | public class StringEntity { 14 | private String title; 15 | private List childListStr; 16 | 17 | public StringEntity(String title, List childListStr) { 18 | this.title = title; 19 | this.childListStr = childListStr; 20 | } 21 | 22 | public String getTitle() { 23 | return title; 24 | } 25 | 26 | public void setTitle(String title) { 27 | this.title = title; 28 | } 29 | 30 | public List getChildListStr() { 31 | return childListStr; 32 | } 33 | 34 | public void setChildListStr(List childListStr) { 35 | this.childListStr = childListStr; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /srlibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | group='com.github.heynchy' 4 | android { 5 | compileSdkVersion 23 6 | buildToolsVersion "25.0.2" 7 | 8 | defaultConfig { 9 | minSdkVersion 16 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | compile 'com.android.support:appcompat-v7:23.0.1' 31 | testCompile 'junit:junit:4.12' 32 | } 33 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/SwipeMenu.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary; 2 | 3 | import android.content.Context; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class SwipeMenu { 9 | 10 | private Context mContext; 11 | private List mItems; 12 | private int mViewType; 13 | 14 | public SwipeMenu(Context context) { 15 | mContext = context; 16 | mItems = new ArrayList(); 17 | } 18 | 19 | public Context getContext() { 20 | return mContext; 21 | } 22 | 23 | public void addMenuItem(SwipeMenuItem item) { 24 | mItems.add(item); 25 | } 26 | 27 | public void removeMenuItem(SwipeMenuItem item) { 28 | mItems.remove(item); 29 | } 30 | 31 | public List getMenuItems() { 32 | return mItems; 33 | } 34 | 35 | public SwipeMenuItem getMenuItem(int index) { 36 | return mItems.get(index); 37 | } 38 | 39 | public int getViewType() { 40 | return mViewType; 41 | } 42 | 43 | public void setViewType(int viewType) { 44 | this.mViewType = viewType; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.2" 6 | defaultConfig { 7 | applicationId "com.chy.swipemenuandpulltorefresh" 8 | minSdkVersion 16 9 | targetSdkVersion 29 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:26.0.2' 28 | testCompile 'junit:junit:4.12' 29 | // compile 'com.github.heynchy:SwipeMenuAndPullToRefresh:0.1.6' 30 | compile project(path: ':srlibrary') 31 | } 32 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/slistview/SMRListView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.slistview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | 6 | import com.chy.srlibrary.interfaceutil.PullableUtil; 7 | 8 | 9 | /** 10 | * PullToRefreshAndSwipListView 11 | * 12 | * @author CHY 2016/11/14. 13 | */ 14 | public class SMRListView extends SMListView implements PullableUtil { 15 | public SMRListView(Context context) { 16 | super(context); 17 | } 18 | 19 | public SMRListView(Context context, AttributeSet attrs, int defStyle) { 20 | super(context, attrs, defStyle); 21 | } 22 | 23 | public SMRListView(Context context, AttributeSet attrs) { 24 | super(context, attrs); 25 | } 26 | 27 | @Override 28 | public boolean canPullDown() { 29 | if (getCount() == 0) { 30 | // 没有item的时候也可以下拉刷新 31 | return true; 32 | } else if (getFirstVisiblePosition() == 0 33 | && (null != getChildAt(0)) && (getChildAt(0).getTop() >= 0)) { 34 | // 滑到ListView的顶部了 35 | return true; 36 | } else { 37 | return false; 38 | } 39 | // return false; 40 | } 41 | 42 | @Override 43 | public boolean canPullUp() { 44 | if (getLastVisiblePosition() == (getCount() - 1)) { 45 | // 滑到底部了 46 | if ((null != getChildAt(getLastVisiblePosition() - getFirstVisiblePosition())) 47 | && (getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()).getBottom() 48 | == getMeasuredHeight())) 49 | return true; 50 | } 51 | return false; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_smr.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | /> 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/expandview/SMRExpandView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.expandview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | 6 | import com.chy.srlibrary.interfaceutil.PullableUtil; 7 | 8 | 9 | /** 10 | * PullToRefreshAndSwipeMenuListView 11 | * 12 | * @author 2016/7/27. 13 | * 融合下拉刷新,上拉加载和侧滑 14 | */ 15 | public class SMRExpandView extends SMExpandableView implements PullableUtil { 16 | 17 | public SMRExpandView(Context context) { 18 | super(context); 19 | } 20 | 21 | public SMRExpandView(Context context, AttributeSet attrs, int defStyle) { 22 | super(context, attrs, defStyle); 23 | } 24 | 25 | public SMRExpandView(Context context, AttributeSet attrs) { 26 | super(context, attrs); 27 | } 28 | 29 | @Override 30 | public boolean canPullDown() { 31 | if (getCount() == 0) { 32 | // 没有item的时候也可以下拉刷新 33 | return true; 34 | } else if (getFirstVisiblePosition() == 0 35 | && (null != getChildAt(0)) && (getChildAt(0).getTop() >= 0)) { 36 | // 滑到ListView的顶部了 37 | return true; 38 | } else { 39 | return false; 40 | } 41 | } 42 | 43 | @Override 44 | public boolean canPullUp() { 45 | if (getLastVisiblePosition() == (getCount() - 1)) { 46 | // 滑到底部了 47 | if ((null != getChildAt(getLastVisiblePosition() - getFirstVisiblePosition())) 48 | && (getChildAt(getLastVisiblePosition() - getFirstVisiblePosition()).getBottom() 49 | == getMeasuredHeight())) 50 | return true; 51 | } 52 | return false; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_smre.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | /> 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/adapter/StringDataAdapter.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.adapter; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.TextView; 9 | 10 | 11 | import com.chy.swipemenuandpulltorefresh.R; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * StringDataAdapter 17 | * 18 | * @author lenovo 2017/5/2. 19 | */ 20 | public class StringDataAdapter extends BaseAdapter { 21 | private Context context; 22 | private List dataList; 23 | 24 | public StringDataAdapter(Context context, List dataList) { 25 | this.context = context; 26 | this.dataList = dataList; 27 | } 28 | 29 | @Override 30 | public int getCount() { 31 | return dataList.size(); 32 | } 33 | 34 | @Override 35 | public Object getItem(int position) { 36 | return dataList.get(position); 37 | } 38 | 39 | @Override 40 | public long getItemId(int position) { 41 | return position; 42 | } 43 | 44 | @Override 45 | public View getView(int position, View convertView, ViewGroup parent) { 46 | String str = dataList.get(position); 47 | ViewHolder viewHolder; 48 | if (convertView == null) { 49 | convertView = LayoutInflater.from(context).inflate(R.layout.item, null); 50 | viewHolder = new ViewHolder(); 51 | viewHolder.textView = (TextView) convertView.findViewById(R.id.text); 52 | convertView.setTag(viewHolder); 53 | } else { 54 | viewHolder = (ViewHolder) convertView.getTag(); 55 | } 56 | viewHolder.textView.setText(str); 57 | return convertView; 58 | } 59 | 60 | private class ViewHolder { 61 | TextView textView; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/SwipeMenuItem.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary; 2 | 3 | 4 | import android.content.Context; 5 | import android.graphics.drawable.Drawable; 6 | 7 | public class SwipeMenuItem { 8 | 9 | private int id; 10 | private Context mContext; 11 | private String title; 12 | private Drawable icon; 13 | private Drawable background; 14 | private int titleColor; 15 | private int titleSize; 16 | private int width; 17 | 18 | public SwipeMenuItem(Context context) { 19 | mContext = context; 20 | } 21 | 22 | public int getId() { 23 | return id; 24 | } 25 | 26 | public void setId(int id) { 27 | this.id = id; 28 | } 29 | 30 | public int getTitleColor() { 31 | return titleColor; 32 | } 33 | 34 | public int getTitleSize() { 35 | return titleSize; 36 | } 37 | 38 | public void setTitleSize(int titleSize) { 39 | this.titleSize = titleSize; 40 | } 41 | 42 | public void setTitleColor(int titleColor) { 43 | this.titleColor = titleColor; 44 | } 45 | 46 | public String getTitle() { 47 | return title; 48 | } 49 | 50 | public void setTitle(String title) { 51 | this.title = title; 52 | } 53 | 54 | public void setTitle(int resId) { 55 | setTitle(mContext.getString(resId)); 56 | } 57 | 58 | public Drawable getIcon() { 59 | return icon; 60 | } 61 | 62 | public void setIcon(Drawable icon) { 63 | this.icon = icon; 64 | } 65 | 66 | public void setIcon(int resId) { 67 | this.icon = mContext.getResources().getDrawable(resId); 68 | } 69 | 70 | public Drawable getBackground() { 71 | return background; 72 | } 73 | 74 | public void setBackground(Drawable background) { 75 | this.background = background; 76 | } 77 | 78 | public void setBackground(int resId) { 79 | this.background = mContext.getResources().getDrawable(resId); 80 | } 81 | 82 | public int getWidth() { 83 | return width; 84 | } 85 | 86 | public void setWidth(int width) { 87 | this.width = width; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /srlibrary/src/main/res/layout/refresh_head.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 14 | 15 | 19 | 20 | 27 | 28 | 36 | 37 | 45 | 46 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /srlibrary/src/main/res/layout/refresh_load.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 21 | 22 | 29 | 30 | 38 | 39 | 47 | 48 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/slistview/SMView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.slistview; 2 | 3 | import android.text.TextUtils; 4 | import android.view.Gravity; 5 | import android.view.View; 6 | import android.widget.ImageView; 7 | import android.widget.LinearLayout; 8 | import android.widget.TextView; 9 | 10 | 11 | import com.chy.srlibrary.SwipeMenu; 12 | import com.chy.srlibrary.SwipeMenuItem; 13 | 14 | import java.util.List; 15 | 16 | 17 | /** 18 | * SwipeMenuView 19 | * 20 | * @author CHY 2016/11/8. 21 | */ 22 | public class SMView extends LinearLayout implements View.OnClickListener { 23 | 24 | private SMListView mListView; 25 | private SMLayout mLayout; 26 | private SwipeMenu mMenu; 27 | private SMView.OnSwipeItemClickListener onItemClickListener; 28 | private int position; 29 | 30 | public int getPosition() { 31 | return position; 32 | } 33 | 34 | public void setPosition(int position) { 35 | this.position = position; 36 | } 37 | 38 | public SMView(SwipeMenu menu, SMListView listView) { 39 | super(menu.getContext()); 40 | mListView = listView; 41 | mMenu = menu; 42 | List items = menu.getMenuItems(); 43 | int id = 0; 44 | for (SwipeMenuItem item : items) { 45 | addItem(item, id++); 46 | } 47 | } 48 | 49 | private void addItem(SwipeMenuItem item, int id) { 50 | LayoutParams params = new LayoutParams(item.getWidth(), 51 | LayoutParams.MATCH_PARENT); 52 | LinearLayout parent = new LinearLayout(getContext()); 53 | parent.setId(id); 54 | parent.setGravity(Gravity.CENTER); 55 | parent.setOrientation(LinearLayout.VERTICAL); 56 | parent.setLayoutParams(params); 57 | parent.setBackgroundDrawable(item.getBackground()); 58 | parent.setOnClickListener(this); 59 | addView(parent); 60 | 61 | if (item.getIcon() != null) { 62 | parent.addView(createIcon(item)); 63 | } 64 | if (!TextUtils.isEmpty(item.getTitle())) { 65 | parent.addView(createTitle(item)); 66 | } 67 | 68 | } 69 | 70 | private ImageView createIcon(SwipeMenuItem item) { 71 | ImageView iv = new ImageView(getContext()); 72 | iv.setImageDrawable(item.getIcon()); 73 | return iv; 74 | } 75 | 76 | private TextView createTitle(SwipeMenuItem item) { 77 | TextView tv = new TextView(getContext()); 78 | tv.setText(item.getTitle()); 79 | tv.setGravity(Gravity.CENTER); 80 | tv.setTextSize(item.getTitleSize()); 81 | tv.setTextColor(item.getTitleColor()); 82 | return tv; 83 | } 84 | 85 | @Override 86 | public void onClick(View v) { 87 | if (onItemClickListener != null && mLayout.isOpen()) { 88 | onItemClickListener.onItemClick(this, mMenu, v.getId()); 89 | } 90 | } 91 | 92 | public OnSwipeItemClickListener getOnSwipeItemClickListener() { 93 | return onItemClickListener; 94 | } 95 | 96 | public void setOnSwipeItemClickListener(OnSwipeItemClickListener onItemClickListener) { 97 | this.onItemClickListener = onItemClickListener; 98 | } 99 | 100 | public void setLayout(SMLayout mLayout) { 101 | this.mLayout = mLayout; 102 | } 103 | 104 | public static interface OnSwipeItemClickListener { 105 | void onItemClick(SMView view, SwipeMenu menu, int index); 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 26 | 27 | 38 | 39 | 50 | 51 | 62 | 63 | 74 | 75 | 86 | 87 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/expandview/SMExpandView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.expandview; 2 | 3 | import android.text.TextUtils; 4 | import android.view.Gravity; 5 | import android.view.View; 6 | import android.view.View.OnClickListener; 7 | import android.widget.ImageView; 8 | import android.widget.LinearLayout; 9 | import android.widget.TextView; 10 | 11 | 12 | import com.chy.srlibrary.SwipeMenu; 13 | import com.chy.srlibrary.SwipeMenuItem; 14 | 15 | import java.util.List; 16 | 17 | 18 | /** 19 | * SwipeMenuLayoutView 20 | * 21 | * @author CHY 2016/7/29. 22 | * 处理侧滑菜单的选项 23 | */ 24 | public class SMExpandView extends LinearLayout implements OnClickListener { 25 | 26 | private SMExpandableView mListView; 27 | private SMLayoutView mLayout; 28 | private SwipeMenu mMenu; 29 | private OnSwipeItemClickListener onItemClickListener; 30 | private int mPosition; 31 | 32 | public int getPosition() { 33 | return mPosition; 34 | } 35 | 36 | public void setPosition(int mPosition) { 37 | this.mPosition = mPosition; 38 | } 39 | 40 | public SMExpandView(SwipeMenu menu, SMExpandableView listView) { 41 | super(menu.getContext()); 42 | mListView = listView; 43 | mMenu = menu; 44 | List items = menu.getMenuItems(); 45 | int id = 0; 46 | for (SwipeMenuItem item : items) { 47 | addItem(item, id++); 48 | } 49 | } 50 | 51 | private void addItem(SwipeMenuItem item, int id) { 52 | LayoutParams params = new LayoutParams(item.getWidth(), 53 | LayoutParams.MATCH_PARENT); 54 | LinearLayout parent = new LinearLayout(getContext()); 55 | parent.setId(id); 56 | parent.setGravity(Gravity.CENTER); 57 | parent.setOrientation(LinearLayout.VERTICAL); 58 | parent.setLayoutParams(params); 59 | parent.setBackgroundDrawable(item.getBackground()); 60 | parent.setOnClickListener(this); 61 | 62 | addView(parent); 63 | 64 | if (item.getIcon() != null) { 65 | parent.addView(createIcon(item)); 66 | } 67 | if (!TextUtils.isEmpty(item.getTitle())) { 68 | parent.addView(createTitle(item)); 69 | } 70 | 71 | } 72 | 73 | private ImageView createIcon(SwipeMenuItem item) { 74 | ImageView iv = new ImageView(getContext()); 75 | iv.setImageDrawable(item.getIcon()); 76 | return iv; 77 | } 78 | 79 | private TextView createTitle(SwipeMenuItem item) { 80 | TextView tv = new TextView(getContext()); 81 | tv.setText(item.getTitle()); 82 | tv.setGravity(Gravity.CENTER); 83 | tv.setTextSize(item.getTitleSize()); 84 | tv.setTextColor(item.getTitleColor()); 85 | return tv; 86 | } 87 | 88 | @Override 89 | public void onClick(View v) { 90 | if (onItemClickListener != null && mLayout.isOpen()) { 91 | onItemClickListener.onItemClick(this, mMenu, v.getId()); 92 | } 93 | } 94 | 95 | public OnSwipeItemClickListener getOnSwipeItemClickListener() { 96 | return onItemClickListener; 97 | } 98 | 99 | public void setOnSwipeItemClickListener(OnSwipeItemClickListener onItemClickListener) { 100 | this.onItemClickListener = onItemClickListener; 101 | } 102 | 103 | public void setLayout(SMLayoutView mLayout) { 104 | this.mLayout = mLayout; 105 | } 106 | 107 | 108 | public static interface OnSwipeItemClickListener { 109 | void onItemClick(SMExpandView view, SwipeMenu menu, int index); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.TextView; 9 | 10 | import com.chy.swipemenuandpulltorefresh.ExpandViewActivity.RExpandListViewActivity; 11 | import com.chy.swipemenuandpulltorefresh.ExpandViewActivity.SMExpandListViewActivity; 12 | import com.chy.swipemenuandpulltorefresh.ExpandViewActivity.SMRExpandListViewActivity; 13 | import com.chy.swipemenuandpulltorefresh.ListViewAcitvity.RListViewActivity; 14 | import com.chy.swipemenuandpulltorefresh.ListViewAcitvity.SMListViewActivity; 15 | import com.chy.swipemenuandpulltorefresh.ListViewAcitvity.SMRListViewActivity; 16 | 17 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 18 | 19 | private TextView mTv1; 20 | private TextView mTv2; 21 | private TextView mTv3; 22 | private TextView mTv4; 23 | private TextView mTv5; 24 | private TextView mTv6; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | initView(); 31 | } 32 | 33 | @Override 34 | public void onClick(View v) { 35 | switch (v.getId()) { 36 | case R.id.lsr_tv: 37 | // listView的侧滑删除,上拉加载和下拉刷新 38 | Intent intent0 = new Intent(MainActivity.this, SMRListViewActivity.class); 39 | startActivity(intent0); 40 | break; 41 | case R.id.ls_tv: 42 | // listView的侧滑删除 43 | Intent intent1 = new Intent(MainActivity.this, SMListViewActivity.class); 44 | startActivity(intent1); 45 | break; 46 | case R.id.lr_tv: 47 | // listView的上拉加载和下拉刷新 48 | Intent intent2 = new Intent(MainActivity.this, RListViewActivity.class); 49 | startActivity(intent2); 50 | break; 51 | case R.id.esr_tv: 52 | // expandablelistView的侧滑删除,上拉加载和下拉刷新 53 | Intent intent3 = new Intent(MainActivity.this, SMRExpandListViewActivity.class); 54 | startActivity(intent3); 55 | break; 56 | case R.id.es_tv: 57 | // expandablelistView的侧滑删除 58 | Intent intent4 = new Intent(MainActivity.this, SMExpandListViewActivity.class); 59 | startActivity(intent4); 60 | break; 61 | case R.id.er_tv: 62 | // expandablelistView的上拉加载和下拉刷新 63 | Intent intent5 = new Intent(MainActivity.this, RExpandListViewActivity.class); 64 | startActivity(intent5); 65 | break; 66 | } 67 | } 68 | 69 | 70 | private void initView() { 71 | mTv1 = (TextView) findViewById(R.id.lsr_tv); 72 | mTv2 = (TextView) findViewById(R.id.ls_tv); 73 | mTv3 = (TextView) findViewById(R.id.lr_tv); 74 | mTv4 = (TextView) findViewById(R.id.esr_tv); 75 | mTv5 = (TextView) findViewById(R.id.er_tv); 76 | mTv6 = (TextView) findViewById(R.id.es_tv); 77 | mTv1.setOnClickListener(this); 78 | mTv2.setOnClickListener(this); 79 | mTv3.setOnClickListener(this); 80 | mTv4.setOnClickListener(this); 81 | mTv5.setOnClickListener(this); 82 | mTv6.setOnClickListener(this); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ExpandViewActivity/SMExpandListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ExpandViewActivity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.expandview.SMExpandableView; 16 | import com.chy.srlibrary.expandview.SMRExpandView; 17 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 18 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 19 | import com.chy.swipemenuandpulltorefresh.R; 20 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataExpandAdapter; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * ExpandListView 父条目侧滑删除 27 | */ 28 | public class SMExpandListViewActivity extends AppCompatActivity { 29 | 30 | private SMRExpandView mSMRExpandView; // 侧滑ExpandablelistView 31 | private StringDataExpandAdapter mAdapter; 32 | private List mDataList; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_sme); 38 | initView(); 39 | mAdapter = new StringDataExpandAdapter(this, mDataList); 40 | mSMRExpandView.setAdapter(mAdapter); 41 | 42 | // 设置侧滑的选项 43 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 44 | @Override 45 | public void create(SwipeMenu menu) { 46 | 47 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 48 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 49 | 0x3F, 0x25))); 50 | deleteItem.setWidth(dp2px(60)); 51 | deleteItem.setTitle("删除"); 52 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 53 | deleteItem.setTitleSize(15); 54 | menu.addMenuItem(deleteItem); 55 | } 56 | }; 57 | mSMRExpandView.setMenuCreator(creator); 58 | 59 | // 侧滑的监听事件 60 | mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 61 | @Override 62 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 63 | mDataList.remove(i); 64 | mAdapter.notifyDataSetChanged(); 65 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 66 | return false; 67 | } 68 | }); 69 | 70 | } 71 | 72 | 73 | private void initView() { 74 | mSMRExpandView = (SMRExpandView) findViewById(R.id.elv_swipe_menu); 75 | 76 | // 初始化测试数据 77 | mDataList = new ArrayList<>(); 78 | List childList = new ArrayList<>(); 79 | for (int i = 0; i < 4; i++) { 80 | childList.add("小标题: " + i); 81 | } 82 | for (int i = 0; i < 20; i++) { 83 | mDataList.add(new StringEntity("大标题:" + i, childList)); 84 | } 85 | } 86 | 87 | /** 88 | * dp与px之间的转换 89 | * 90 | * @param dp 91 | * @return 92 | */ 93 | private int dp2px(int dp) { 94 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 95 | getResources().getDisplayMetrics()); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ListViewAcitvity/SMListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ListViewAcitvity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | import android.util.TypedValue; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.widget.AdapterView; 12 | import android.widget.TextView; 13 | import android.widget.Toast; 14 | 15 | import com.chy.srlibrary.PTRLayoutView; 16 | import com.chy.srlibrary.SwipeMenu; 17 | import com.chy.srlibrary.SwipeMenuItem; 18 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 19 | import com.chy.srlibrary.slistview.SMListView; 20 | import com.chy.srlibrary.slistview.SMRListView; 21 | import com.chy.swipemenuandpulltorefresh.R; 22 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * listView 的侧滑删除 29 | */ 30 | public class SMListViewActivity extends AppCompatActivity { 31 | 32 | private SMRListView mSWRListView; // 侧滑listView 33 | private StringDataAdapter mAdapter; 34 | private List mDataList; 35 | 36 | @Override 37 | protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_sm); 40 | initView(); 41 | mDataList = new ArrayList<>(); 42 | for (int i = 0; i < 20; i++) { 43 | mDataList.add("测试数据:" + i); 44 | } 45 | mAdapter = new StringDataAdapter(this, mDataList); 46 | mSWRListView.setAdapter(mAdapter); 47 | 48 | // 设置侧滑的选项 49 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 50 | @Override 51 | public void create(SwipeMenu menu) { 52 | 53 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 54 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 55 | 0x3F, 0x25))); 56 | deleteItem.setWidth(dp2px(60)); 57 | deleteItem.setTitle("删除"); 58 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 59 | deleteItem.setTitleSize(15); 60 | menu.addMenuItem(deleteItem); 61 | } 62 | }; 63 | mSWRListView.setMenuCreator(creator); 64 | 65 | // 侧滑的监听事件 66 | mSWRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 67 | @Override 68 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 69 | mDataList.remove(i); 70 | mAdapter.notifyDataSetChanged(); 71 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 72 | return false; 73 | } 74 | }); 75 | mSWRListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 76 | @Override 77 | public void onItemClick(AdapterView parent, View view, int position, long id) { 78 | Toast.makeText(getApplicationContext(), "点击了"+ position + "项!!!", Toast.LENGTH_SHORT).show(); 79 | } 80 | }); 81 | } 82 | 83 | 84 | private void initView() { 85 | mSWRListView = (SMRListView) findViewById(R.id.lv_swipe_menu); 86 | 87 | } 88 | 89 | /** 90 | * dp与px之间的转换 91 | * 92 | * @param dp 93 | * @return 94 | */ 95 | private int dp2px(int dp) { 96 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 97 | getResources().getDisplayMetrics()); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/adapter/StringDataExpandAdapter.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.adapter; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseExpandableListAdapter; 8 | import android.widget.ExpandableListAdapter; 9 | import android.widget.TextView; 10 | 11 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 12 | import com.chy.swipemenuandpulltorefresh.R; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * StringDataExpandAdapter 18 | * 19 | * @author lenovo 2017/5/4. 20 | * Function Describe 21 | * @modify lenovo 2017/5/4. 22 | * Function Describe 23 | */ 24 | public class StringDataExpandAdapter extends BaseExpandableListAdapter { 25 | private Context context; 26 | private List parentList; 27 | 28 | public StringDataExpandAdapter(Context context, List parentList) { 29 | this.context = context; 30 | this.parentList = parentList; 31 | } 32 | 33 | 34 | @Override 35 | public int getGroupCount() { 36 | return parentList.size(); 37 | } 38 | 39 | @Override 40 | public int getChildrenCount(int groupPosition) { 41 | return parentList.get(groupPosition).getChildListStr().size(); 42 | } 43 | 44 | @Override 45 | public Object getGroup(int groupPosition) { 46 | return parentList.get(groupPosition); 47 | } 48 | 49 | @Override 50 | public Object getChild(int groupPosition, int childPosition) { 51 | return parentList.get(groupPosition).getChildListStr().get(childPosition); 52 | } 53 | 54 | @Override 55 | public long getGroupId(int groupPosition) { 56 | return groupPosition; 57 | } 58 | 59 | @Override 60 | public long getChildId(int groupPosition, int childPosition) { 61 | return childPosition; 62 | } 63 | 64 | @Override 65 | public boolean hasStableIds() { 66 | return false; 67 | } 68 | 69 | @Override 70 | public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 71 | StringEntity entity = parentList.get(groupPosition); 72 | ViewHolderParent viewHolderParent; 73 | if (convertView == null) { 74 | convertView = LayoutInflater.from(context).inflate(R.layout.item, null); 75 | viewHolderParent = new ViewHolderParent(); 76 | viewHolderParent.parentTv = (TextView) convertView.findViewById(R.id.text); 77 | convertView.setTag(viewHolderParent); 78 | } else { 79 | viewHolderParent = (ViewHolderParent) convertView.getTag(); 80 | } 81 | viewHolderParent.parentTv.setText(entity.getTitle()); 82 | return convertView; 83 | } 84 | 85 | @Override 86 | public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 87 | String str = (String) getChild(groupPosition, childPosition); 88 | ViewHolderChild holderChild; 89 | if (convertView == null) { 90 | convertView = LayoutInflater.from(context).inflate(R.layout.item, null); 91 | holderChild = new ViewHolderChild(); 92 | holderChild.childTv = (TextView) convertView.findViewById(R.id.text); 93 | convertView.setTag(holderChild); 94 | } else { 95 | holderChild = (ViewHolderChild) convertView.getTag(); 96 | } 97 | holderChild.childTv.setText(str); 98 | holderChild.childTv.setTextSize(13); 99 | holderChild.childTv.setTextColor(context.getResources().getColor(R.color.colorAccent)); 100 | return convertView; 101 | } 102 | 103 | @Override 104 | public boolean isChildSelectable(int groupPosition, int childPosition) { 105 | return true; 106 | } 107 | 108 | private class ViewHolderParent { 109 | private TextView parentTv; 110 | } 111 | 112 | private class ViewHolderChild { 113 | private TextView childTv; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/slistview/SMAdapter.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.slistview; 2 | 3 | import android.content.Context; 4 | import android.database.DataSetObserver; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.ColorDrawable; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ListAdapter; 10 | import android.widget.WrapperListAdapter; 11 | 12 | import com.chy.srlibrary.SwipeMenu; 13 | import com.chy.srlibrary.SwipeMenuItem; 14 | 15 | 16 | /** 17 | * SMAdapter 18 | * 19 | * @author CHY 2016/11/8. 20 | */ 21 | public class SMAdapter implements WrapperListAdapter, 22 | SMView.OnSwipeItemClickListener { 23 | 24 | private ListAdapter mAdapter; 25 | private Context mContext; 26 | private SMListView.OnMenuItemClickListener onMenuItemClickListener; 27 | 28 | public SMAdapter(Context context, ListAdapter adapter) { 29 | mAdapter = adapter; 30 | mContext = context; 31 | } 32 | 33 | @Override 34 | public int getCount() { 35 | return mAdapter.getCount(); 36 | } 37 | 38 | @Override 39 | public Object getItem(int position) { 40 | return mAdapter.getItem(position); 41 | } 42 | 43 | @Override 44 | public long getItemId(int position) { 45 | return mAdapter.getItemId(position); 46 | } 47 | 48 | @Override 49 | public View getView(int position, View convertView, ViewGroup parent) { 50 | SMLayout layout = null; 51 | if (convertView == null) { 52 | View contentView = mAdapter.getView(position, convertView, parent); 53 | SwipeMenu menu = new SwipeMenu(mContext); 54 | menu.setViewType(mAdapter.getItemViewType(position)); 55 | createMenu(menu); 56 | SMView menuView = new SMView(menu, 57 | (SMListView) parent); 58 | menuView.setOnSwipeItemClickListener(this); 59 | SMListView listView = (SMListView) parent; 60 | layout = new SMLayout(contentView, menuView, 61 | listView.getCloseInterpolator(), 62 | listView.getOpenInterpolator()); 63 | layout.setPosition(position); 64 | } else { 65 | layout = (SMLayout) convertView; 66 | layout.closeMenu(); 67 | layout.setPosition(position); 68 | View view = mAdapter.getView(position, layout.getContentView(), 69 | parent); 70 | } 71 | return layout; 72 | } 73 | 74 | public void createMenu(SwipeMenu menu) { 75 | // Test Code 76 | SwipeMenuItem item = new SwipeMenuItem(mContext); 77 | item.setTitle("Item 1"); 78 | item.setBackground(new ColorDrawable(Color.GRAY)); 79 | item.setWidth(300); 80 | menu.addMenuItem(item); 81 | 82 | item = new SwipeMenuItem(mContext); 83 | item.setTitle("Item 2"); 84 | item.setBackground(new ColorDrawable(Color.RED)); 85 | item.setWidth(300); 86 | menu.addMenuItem(item); 87 | } 88 | 89 | @Override 90 | public void onItemClick(SMView view, SwipeMenu menu, int index) { 91 | if (onMenuItemClickListener != null) { 92 | onMenuItemClickListener.onMenuItemClick(view.getPosition(), menu, 93 | index); 94 | } 95 | } 96 | 97 | public void setOnMenuItemClickListener( 98 | SMListView.OnMenuItemClickListener onMenuItemClickListener) { 99 | this.onMenuItemClickListener = onMenuItemClickListener; 100 | } 101 | 102 | @Override 103 | public void registerDataSetObserver(DataSetObserver observer) { 104 | mAdapter.registerDataSetObserver(observer); 105 | } 106 | 107 | @Override 108 | public void unregisterDataSetObserver(DataSetObserver observer) { 109 | mAdapter.unregisterDataSetObserver(observer); 110 | } 111 | 112 | @Override 113 | public boolean areAllItemsEnabled() { 114 | return mAdapter.areAllItemsEnabled(); 115 | } 116 | 117 | @Override 118 | public boolean isEnabled(int position) { 119 | return mAdapter.isEnabled(position); 120 | } 121 | 122 | @Override 123 | public boolean hasStableIds() { 124 | return mAdapter.hasStableIds(); 125 | } 126 | 127 | @Override 128 | public int getItemViewType(int position) { 129 | return mAdapter.getItemViewType(position); 130 | } 131 | 132 | @Override 133 | public int getViewTypeCount() { 134 | return mAdapter.getViewTypeCount(); 135 | } 136 | 137 | @Override 138 | public boolean isEmpty() { 139 | return mAdapter.isEmpty(); 140 | } 141 | 142 | @Override 143 | public ListAdapter getWrappedAdapter() { 144 | return mAdapter; 145 | } 146 | 147 | } 148 | 149 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ListViewAcitvity/SMRListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ListViewAcitvity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 16 | import com.chy.srlibrary.slistview.SMListView; 17 | import com.chy.srlibrary.slistview.SMRListView; 18 | import com.chy.swipemenuandpulltorefresh.R; 19 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * listView 的侧滑删除,上拉加载和下拉刷新 26 | */ 27 | public class SMRListViewActivity extends AppCompatActivity { 28 | 29 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 30 | private SMRListView mSWRListView; // 侧滑listView 31 | private StringDataAdapter mAdapter; 32 | private List mDataList; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_smr); 38 | initView(); 39 | mDataList = new ArrayList<>(); 40 | for (int i = 0; i < 20; i++) { 41 | mDataList.add("测试数据:" + i); 42 | } 43 | mAdapter = new StringDataAdapter(this, mDataList); 44 | mSWRListView.setAdapter(mAdapter); 45 | 46 | // 设置侧滑的选项 47 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 48 | @Override 49 | public void create(SwipeMenu menu) { 50 | 51 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 52 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 53 | 0x3F, 0x25))); 54 | deleteItem.setWidth(dp2px(60)); 55 | deleteItem.setTitle("删除"); 56 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 57 | deleteItem.setTitleSize(15); 58 | menu.addMenuItem(deleteItem); 59 | } 60 | }; 61 | mSWRListView.setMenuCreator(creator); 62 | 63 | // 侧滑的监听事件 64 | mSWRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 65 | @Override 66 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 67 | mDataList.remove(i); 68 | mAdapter.notifyDataSetChanged(); 69 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 70 | return false; 71 | } 72 | }); 73 | 74 | // 上拉加载和下拉刷新的监听事件 75 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 76 | @Override 77 | public void onRefresh(PTRLayoutView ptrLayoutView) { 78 | // 处理下拉刷新 79 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 80 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 81 | } 82 | 83 | @Override 84 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 85 | // 处理上拉加载 86 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 87 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 88 | } 89 | }); 90 | } 91 | 92 | 93 | private void initView() { 94 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 95 | mSWRListView = (SMRListView) findViewById(R.id.lv_swipe_menu); 96 | 97 | } 98 | 99 | /** 100 | * dp与px之间的转换 101 | * 102 | * @param dp 103 | * @return 104 | */ 105 | private int dp2px(int dp) { 106 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 107 | getResources().getDisplayMetrics()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ListViewAcitvity/RListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ListViewAcitvity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 16 | import com.chy.srlibrary.slistview.SMListView; 17 | import com.chy.srlibrary.slistview.SMRListView; 18 | import com.chy.swipemenuandpulltorefresh.R; 19 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * listView的上拉加载和下拉刷新 26 | */ 27 | public class RListViewActivity extends AppCompatActivity { 28 | 29 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 30 | private SMRListView mSMRListView; // 侧滑listView 31 | private StringDataAdapter mAdapter; 32 | private List mDataList; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_smr); 38 | initView(); 39 | mDataList = new ArrayList<>(); 40 | for (int i = 0; i < 20; i++) { 41 | mDataList.add("测试数据:" + i); 42 | } 43 | mAdapter = new StringDataAdapter(this, mDataList); 44 | mSMRListView.setAdapter(mAdapter); 45 | 46 | // // 设置侧滑的选项 47 | // SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 48 | // @Override 49 | // public void create(SwipeMenu menu) { 50 | // 51 | // SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 52 | // deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 53 | // 0x3F, 0x25))); 54 | // deleteItem.setWidth(dp2px(60)); 55 | // deleteItem.setTitle("删除"); 56 | // deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 57 | // deleteItem.setTitleSize(15); 58 | // menu.addMenuItem(deleteItem); 59 | // } 60 | // }; 61 | // mSWRListView.setMenuCreator(creator); 62 | 63 | // 侧滑的监听事件 64 | mSMRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 65 | @Override 66 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 67 | mDataList.remove(i); 68 | mAdapter.notifyDataSetChanged(); 69 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 70 | return false; 71 | } 72 | }); 73 | 74 | // 上拉加载和下拉刷新的监听事件 75 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 76 | @Override 77 | public void onRefresh(PTRLayoutView ptrLayoutView) { 78 | // 处理下拉刷新 79 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 80 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 81 | } 82 | 83 | @Override 84 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 85 | // 处理上拉加载 86 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 87 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 88 | } 89 | }); 90 | } 91 | 92 | 93 | private void initView() { 94 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 95 | mSMRListView = (SMRListView) findViewById(R.id.lv_swipe_menu); 96 | 97 | } 98 | 99 | /** 100 | * dp与px之间的转换 101 | * 102 | * @param dp 103 | * @return 104 | */ 105 | private int dp2px(int dp) { 106 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 107 | getResources().getDisplayMetrics()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ExpandViewActivity/RExpandListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ExpandViewActivity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.expandview.SMExpandableView; 16 | import com.chy.srlibrary.expandview.SMRExpandView; 17 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 18 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 19 | import com.chy.swipemenuandpulltorefresh.R; 20 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataExpandAdapter; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | public class RExpandListViewActivity extends AppCompatActivity { 26 | 27 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 28 | private SMRExpandView mSMRExpandView; // 侧滑ExpandablelistView 29 | private StringDataExpandAdapter mAdapter; 30 | private List mDataList; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_smre); 36 | initView(); 37 | mAdapter = new StringDataExpandAdapter(this, mDataList); 38 | mSMRExpandView.setAdapter(mAdapter); 39 | 40 | // // 设置侧滑的选项 41 | // SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 42 | // @Override 43 | // public void create(SwipeMenu menu) { 44 | // 45 | // SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 46 | // deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 47 | // 0x3F, 0x25))); 48 | // deleteItem.setWidth(dp2px(60)); 49 | // deleteItem.setTitle("删除"); 50 | // deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 51 | // deleteItem.setTitleSize(15); 52 | // menu.addMenuItem(deleteItem); 53 | // } 54 | // }; 55 | // mSMRExpandView.setMenuCreator(creator); 56 | // 57 | // // 侧滑的监听事件 58 | // mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 59 | // @Override 60 | // public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 61 | // mDataList.remove(i); 62 | // mAdapter.notifyDataSetChanged(); 63 | // Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 64 | // return false; 65 | // } 66 | // }); 67 | 68 | // 上拉加载和下拉刷新的监听事件 69 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 70 | @Override 71 | public void onRefresh(PTRLayoutView ptrLayoutView) { 72 | // 处理下拉刷新 73 | mAdapter.notifyDataSetChanged(); 74 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 75 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 76 | } 77 | 78 | @Override 79 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 80 | // 处理上拉加载 81 | mAdapter.notifyDataSetChanged(); 82 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 83 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 84 | } 85 | }); 86 | } 87 | 88 | 89 | private void initView() { 90 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 91 | mSMRExpandView = (SMRExpandView) findViewById(R.id.elv_swipe_menu); 92 | 93 | // 初始化测试数据 94 | mDataList = new ArrayList<>(); 95 | List childList = new ArrayList<>(); 96 | for (int i = 0; i < 4; i++) { 97 | childList.add("小标题: " + i); 98 | } 99 | for (int i = 0; i < 20; i++) { 100 | mDataList.add(new StringEntity("大标题:" + i, childList)); 101 | } 102 | } 103 | 104 | /** 105 | * dp与px之间的转换 106 | * 107 | * @param dp 108 | * @return 109 | */ 110 | private int dp2px(int dp) { 111 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 112 | getResources().getDisplayMetrics()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ExpandViewActivity/SMExpandListViewActivity2.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ExpandViewActivity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.SwipeMenu; 13 | import com.chy.srlibrary.SwipeMenuItem; 14 | import com.chy.srlibrary.expandview.SMExpandableView; 15 | import com.chy.srlibrary.expandview.SMRExpandView; 16 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 17 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 18 | import com.chy.swipemenuandpulltorefresh.R; 19 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataExpandAdapter; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * ExpandListView 父条目侧滑删除(可设置特定项不予滑动删除) 26 | */ 27 | public class SMExpandListViewActivity2 extends AppCompatActivity { 28 | 29 | private SMRExpandView mSMRExpandView; // 侧滑ExpandablelistView 30 | private StringDataExpandAdapter mAdapter; 31 | private List mDataList; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_sme); 37 | initView(); 38 | mAdapter = new StringDataExpandAdapter(this, mDataList); 39 | mSMRExpandView.setAdapter(mAdapter); 40 | 41 | // 设置侧滑的选项 42 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 43 | @Override 44 | public void create(SwipeMenu menu) { 45 | 46 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 47 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 48 | 0x3F, 0x25))); 49 | deleteItem.setWidth(dp2px(60)); 50 | deleteItem.setTitle("删除"); 51 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 52 | deleteItem.setTitleSize(15); 53 | menu.addMenuItem(deleteItem); 54 | } 55 | }; 56 | mSMRExpandView.setMenuCreator(creator); 57 | 58 | // 侧滑的监听事件 59 | mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 60 | @Override 61 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 62 | mDataList.remove(i); 63 | mAdapter.notifyDataSetChanged(); 64 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 65 | return false; 66 | } 67 | }); 68 | 69 | // 设置条件可以让特定的项不滑动 70 | mSMRExpandView.setOnTouchListener(new View.OnTouchListener() { 71 | @Override 72 | public boolean onTouch(View v, MotionEvent event) { 73 | int action = event.getAction(); 74 | switch (action) { 75 | case MotionEvent.ACTION_DOWN: 76 | /** 77 | * oldPos 滑动的所处位置的position 78 | * setSwipeEnable() 是否进行侧滑: 79 | * 设置为false则不会发生侧滑; 80 | * 设置为true则会侧滑 81 | * 默认值为true 82 | * 83 | * 这里可根据具体的条件来判定是否可以进行滑动 84 | */ 85 | int oldPos = mSMRExpandView.pointToPosition((int) event.getX(), (int) event.getY()); 86 | if (oldPos < 5) { 87 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 88 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 89 | mSMRExpandView.setSwipeEnable(false); 90 | } else { 91 | mSMRExpandView.setSwipeEnable(true); 92 | } 93 | } 94 | return false; 95 | } 96 | }); 97 | 98 | } 99 | 100 | 101 | private void initView() { 102 | mSMRExpandView = (SMRExpandView) findViewById(R.id.elv_swipe_menu); 103 | 104 | // 初始化测试数据 105 | mDataList = new ArrayList<>(); 106 | List childList = new ArrayList<>(); 107 | for (int i = 0; i < 4; i++) { 108 | childList.add("小标题: " + i); 109 | } 110 | for (int i = 0; i < 20; i++) { 111 | mDataList.add(new StringEntity("大标题:" + i, childList)); 112 | } 113 | } 114 | 115 | /** 116 | * dp与px之间的转换 117 | * 118 | * @param dp 119 | * @return 120 | */ 121 | private int dp2px(int dp) { 122 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 123 | getResources().getDisplayMetrics()); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ListViewAcitvity/SMListViewActivity2.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ListViewAcitvity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.widget.AdapterView; 11 | import android.widget.Toast; 12 | 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 16 | import com.chy.srlibrary.slistview.SMListView; 17 | import com.chy.srlibrary.slistview.SMRListView; 18 | import com.chy.swipemenuandpulltorefresh.R; 19 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * listView 的侧滑删除(可设置条件令某一项不滑动) 26 | */ 27 | public class SMListViewActivity2 extends AppCompatActivity { 28 | 29 | private SMRListView mSWRListView; // 侧滑listView 30 | private StringDataAdapter mAdapter; 31 | private List mDataList; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_sm); 37 | initView(); 38 | mDataList = new ArrayList<>(); 39 | for (int i = 0; i < 20; i++) { 40 | mDataList.add("测试数据:" + i); 41 | } 42 | mAdapter = new StringDataAdapter(this, mDataList); 43 | mSWRListView.setAdapter(mAdapter); 44 | 45 | // 设置侧滑的选项 46 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 47 | @Override 48 | public void create(SwipeMenu menu) { 49 | 50 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 51 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 52 | 0x3F, 0x25))); 53 | deleteItem.setWidth(dp2px(60)); 54 | deleteItem.setTitle("删除"); 55 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 56 | deleteItem.setTitleSize(15); 57 | menu.addMenuItem(deleteItem); 58 | } 59 | }; 60 | mSWRListView.setMenuCreator(creator); 61 | 62 | // 侧滑的监听事件 63 | mSWRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 64 | @Override 65 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 66 | mDataList.remove(i); 67 | mAdapter.notifyDataSetChanged(); 68 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 69 | return false; 70 | } 71 | }); 72 | mSWRListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 73 | @Override 74 | public void onItemClick(AdapterView parent, View view, int position, long id) { 75 | Toast.makeText(getApplicationContext(), "点击了"+ position + "项!!!", Toast.LENGTH_SHORT).show(); 76 | } 77 | }); 78 | 79 | mSWRListView.setOnTouchListener(new View.OnTouchListener() { 80 | @Override 81 | public boolean onTouch(View v, MotionEvent event) { 82 | int action = event.getAction(); 83 | switch (action) { 84 | case MotionEvent.ACTION_DOWN: 85 | /** 86 | * oldPos 滑动的所处位置的position 87 | * setSwipeEnable() 是否进行侧滑: 88 | * 设置为false则不会发生侧滑; 89 | * 设置为true则会侧滑 90 | * 默认值为true 91 | * 92 | * 这里可根据具体的条件来判定是否可以进行滑动 93 | */ 94 | int oldPos = mSWRListView.pointToPosition((int) event.getX(), (int) event.getY()); 95 | if (oldPos < 5) { 96 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 97 | mSWRListView.setSwipeEnable(false); 98 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 99 | } else { 100 | mSWRListView.setSwipeEnable(true); 101 | } 102 | } 103 | return false; 104 | } 105 | }); 106 | } 107 | 108 | 109 | private void initView() { 110 | mSWRListView = (SMRListView) findViewById(R.id.lv_swipe_menu); 111 | 112 | } 113 | 114 | /** 115 | * dp与px之间的转换 116 | * 117 | * @param dp 118 | * @return 119 | */ 120 | private int dp2px(int dp) { 121 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 122 | getResources().getDisplayMetrics()); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ExpandViewActivity/SMRExpandListViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ExpandViewActivity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.expandview.SMExpandableView; 16 | import com.chy.srlibrary.expandview.SMRExpandView; 17 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 18 | import com.chy.srlibrary.slistview.SMListView; 19 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 20 | import com.chy.swipemenuandpulltorefresh.R; 21 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 22 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataExpandAdapter; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * ExpandListView 父条目侧滑删除和上拉刷新以及下拉加载 29 | */ 30 | public class SMRExpandListViewActivity extends AppCompatActivity { 31 | 32 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 33 | private SMRExpandView mSMRExpandView; // 侧滑ExpandablelistView 34 | private StringDataExpandAdapter mAdapter; 35 | private List mDataList; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_smre); 41 | initView(); 42 | mAdapter = new StringDataExpandAdapter(this, mDataList); 43 | mSMRExpandView.setAdapter(mAdapter); 44 | 45 | // 设置侧滑的选项 46 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 47 | @Override 48 | public void create(SwipeMenu menu) { 49 | 50 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 51 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 52 | 0x3F, 0x25))); 53 | deleteItem.setWidth(dp2px(60)); 54 | deleteItem.setTitle("删除"); 55 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 56 | deleteItem.setTitleSize(15); 57 | menu.addMenuItem(deleteItem); 58 | } 59 | }; 60 | mSMRExpandView.setMenuCreator(creator); 61 | 62 | // 侧滑的监听事件 63 | mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 64 | @Override 65 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 66 | mDataList.remove(i); 67 | mAdapter.notifyDataSetChanged(); 68 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 69 | return false; 70 | } 71 | }); 72 | 73 | // 上拉加载和下拉刷新的监听事件 74 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 75 | @Override 76 | public void onRefresh(PTRLayoutView ptrLayoutView) { 77 | 78 | // 刷新测试数据 79 | mDataList.clear(); 80 | List childList = new ArrayList<>(); 81 | for (int i = 0; i < 4; i++) { 82 | childList.add("小标题: " + i); 83 | } 84 | for (int i = 0; i < 20; i++) { 85 | mDataList.add(new StringEntity("大标题:" + i, childList)); 86 | } 87 | // 处理下拉刷新 88 | mAdapter.notifyDataSetChanged(); 89 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 90 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 91 | } 92 | 93 | @Override 94 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 95 | // 处理上拉加载 96 | List childList = new ArrayList<>(); 97 | for (int i = 0; i < 4; i++) { 98 | childList.add("小标题: " + i); 99 | } 100 | mDataList.add(new StringEntity("大标题:加载", childList)); 101 | mAdapter.notifyDataSetChanged(); 102 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 103 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 104 | } 105 | }); 106 | } 107 | 108 | 109 | private void initView() { 110 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 111 | mSMRExpandView = (SMRExpandView) findViewById(R.id.elv_swipe_menu); 112 | 113 | // 初始化测试数据 114 | mDataList = new ArrayList<>(); 115 | List childList = new ArrayList<>(); 116 | for (int i = 0; i < 4; i++) { 117 | childList.add("小标题: " + i); 118 | } 119 | for (int i = 0; i < 20; i++) { 120 | mDataList.add(new StringEntity("大标题:" + i, childList)); 121 | } 122 | } 123 | 124 | /** 125 | * dp与px之间的转换 126 | * 127 | * @param dp 128 | * @return 129 | */ 130 | private int dp2px(int dp) { 131 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 132 | getResources().getDisplayMetrics()); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ListViewAcitvity/SMRListViewActivity2.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ListViewAcitvity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 16 | import com.chy.srlibrary.slistview.SMListView; 17 | import com.chy.srlibrary.slistview.SMRListView; 18 | import com.chy.swipemenuandpulltorefresh.R; 19 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataAdapter; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | /** 25 | * listView 的侧滑删除,上拉加载和下拉刷新 (可设置条件令某项不滑动) 26 | */ 27 | public class SMRListViewActivity2 extends AppCompatActivity { 28 | 29 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 30 | private SMRListView mSWRListView; // 侧滑listView 31 | private StringDataAdapter mAdapter; 32 | private List mDataList; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_smr); 38 | initView(); 39 | mDataList = new ArrayList<>(); 40 | for (int i = 0; i < 20; i++) { 41 | mDataList.add("测试数据:" + i); 42 | } 43 | mAdapter = new StringDataAdapter(this, mDataList); 44 | mSWRListView.setAdapter(mAdapter); 45 | 46 | // 设置侧滑的选项 47 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 48 | @Override 49 | public void create(SwipeMenu menu) { 50 | 51 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 52 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 53 | 0x3F, 0x25))); 54 | deleteItem.setWidth(dp2px(60)); 55 | deleteItem.setTitle("删除"); 56 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 57 | deleteItem.setTitleSize(15); 58 | menu.addMenuItem(deleteItem); 59 | } 60 | }; 61 | mSWRListView.setMenuCreator(creator); 62 | 63 | // 侧滑的监听事件 64 | mSWRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 65 | @Override 66 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 67 | mDataList.remove(i); 68 | mAdapter.notifyDataSetChanged(); 69 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 70 | return false; 71 | } 72 | }); 73 | mSWRListView.setOnTouchListener(new View.OnTouchListener() { 74 | @Override 75 | public boolean onTouch(View v, MotionEvent event) { 76 | int action = event.getAction(); 77 | switch (action) { 78 | case MotionEvent.ACTION_DOWN: 79 | /** 80 | * oldPos 滑动的所处位置的position 81 | * setSwipeEnable() 是否进行侧滑: 82 | * 设置为false则不会发生侧滑; 83 | * 设置为true则会侧滑 84 | * 默认值为true 85 | * 86 | * 这里可根据具体的条件来判定是否可以进行滑动 87 | */ 88 | int oldPos = mSWRListView.pointToPosition((int) event.getX(), (int) event.getY()); 89 | if (oldPos < 5) { 90 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 91 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 92 | mSWRListView.setSwipeEnable(false); 93 | } else { 94 | mSWRListView.setSwipeEnable(true); 95 | } 96 | } 97 | return false; 98 | } 99 | }); 100 | 101 | // 上拉加载和下拉刷新的监听事件 102 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 103 | @Override 104 | public void onRefresh(PTRLayoutView ptrLayoutView) { 105 | // 处理下拉刷新 106 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 107 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 108 | } 109 | 110 | @Override 111 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 112 | // 处理上拉加载 113 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 114 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 115 | } 116 | }); 117 | } 118 | 119 | 120 | private void initView() { 121 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 122 | mSWRListView = (SMRListView) findViewById(R.id.lv_swipe_menu); 123 | 124 | } 125 | 126 | /** 127 | * dp与px之间的转换 128 | * 129 | * @param dp 130 | * @return 131 | */ 132 | private int dp2px(int dp) { 133 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 134 | getResources().getDisplayMetrics()); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/expandview/SMExpandableAdapter.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.expandview; 2 | 3 | import android.content.Context; 4 | import android.database.DataSetObserver; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.ColorDrawable; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.BaseExpandableListAdapter; 11 | import android.widget.ExpandableListAdapter; 12 | 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | 16 | 17 | /** 18 | * SMExpandableAdapter 19 | * 20 | * @author CHY 2016/7/29. 21 | * 22 | * ExpandableListView侧滑布局的适配器 23 | */ 24 | public class SMExpandableAdapter extends BaseExpandableListAdapter implements SMExpandView.OnSwipeItemClickListener { 25 | 26 | private ExpandableListAdapter mAdapter; 27 | private Context mContext; 28 | private SMExpandableView.OnMenuItemClickListener onMenuItemClickListener; 29 | 30 | public SMExpandableAdapter(Context context, ExpandableListAdapter adapter) { 31 | mAdapter = adapter; 32 | mContext = context; 33 | } 34 | 35 | public void createMenu(SwipeMenu menu) { 36 | // Test Code 37 | SwipeMenuItem item = new SwipeMenuItem(mContext); 38 | item.setTitle("Item 1"); 39 | item.setBackground(new ColorDrawable(Color.GRAY)); 40 | item.setWidth(300); 41 | menu.addMenuItem(item); 42 | 43 | item = new SwipeMenuItem(mContext); 44 | item.setTitle("Item 2"); 45 | item.setBackground(new ColorDrawable(Color.RED)); 46 | item.setWidth(300); 47 | menu.addMenuItem(item); 48 | } 49 | 50 | @Override 51 | public View getGroupView(int position, boolean b, View convertView, ViewGroup parent) { 52 | SMLayoutView layout = null; 53 | if (convertView == null) { 54 | View contentView = mAdapter.getGroupView(position, b, convertView, parent); 55 | SwipeMenu menu = new SwipeMenu(mContext); 56 | menu.setViewType(1); 57 | createMenu(menu); 58 | SMExpandView menuView = new SMExpandView(menu, 59 | (SMExpandableView) parent); 60 | Log.i("heyn", "ssssssss onItemClsssssick"); 61 | menuView.setOnSwipeItemClickListener(this); 62 | SMExpandableView listView = (SMExpandableView) parent; 63 | layout = new SMLayoutView(contentView, menuView, 64 | listView.getCloseInterpolator(), 65 | listView.getOpenInterpolator()); 66 | layout.setPosition(position); 67 | } else { 68 | layout = (SMLayoutView) convertView; 69 | layout.closeMenu(); 70 | layout.setPosition(position); 71 | View view = mAdapter.getGroupView(position, b, layout.getContentView(), 72 | parent); 73 | } 74 | 75 | return layout; 76 | } 77 | 78 | @Override 79 | public View getChildView(int position, int i1, boolean b, View convertView, ViewGroup parent) { 80 | convertView = mAdapter.getChildView(position, i1, b, convertView, parent); 81 | return convertView; 82 | // 为child添加滑动删除 83 | // SwipeMenuLayoutView layout = null; 84 | // if (convertView == null) { 85 | // View contentView = mAdapter.getChildView(position,i1, b, convertView, parent); 86 | // SwipeMenu menu = new SwipeMenu(mContext); 87 | // menu.setViewType(1); 88 | // createMenu(menu); 89 | // SwipeMenuView menuView = new SwipeMenuView(menu, 90 | // (SwipeMenuExpandableListView) parent); 91 | // menuView.setOnSwipeItemClickListener(this); 92 | // SwipeMenuExpandableListView listView = (SwipeMenuExpandableListView) parent; 93 | // layout = new SwipeMenuLayoutView(contentView, menuView, 94 | // listView.getCloseInterpolator(), 95 | // listView.getOpenInterpolator()); 96 | // layout.setPosition(i1); 97 | // } else { 98 | // layout = (SwipeMenuLayoutView) convertView; 99 | // layout.closeMenu(); 100 | // layout.setPosition(i1); 101 | // View view = mAdapter.getChildView(position,i1, b, layout.getContentView(), 102 | // parent); 103 | // } 104 | // return layout; 105 | } 106 | 107 | 108 | public void setOnSwipeItemClickListener( 109 | SMExpandableView.OnMenuItemClickListener onMenuItemClickListener) { 110 | this.onMenuItemClickListener = onMenuItemClickListener; 111 | } 112 | 113 | @Override 114 | public void registerDataSetObserver(DataSetObserver observer) { 115 | mAdapter.registerDataSetObserver(observer); 116 | } 117 | 118 | @Override 119 | public void unregisterDataSetObserver(DataSetObserver observer) { 120 | mAdapter.unregisterDataSetObserver(observer); 121 | } 122 | 123 | @Override 124 | public int getGroupCount() { 125 | return mAdapter.getGroupCount(); 126 | } 127 | 128 | @Override 129 | public int getChildrenCount(int i) { 130 | return mAdapter.getChildrenCount(i); 131 | } 132 | 133 | @Override 134 | public Object getGroup(int i) { 135 | return mAdapter.getGroup(i); 136 | } 137 | 138 | @Override 139 | public Object getChild(int i, int i1) { 140 | return mAdapter.getChild(i, i1); 141 | } 142 | 143 | @Override 144 | public long getGroupId(int i) { 145 | return i; 146 | } 147 | 148 | @Override 149 | public long getChildId(int i, int i1) { 150 | return i1; 151 | } 152 | 153 | @Override 154 | public boolean areAllItemsEnabled() { 155 | return mAdapter.areAllItemsEnabled(); 156 | } 157 | 158 | 159 | @Override 160 | public boolean hasStableIds() { 161 | return mAdapter.hasStableIds(); 162 | } 163 | 164 | @Override 165 | public boolean isChildSelectable(int i, int i1) { 166 | return true; 167 | } 168 | 169 | @Override 170 | public boolean isEmpty() { 171 | return mAdapter.isEmpty(); 172 | } 173 | 174 | @Override 175 | public void onItemClick(SMExpandView view, SwipeMenu menu, int index) { 176 | if (onMenuItemClickListener != null) { 177 | onMenuItemClickListener.onMenuItemClick(view.getPosition(), menu, index); 178 | } 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /app/src/main/java/com/chy/swipemenuandpulltorefresh/ExpandViewActivity/SMRExpandListViewActivity2.java: -------------------------------------------------------------------------------- 1 | package com.chy.swipemenuandpulltorefresh.ExpandViewActivity; 2 | 3 | import android.graphics.Color; 4 | import android.graphics.drawable.ColorDrawable; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.TypedValue; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.widget.Toast; 11 | 12 | import com.chy.srlibrary.PTRLayoutView; 13 | import com.chy.srlibrary.SwipeMenu; 14 | import com.chy.srlibrary.SwipeMenuItem; 15 | import com.chy.srlibrary.expandview.SMExpandableView; 16 | import com.chy.srlibrary.expandview.SMRExpandView; 17 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 18 | import com.chy.swipemenuandpulltorefresh.Entity.StringEntity; 19 | import com.chy.swipemenuandpulltorefresh.R; 20 | import com.chy.swipemenuandpulltorefresh.adapter.StringDataExpandAdapter; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | /** 25 | * ExpandListView 父条目侧滑删除和上拉刷新以及下拉加载 (可设置特定项不予滑动删除) 26 | */ 27 | public class SMRExpandListViewActivity2 extends AppCompatActivity { 28 | 29 | private PTRLayoutView mPTRLayoutView; // 刷新控制器 30 | private SMRExpandView mSMRExpandView; // 侧滑ExpandablelistView 31 | private StringDataExpandAdapter mAdapter; 32 | private List mDataList; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_smre); 38 | initView(); 39 | mAdapter = new StringDataExpandAdapter(this, mDataList); 40 | mSMRExpandView.setAdapter(mAdapter); 41 | 42 | // 设置侧滑的选项 43 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 44 | @Override 45 | public void create(SwipeMenu menu) { 46 | 47 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 48 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 49 | 0x3F, 0x25))); 50 | deleteItem.setWidth(dp2px(60)); 51 | deleteItem.setTitle("删除"); 52 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 53 | deleteItem.setTitleSize(15); 54 | menu.addMenuItem(deleteItem); 55 | } 56 | }; 57 | mSMRExpandView.setMenuCreator(creator); 58 | 59 | // 侧滑的监听事件 60 | mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 61 | @Override 62 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 63 | mDataList.remove(i); 64 | mAdapter.notifyDataSetChanged(); 65 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 66 | return false; 67 | } 68 | }); 69 | 70 | // 上拉加载和下拉刷新的监听事件 71 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 72 | @Override 73 | public void onRefresh(PTRLayoutView ptrLayoutView) { 74 | // 处理下拉刷新 75 | mAdapter.notifyDataSetChanged(); 76 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 77 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 78 | } 79 | 80 | @Override 81 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 82 | // 处理上拉加载 83 | mAdapter.notifyDataSetChanged(); 84 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 85 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 86 | } 87 | }); 88 | 89 | // 设置条件可以另特定的项不滑动 90 | mSMRExpandView.setOnTouchListener(new View.OnTouchListener() { 91 | @Override 92 | public boolean onTouch(View v, MotionEvent event) { 93 | int action = event.getAction(); 94 | switch (action) { 95 | case MotionEvent.ACTION_DOWN: 96 | /** 97 | * oldPos 滑动的所处位置的position 98 | * setSwipeEnable() 是否进行侧滑: 99 | * 设置为false则不会发生侧滑; 100 | * 设置为true则会侧滑 101 | * 默认值为true 102 | * 103 | * 这里可根据具体的条件来判定是否可以进行滑动 104 | */ 105 | int oldPos = mSMRExpandView.pointToPosition((int) event.getX(), (int) event.getY()); 106 | if (oldPos < 5) { 107 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 108 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 109 | mSMRExpandView.setSwipeEnable(false); 110 | } else { 111 | mSMRExpandView.setSwipeEnable(true); 112 | } 113 | } 114 | return false; 115 | } 116 | }); 117 | } 118 | 119 | 120 | private void initView() { 121 | mPTRLayoutView = (PTRLayoutView) findViewById(R.id.refresh_view); 122 | mSMRExpandView = (SMRExpandView) findViewById(R.id.elv_swipe_menu); 123 | 124 | // 初始化测试数据 125 | mDataList = new ArrayList<>(); 126 | List childList = new ArrayList<>(); 127 | for (int i = 0; i < 4; i++) { 128 | childList.add("小标题: " + i); 129 | } 130 | for (int i = 0; i < 20; i++) { 131 | mDataList.add(new StringEntity("大标题:" + i, childList)); 132 | } 133 | } 134 | 135 | /** 136 | * dp与px之间的转换 137 | * 138 | * @param dp 139 | * @return 140 | */ 141 | private int dp2px(int dp) { 142 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 143 | getResources().getDisplayMetrics()); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/slistview/SMListView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.slistview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.util.TypedValue; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.animation.Interpolator; 9 | import android.widget.ListAdapter; 10 | import android.widget.ListView; 11 | 12 | import com.chy.srlibrary.SwipeMenu; 13 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 14 | 15 | 16 | /** 17 | * SwipeMenuListView 18 | * 19 | * @author CHY 2016/11/8. 20 | * Function Describe 21 | */ 22 | public class SMListView extends ListView { 23 | 24 | private static final int TOUCH_STATE_NONE = 0; 25 | private static final int TOUCH_STATE_X = 1; 26 | private static final int TOUCH_STATE_Y = 2; 27 | 28 | public static final int DIRECTION_LEFT = 1; 29 | public static final int DIRECTION_RIGHT = -1; 30 | private int mDirection = 1;//swipe from right to left by default 31 | 32 | private int MAX_Y = 5; 33 | private int MAX_X = 3; 34 | private float mDownX; 35 | private float mDownY; 36 | private int mTouchState; 37 | private int mTouchPosition; 38 | private boolean swipeEnable = true; // 标志位---是否允许左滑 39 | private SMLayout mTouchView; 40 | private SMListView.OnSwipeListener mOnSwipeListener; 41 | 42 | private SwipeMenuCreatorInterfaceUtil mMenuCreator; 43 | private SMListView.OnMenuItemClickListener mOnMenuItemClickListener; 44 | private SMListView.OnMenuStateChangeListener mOnMenuStateChangeListener; 45 | private Interpolator mCloseInterpolator; 46 | private Interpolator mOpenInterpolator; 47 | 48 | public SMListView(Context context) { 49 | super(context); 50 | init(); 51 | } 52 | 53 | public SMListView(Context context, AttributeSet attrs, int defStyle) { 54 | super(context, attrs, defStyle); 55 | init(); 56 | } 57 | 58 | public SMListView(Context context, AttributeSet attrs) { 59 | super(context, attrs); 60 | init(); 61 | } 62 | 63 | private void init() { 64 | MAX_X = dp2px(MAX_X); 65 | MAX_Y = dp2px(MAX_Y); 66 | mTouchState = TOUCH_STATE_NONE; 67 | } 68 | 69 | @Override 70 | public void setAdapter(ListAdapter adapter) { 71 | super.setAdapter(new SMAdapter(getContext(), adapter) { 72 | @Override 73 | public void createMenu(SwipeMenu menu) { 74 | if (mMenuCreator != null) { 75 | mMenuCreator.create(menu); 76 | } 77 | } 78 | 79 | @Override 80 | public void onItemClick(SMView view, SwipeMenu menu, 81 | int index) { 82 | boolean flag = false; 83 | if (mOnMenuItemClickListener != null) { 84 | flag = mOnMenuItemClickListener.onMenuItemClick( 85 | view.getPosition(), menu, index); 86 | } 87 | if (mTouchView != null && !flag) { 88 | mTouchView.smoothCloseMenu(); 89 | } 90 | } 91 | }); 92 | } 93 | 94 | public void setCloseInterpolator(Interpolator interpolator) { 95 | mCloseInterpolator = interpolator; 96 | } 97 | 98 | public void setOpenInterpolator(Interpolator interpolator) { 99 | mOpenInterpolator = interpolator; 100 | } 101 | 102 | public Interpolator getOpenInterpolator() { 103 | return mOpenInterpolator; 104 | } 105 | 106 | public Interpolator getCloseInterpolator() { 107 | return mCloseInterpolator; 108 | } 109 | 110 | @Override 111 | public boolean onInterceptTouchEvent(MotionEvent ev) { 112 | return super.onInterceptTouchEvent(ev); 113 | } 114 | 115 | @Override 116 | public boolean onTouchEvent(MotionEvent ev) { 117 | if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) 118 | return super.onTouchEvent(ev); 119 | int action = ev.getAction(); 120 | switch (action) { 121 | case MotionEvent.ACTION_DOWN: 122 | int oldPos = mTouchPosition; 123 | swipeEnable = isSwipeEnable(); 124 | mDownX = ev.getX(); 125 | mDownY = ev.getY(); 126 | mTouchState = TOUCH_STATE_NONE; 127 | mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); 128 | if (mTouchPosition == oldPos && mTouchView != null 129 | && mTouchView.isOpen()) { 130 | mTouchState = TOUCH_STATE_X; 131 | mTouchView.onSwipe(ev); 132 | return true; 133 | } 134 | 135 | View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); 136 | 137 | if (mTouchView != null && mTouchView.isOpen()) { 138 | mTouchView.smoothCloseMenu(); 139 | mTouchView = null; 140 | // return super.onTouchEvent(ev); 141 | // try to cancel the touch event 142 | MotionEvent cancelEvent = MotionEvent.obtain(ev); 143 | cancelEvent.setAction(MotionEvent.ACTION_CANCEL); 144 | onTouchEvent(cancelEvent); 145 | if (mOnMenuStateChangeListener != null) { 146 | mOnMenuStateChangeListener.onMenuClose(oldPos); 147 | } 148 | return true; 149 | } 150 | if (view instanceof SMLayout) { 151 | mTouchView = (SMLayout) view; 152 | mTouchView.setSwipeDirection(mDirection); 153 | } 154 | if (mTouchView != null) { 155 | mTouchView.onSwipe(ev); 156 | } 157 | break; 158 | case MotionEvent.ACTION_MOVE: 159 | float dy = Math.abs((ev.getY() - mDownY)); 160 | float dx = Math.abs((ev.getX() - mDownX)); 161 | if (mTouchState == TOUCH_STATE_X) { 162 | if (mTouchView != null && swipeEnable) { 163 | mTouchView.onSwipe(ev); 164 | } 165 | getSelector().setState(new int[]{0}); 166 | ev.setAction(MotionEvent.ACTION_CANCEL); 167 | super.onTouchEvent(ev); 168 | return true; 169 | } else if (mTouchState == TOUCH_STATE_NONE) { 170 | if (Math.abs(dy) > MAX_Y) { 171 | mTouchState = TOUCH_STATE_Y; 172 | } else if (dx > MAX_X) { 173 | mTouchState = TOUCH_STATE_X; 174 | if (mOnSwipeListener != null) { 175 | mOnSwipeListener.onSwipeStart(mTouchPosition); 176 | } 177 | } 178 | } 179 | break; 180 | case MotionEvent.ACTION_UP: 181 | if (mTouchState == TOUCH_STATE_X) { 182 | if (mTouchView != null) { 183 | boolean isBeforeOpen = mTouchView.isOpen(); 184 | if (swipeEnable) { 185 | mTouchView.onSwipe(ev); 186 | } 187 | boolean isAfterOpen = mTouchView.isOpen(); 188 | if (isBeforeOpen != isAfterOpen && mOnMenuStateChangeListener != null) { 189 | if (isAfterOpen) { 190 | mOnMenuStateChangeListener.onMenuOpen(mTouchPosition); 191 | } else { 192 | mOnMenuStateChangeListener.onMenuClose(mTouchPosition); 193 | } 194 | } 195 | if (!isAfterOpen) { 196 | mTouchPosition = -1; 197 | mTouchView = null; 198 | } 199 | } 200 | if (mOnSwipeListener != null) { 201 | mOnSwipeListener.onSwipeEnd(mTouchPosition); 202 | } 203 | ev.setAction(MotionEvent.ACTION_CANCEL); 204 | super.onTouchEvent(ev); 205 | return true; 206 | } 207 | break; 208 | } 209 | return super.onTouchEvent(ev); 210 | } 211 | 212 | public boolean isSwipeEnable() { 213 | return swipeEnable; 214 | } 215 | 216 | public void setSwipeEnable(boolean swipeEnable) { 217 | this.swipeEnable = swipeEnable; 218 | } 219 | 220 | public void smoothOpenMenu(int position) { 221 | if (position >= getFirstVisiblePosition() 222 | && position <= getLastVisiblePosition()) { 223 | View view = getChildAt(position - getFirstVisiblePosition()); 224 | if (view instanceof SMLayout) { 225 | mTouchPosition = position; 226 | if (mTouchView != null && mTouchView.isOpen()) { 227 | mTouchView.smoothCloseMenu(); 228 | } 229 | mTouchView = (SMLayout) view; 230 | mTouchView.setSwipeDirection(mDirection); 231 | mTouchView.smoothOpenMenu(); 232 | } 233 | } 234 | } 235 | 236 | public void smoothCloseMenu() { 237 | if (mTouchView != null && mTouchView.isOpen()) { 238 | mTouchView.smoothCloseMenu(); 239 | } 240 | } 241 | 242 | private int dp2px(int dp) { 243 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 244 | getContext().getResources().getDisplayMetrics()); 245 | } 246 | 247 | public void setMenuCreator(SwipeMenuCreatorInterfaceUtil menuCreator) { 248 | this.mMenuCreator = menuCreator; 249 | } 250 | 251 | public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) { 252 | this.mOnMenuItemClickListener = onMenuItemClickListener; 253 | } 254 | 255 | public void setOnSwipeListener(OnSwipeListener onSwipeListener) { 256 | this.mOnSwipeListener = onSwipeListener; 257 | } 258 | 259 | public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeListener) { 260 | mOnMenuStateChangeListener = onMenuStateChangeListener; 261 | } 262 | 263 | public static interface OnMenuItemClickListener { 264 | boolean onMenuItemClick(int position, SwipeMenu menu, int index); 265 | } 266 | 267 | public static interface OnSwipeListener { 268 | void onSwipeStart(int position); 269 | 270 | void onSwipeEnd(int position); 271 | } 272 | 273 | public static interface OnMenuStateChangeListener { 274 | void onMenuOpen(int position); 275 | 276 | void onMenuClose(int position); 277 | } 278 | 279 | public void setSwipeDirection(int direction) { 280 | mDirection = direction; 281 | } 282 | } 283 | 284 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/slistview/SMLayout.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.slistview; 2 | 3 | import android.content.Context; 4 | import android.support.v4.view.GestureDetectorCompat; 5 | import android.support.v4.widget.ScrollerCompat; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.util.TypedValue; 9 | import android.view.GestureDetector; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.view.animation.Interpolator; 13 | import android.widget.AbsListView; 14 | import android.widget.FrameLayout; 15 | 16 | 17 | /** 18 | * SwipeMenuLayout 19 | * 20 | * @author CHY 2016/11/8. 21 | */ 22 | public class SMLayout extends FrameLayout { 23 | 24 | private static final int CONTENT_VIEW_ID = 1; 25 | private static final int MENU_VIEW_ID = 2; 26 | 27 | private static final int STATE_CLOSE = 0; 28 | private static final int STATE_OPEN = 1; 29 | 30 | private int mSwipeDirection; 31 | 32 | private View mContentView; 33 | private SMView mMenuView; 34 | private int mDownX; 35 | private int state = STATE_CLOSE; 36 | private GestureDetectorCompat mGestureDetector; 37 | private GestureDetector.OnGestureListener mGestureListener; 38 | private boolean isFling; 39 | private int MIN_FLING = dp2px(15); 40 | private int MAX_VELOCITYX = -dp2px(500); 41 | private ScrollerCompat mOpenScroller; 42 | private ScrollerCompat mCloseScroller; 43 | private int mBaseX; 44 | private int position; 45 | private Interpolator mCloseInterpolator; 46 | private Interpolator mOpenInterpolator; 47 | 48 | public SMLayout(View contentView, SMView menuView) { 49 | this(contentView, menuView, null, null); 50 | } 51 | 52 | public SMLayout(View contentView, SMView menuView, 53 | Interpolator closeInterpolator, Interpolator openInterpolator) { 54 | super(contentView.getContext()); 55 | mCloseInterpolator = closeInterpolator; 56 | mOpenInterpolator = openInterpolator; 57 | mContentView = contentView; 58 | mMenuView = menuView; 59 | mMenuView.setLayout(this); 60 | init(); 61 | } 62 | 63 | // private SwipeMenuLayout(Context context, AttributeSet attrs, int 64 | // defStyle) { 65 | // super(context, attrs, defStyle); 66 | // } 67 | 68 | private SMLayout(Context context, AttributeSet attrs) { 69 | super(context, attrs); 70 | } 71 | 72 | private SMLayout(Context context) { 73 | super(context); 74 | } 75 | 76 | public int getPosition() { 77 | return position; 78 | } 79 | 80 | public void setPosition(int position) { 81 | this.position = position; 82 | mMenuView.setPosition(position); 83 | } 84 | 85 | public void setSwipeDirection(int swipeDirection) { 86 | mSwipeDirection = swipeDirection; 87 | } 88 | 89 | private void init() { 90 | setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 91 | LayoutParams.WRAP_CONTENT)); 92 | mGestureListener = new GestureDetector.SimpleOnGestureListener() { 93 | @Override 94 | public boolean onDown(MotionEvent e) { 95 | isFling = false; 96 | return true; 97 | } 98 | 99 | @Override 100 | public boolean onFling(MotionEvent e1, MotionEvent e2, 101 | float velocityX, float velocityY) { 102 | // TODO 103 | if (Math.abs(e1.getX() - e2.getX()) > MIN_FLING 104 | && velocityX < MAX_VELOCITYX) { 105 | isFling = true; 106 | } 107 | // Log.i("byz", MAX_VELOCITYX + ", velocityX = " + velocityX); 108 | return super.onFling(e1, e2, velocityX, velocityY); 109 | } 110 | }; 111 | mGestureDetector = new GestureDetectorCompat(getContext(), 112 | mGestureListener); 113 | 114 | // mScroller = ScrollerCompat.create(getContext(), new 115 | // BounceInterpolator()); 116 | if (mCloseInterpolator != null) { 117 | mCloseScroller = ScrollerCompat.create(getContext(), 118 | mCloseInterpolator); 119 | } else { 120 | mCloseScroller = ScrollerCompat.create(getContext()); 121 | } 122 | if (mOpenInterpolator != null) { 123 | mOpenScroller = ScrollerCompat.create(getContext(), 124 | mOpenInterpolator); 125 | } else { 126 | mOpenScroller = ScrollerCompat.create(getContext()); 127 | } 128 | 129 | LayoutParams contentParams = new LayoutParams( 130 | LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 131 | mContentView.setLayoutParams(contentParams); 132 | if (mContentView.getId() < 1) { 133 | mContentView.setId(CONTENT_VIEW_ID); 134 | } 135 | 136 | mMenuView.setId(MENU_VIEW_ID); 137 | mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 138 | LayoutParams.WRAP_CONTENT)); 139 | 140 | addView(mContentView); 141 | addView(mMenuView); 142 | 143 | // if (mContentView.getBackground() == null) { 144 | // mContentView.setBackgroundColor(Color.WHITE); 145 | // } 146 | 147 | // in android 2.x, MenuView height is MATCH_PARENT is not work. 148 | // getViewTreeObserver().addOnGlobalLayoutListener( 149 | // new OnGlobalLayoutListener() { 150 | // @Override 151 | // public void onGlobalLayout() { 152 | // setMenuHeight(mContentView.getHeight()); 153 | // // getViewTreeObserver() 154 | // // .removeGlobalOnLayoutListener(this); 155 | // } 156 | // }); 157 | 158 | } 159 | 160 | @Override 161 | protected void onAttachedToWindow() { 162 | super.onAttachedToWindow(); 163 | } 164 | 165 | @Override 166 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 167 | super.onSizeChanged(w, h, oldw, oldh); 168 | } 169 | 170 | public boolean onSwipe(MotionEvent event) { 171 | mGestureDetector.onTouchEvent(event); 172 | switch (event.getAction()) { 173 | case MotionEvent.ACTION_DOWN: 174 | mDownX = (int) event.getX(); 175 | isFling = false; 176 | break; 177 | case MotionEvent.ACTION_MOVE: 178 | // Log.i("byz", "downX = " + mDownX + ", moveX = " + event.getX()); 179 | int dis = (int) (mDownX - event.getX()); 180 | if (state == STATE_OPEN) { 181 | dis += mMenuView.getWidth() * mSwipeDirection; 182 | } 183 | swipe(dis); 184 | break; 185 | case MotionEvent.ACTION_UP: 186 | if ((isFling || Math.abs(mDownX - event.getX()) > (mMenuView.getWidth() / 2)) && 187 | Math.signum(mDownX - event.getX()) == mSwipeDirection) { 188 | // open 189 | smoothOpenMenu(); 190 | } else { 191 | // close 192 | smoothCloseMenu(); 193 | return false; 194 | } 195 | break; 196 | } 197 | return true; 198 | } 199 | 200 | public boolean isOpen() { 201 | return state == STATE_OPEN; 202 | } 203 | 204 | @Override 205 | public boolean onTouchEvent(MotionEvent event) { 206 | return super.onTouchEvent(event); 207 | } 208 | 209 | private void swipe(int dis) { 210 | if (Math.signum(dis) != mSwipeDirection) { 211 | dis = 0; 212 | } else if (Math.abs(dis) > mMenuView.getWidth()) { 213 | dis = mMenuView.getWidth() * mSwipeDirection; 214 | } 215 | 216 | mContentView.layout(-dis, mContentView.getTop(), 217 | mContentView.getWidth() - dis, getMeasuredHeight()); 218 | 219 | if (mSwipeDirection == SMListView.DIRECTION_LEFT) { 220 | 221 | mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(), 222 | mContentView.getWidth() + mMenuView.getWidth() - dis, 223 | mMenuView.getBottom()); 224 | } else { 225 | mMenuView.layout(-mMenuView.getWidth() - dis, mMenuView.getTop(), 226 | -dis, mMenuView.getBottom()); 227 | } 228 | } 229 | 230 | @Override 231 | public void computeScroll() { 232 | if (state == STATE_OPEN) { 233 | if (mOpenScroller.computeScrollOffset()) { 234 | swipe(mOpenScroller.getCurrX() * mSwipeDirection); 235 | postInvalidate(); 236 | } 237 | } else { 238 | if (mCloseScroller.computeScrollOffset()) { 239 | swipe((mBaseX - mCloseScroller.getCurrX()) * mSwipeDirection); 240 | postInvalidate(); 241 | } 242 | } 243 | } 244 | 245 | public void smoothCloseMenu() { 246 | state = STATE_CLOSE; 247 | if (mSwipeDirection == SMListView.DIRECTION_LEFT) { 248 | mBaseX = -mContentView.getLeft(); 249 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350); 250 | } else { 251 | mBaseX = mMenuView.getRight(); 252 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350); 253 | } 254 | postInvalidate(); 255 | } 256 | 257 | public void smoothOpenMenu() { 258 | state = STATE_OPEN; 259 | if (mSwipeDirection == SMListView.DIRECTION_LEFT) { 260 | mOpenScroller.startScroll(-mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350); 261 | } else { 262 | mOpenScroller.startScroll(mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350); 263 | } 264 | postInvalidate(); 265 | } 266 | 267 | public void closeMenu() { 268 | if (mCloseScroller.computeScrollOffset()) { 269 | mCloseScroller.abortAnimation(); 270 | } 271 | if (state == STATE_OPEN) { 272 | state = STATE_CLOSE; 273 | swipe(0); 274 | } 275 | } 276 | 277 | public void openMenu() { 278 | if (state == STATE_CLOSE) { 279 | state = STATE_OPEN; 280 | swipe(mMenuView.getWidth() * mSwipeDirection); 281 | } 282 | } 283 | 284 | public View getContentView() { 285 | return mContentView; 286 | } 287 | 288 | public SMView getMenuView() { 289 | return mMenuView; 290 | } 291 | 292 | private int dp2px(int dp) { 293 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 294 | getContext().getResources().getDisplayMetrics()); 295 | } 296 | 297 | @Override 298 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 299 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 300 | mMenuView.measure(MeasureSpec.makeMeasureSpec(0, 301 | MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec( 302 | getMeasuredHeight(), MeasureSpec.EXACTLY)); 303 | } 304 | 305 | @Override 306 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 307 | mContentView.layout(0, 0, getMeasuredWidth(), 308 | mContentView.getMeasuredHeight()); 309 | if (mSwipeDirection == SMListView.DIRECTION_LEFT) { 310 | mMenuView.layout(getMeasuredWidth(), 0, 311 | getMeasuredWidth() + mMenuView.getMeasuredWidth(), 312 | mContentView.getMeasuredHeight()); 313 | } else { 314 | mMenuView.layout(-mMenuView.getMeasuredWidth(), 0, 315 | 0, mContentView.getMeasuredHeight()); 316 | } 317 | } 318 | 319 | public void setMenuHeight(int measuredHeight) { 320 | Log.i("byz", "pos = " + position + ", height = " + measuredHeight); 321 | LayoutParams params = (LayoutParams) mMenuView.getLayoutParams(); 322 | if (params.height != measuredHeight) { 323 | params.height = measuredHeight; 324 | mMenuView.setLayoutParams(mMenuView.getLayoutParams()); 325 | } 326 | } 327 | } 328 | 329 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/expandview/SMLayoutView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.expandview; 2 | 3 | import android.content.Context; 4 | import android.support.v4.view.GestureDetectorCompat; 5 | import android.support.v4.widget.ScrollerCompat; 6 | import android.util.AttributeSet; 7 | import android.util.Log; 8 | import android.util.TypedValue; 9 | import android.view.GestureDetector; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.view.animation.Interpolator; 13 | import android.widget.AbsListView; 14 | import android.widget.FrameLayout; 15 | 16 | /** 17 | * SwipeMenuLayoutView 18 | * 19 | * @author CHY 2016/7/29. 20 | * 侧滑菜单的自定义布局 21 | */ 22 | public class SMLayoutView extends FrameLayout { 23 | 24 | private static final int CONTENT_VIEW_ID = 1; 25 | private static final int MENU_VIEW_ID = 2; 26 | 27 | private static final int STATE_CLOSE = 0; 28 | private static final int STATE_OPEN = 1; 29 | 30 | private int mSwipeDirection; 31 | 32 | private View mContentView; 33 | private SMExpandView mMenuView; 34 | private int mDownX; 35 | private int state = STATE_CLOSE; 36 | private GestureDetectorCompat mGestureDetector; 37 | private GestureDetector.OnGestureListener mGestureListener; 38 | private boolean isFling; 39 | private int MIN_FLING = dp2px(15); 40 | private int MAX_VELOCITYX = -dp2px(500); 41 | private ScrollerCompat mOpenScroller; 42 | private ScrollerCompat mCloseScroller; 43 | private int mBaseX; 44 | private int position; 45 | private Interpolator mCloseInterpolator; 46 | private Interpolator mOpenInterpolator; 47 | 48 | private boolean mSwipEnable = true; 49 | 50 | public SMLayoutView(View contentView, SMExpandView menuView) { 51 | this(contentView, menuView, null, null); 52 | } 53 | 54 | public SMLayoutView(View contentView, SMExpandView menuView, 55 | Interpolator closeInterpolator, Interpolator openInterpolator) { 56 | super(contentView.getContext()); 57 | mCloseInterpolator = closeInterpolator; 58 | mOpenInterpolator = openInterpolator; 59 | mContentView = contentView; 60 | mMenuView = menuView; 61 | mMenuView.setLayout(this); 62 | init(); 63 | } 64 | 65 | private SMLayoutView(Context context, AttributeSet attrs) { 66 | super(context, attrs); 67 | } 68 | 69 | private SMLayoutView(Context context) { 70 | super(context); 71 | } 72 | 73 | public int getPosition() { 74 | return position; 75 | } 76 | 77 | public void setPosition(int position) { 78 | this.position = position; 79 | mMenuView.setPosition(position); 80 | } 81 | 82 | public void setSwipeDirection(int swipeDirection) { 83 | mSwipeDirection = swipeDirection; 84 | } 85 | 86 | private void init() { 87 | setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 88 | LayoutParams.WRAP_CONTENT)); 89 | mGestureListener = new GestureDetector.SimpleOnGestureListener() { 90 | @Override 91 | public boolean onDown(MotionEvent e) { 92 | isFling = false; 93 | return true; 94 | } 95 | 96 | @Override 97 | public boolean onFling(MotionEvent e1, MotionEvent e2, 98 | float velocityX, float velocityY) { 99 | // TODO 100 | if (Math.abs(e1.getX() - e2.getX()) > MIN_FLING 101 | && velocityX < MAX_VELOCITYX) { 102 | isFling = true; 103 | } 104 | // Log.i("byz", MAX_VELOCITYX + ", velocityX = " + velocityX); 105 | return super.onFling(e1, e2, velocityX, velocityY); 106 | } 107 | }; 108 | mGestureDetector = new GestureDetectorCompat(getContext(), 109 | mGestureListener); 110 | 111 | // mScroller = ScrollerCompat.create(getContext(), new 112 | // BounceInterpolator()); 113 | if (mCloseInterpolator != null) { 114 | mCloseScroller = ScrollerCompat.create(getContext(), 115 | mCloseInterpolator); 116 | } else { 117 | mCloseScroller = ScrollerCompat.create(getContext()); 118 | } 119 | if (mOpenInterpolator != null) { 120 | mOpenScroller = ScrollerCompat.create(getContext(), 121 | mOpenInterpolator); 122 | } else { 123 | mOpenScroller = ScrollerCompat.create(getContext()); 124 | } 125 | 126 | LayoutParams contentParams = new LayoutParams( 127 | LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 128 | mContentView.setLayoutParams(contentParams); 129 | if (mContentView.getId() < 1) { 130 | mContentView.setId(CONTENT_VIEW_ID); 131 | } 132 | 133 | mMenuView.setId(MENU_VIEW_ID); 134 | mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 135 | LayoutParams.WRAP_CONTENT)); 136 | 137 | addView(mContentView); 138 | addView(mMenuView); 139 | 140 | // if (mContentView.getBackground() == null) { 141 | // mContentView.setBackgroundColor(Color.WHITE); 142 | // } 143 | 144 | // in android 2.x, MenuView height is MATCH_PARENT is not work. 145 | // getViewTreeObserver().addOnGlobalLayoutListener( 146 | // new OnGlobalLayoutListener() { 147 | // @Override 148 | // public void onGlobalLayout() { 149 | // setMenuHeight(mContentView.getHeight()); 150 | // // getViewTreeObserver() 151 | // // .removeGlobalOnLayoutListener(this); 152 | // } 153 | // }); 154 | 155 | } 156 | 157 | @Override 158 | protected void onAttachedToWindow() { 159 | super.onAttachedToWindow(); 160 | } 161 | 162 | @Override 163 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 164 | super.onSizeChanged(w, h, oldw, oldh); 165 | } 166 | 167 | public boolean onSwipe(MotionEvent event) { 168 | mGestureDetector.onTouchEvent(event); 169 | switch (event.getAction()) { 170 | case MotionEvent.ACTION_DOWN: 171 | mDownX = (int) event.getX(); 172 | isFling = false; 173 | break; 174 | case MotionEvent.ACTION_MOVE: 175 | // Log.i("byz", "downX = " + mDownX + ", moveX = " + event.getX()); 176 | int dis = (int) (mDownX - event.getX()); 177 | if (state == STATE_OPEN) { 178 | dis += mMenuView.getWidth() * mSwipeDirection; 179 | ; 180 | } 181 | swipe(dis); 182 | break; 183 | case MotionEvent.ACTION_UP: 184 | if ((isFling || Math.abs(mDownX - event.getX()) > (mMenuView.getWidth() / 2)) && 185 | Math.signum(mDownX - event.getX()) == mSwipeDirection) { 186 | // open 187 | smoothOpenMenu(); 188 | } else { 189 | // close 190 | smoothCloseMenu(); 191 | return false; 192 | } 193 | break; 194 | } 195 | return true; 196 | } 197 | 198 | public boolean isOpen() { 199 | return state == STATE_OPEN; 200 | } 201 | 202 | @Override 203 | public boolean onTouchEvent(MotionEvent event) { 204 | return super.onTouchEvent(event); 205 | } 206 | 207 | private void swipe(int dis) { 208 | if (!mSwipEnable) { 209 | return; 210 | } 211 | if (Math.signum(dis) != mSwipeDirection) { 212 | dis = 0; 213 | } else if (Math.abs(dis) > mMenuView.getWidth()) { 214 | dis = mMenuView.getWidth() * mSwipeDirection; 215 | } 216 | 217 | mContentView.layout(-dis, mContentView.getTop(), 218 | mContentView.getWidth() - dis, getMeasuredHeight()); 219 | 220 | if (mSwipeDirection == SMExpandableView.DIRECTION_LEFT) { 221 | 222 | mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(), 223 | mContentView.getWidth() + mMenuView.getWidth() - dis, 224 | mMenuView.getBottom()); 225 | } else { 226 | mMenuView.layout(-mMenuView.getWidth() - dis, mMenuView.getTop(), 227 | -dis, mMenuView.getBottom()); 228 | } 229 | } 230 | 231 | @Override 232 | public void computeScroll() { 233 | if (state == STATE_OPEN) { 234 | if (mOpenScroller.computeScrollOffset()) { 235 | swipe(mOpenScroller.getCurrX() * mSwipeDirection); 236 | postInvalidate(); 237 | } 238 | } else { 239 | if (mCloseScroller.computeScrollOffset()) { 240 | swipe((mBaseX - mCloseScroller.getCurrX()) * mSwipeDirection); 241 | postInvalidate(); 242 | } 243 | } 244 | } 245 | 246 | public void smoothCloseMenu() { 247 | state = STATE_CLOSE; 248 | if (mSwipeDirection == SMExpandableView.DIRECTION_LEFT) { 249 | mBaseX = -mContentView.getLeft(); 250 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350); 251 | } else { 252 | mBaseX = mMenuView.getRight(); 253 | mCloseScroller.startScroll(0, 0, mMenuView.getWidth(), 0, 350); 254 | } 255 | postInvalidate(); 256 | } 257 | 258 | public void smoothOpenMenu() { 259 | if (!mSwipEnable) { 260 | return; 261 | } 262 | state = STATE_OPEN; 263 | if (mSwipeDirection == SMExpandableView.DIRECTION_LEFT) { 264 | mOpenScroller.startScroll(-mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350); 265 | } else { 266 | mOpenScroller.startScroll(mContentView.getLeft(), 0, mMenuView.getWidth(), 0, 350); 267 | } 268 | postInvalidate(); 269 | } 270 | 271 | public void closeMenu() { 272 | if (mCloseScroller.computeScrollOffset()) { 273 | mCloseScroller.abortAnimation(); 274 | } 275 | if (state == STATE_OPEN) { 276 | state = STATE_CLOSE; 277 | swipe(0); 278 | } 279 | } 280 | 281 | public void openMenu() { 282 | if (!mSwipEnable) { 283 | return; 284 | } 285 | if (state == STATE_CLOSE) { 286 | state = STATE_OPEN; 287 | swipe(mMenuView.getWidth() * mSwipeDirection); 288 | } 289 | } 290 | 291 | public View getContentView() { 292 | return mContentView; 293 | } 294 | 295 | public SMExpandView getMenuView() { 296 | return mMenuView; 297 | } 298 | 299 | private int dp2px(int dp) { 300 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 301 | getContext().getResources().getDisplayMetrics()); 302 | } 303 | 304 | @Override 305 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 306 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 307 | mMenuView.measure(MeasureSpec.makeMeasureSpec(0, 308 | MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec( 309 | getMeasuredHeight(), MeasureSpec.EXACTLY)); 310 | } 311 | 312 | @Override 313 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 314 | mContentView.layout(0, 0, getMeasuredWidth(), 315 | mContentView.getMeasuredHeight()); 316 | if (mSwipeDirection == SMExpandableView.DIRECTION_LEFT) { 317 | mMenuView.layout(getMeasuredWidth(), 0, 318 | getMeasuredWidth() + mMenuView.getMeasuredWidth(), 319 | mContentView.getMeasuredHeight()); 320 | } else { 321 | mMenuView.layout(-mMenuView.getMeasuredWidth(), 0, 322 | 0, mContentView.getMeasuredHeight()); 323 | } 324 | } 325 | 326 | public void setMenuHeight(int measuredHeight) { 327 | Log.i("byz", "pos = " + position + ", height = " + measuredHeight); 328 | LayoutParams params = (LayoutParams) mMenuView.getLayoutParams(); 329 | if (params.height != measuredHeight) { 330 | params.height = measuredHeight; 331 | mMenuView.setLayoutParams(mMenuView.getLayoutParams()); 332 | } 333 | } 334 | 335 | public void setSwipEnable(boolean swipEnable) { 336 | mSwipEnable = swipEnable; 337 | } 338 | 339 | public boolean getSwipEnable() { 340 | return mSwipEnable; 341 | } 342 | } 343 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwipeMenuAndPullToRefresh 2 | 3 | ListView和ExpandableListView支持下拉刷新,上拉加载和侧滑删除三个功能, V0.1.8新增可根据条件设置某项不能侧滑的功能; 4 | 5 | Demo: 6 | ---- 7 |

8 | Screenshot 9 |

10 |

11 | Screenshot 12 |

13 |

14 | Screenshot 15 |

16 | 17 | ## Usage 18 | ### Add dependency 19 | 20 | ```groovy 21 | dependencies { 22 | compile 'com.github.heynchy:SwipeMenuAndPullToRefresh:v0.2.0' 23 | } 24 | 25 | ``` 26 | ## ListView全部功能的实现 27 | ### Step1. 添加xml 28 | ```xml 29 | 33 | 37 | 41 | /> 45 | 46 | ``` 47 | ### Step2. 添加初始化侧滑删除(如果不设置则不会有侧滑删除,仅有刷新) 48 | ```java 49 | final SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 50 | @Override 51 | public void create(SwipeMenu menu) { 52 | 53 | SwipeMenuItem deleteItem = new SwipeMenuItem(mContext); 54 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 55 | 0x3F, 0x25))); 56 | deleteItem.setWidth(dp2px(mContext, 100)); 57 | deleteItem.setTitle("删除"); 58 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 59 | deleteItem.setTitleSize(15); 60 | menu.addMenuItem(deleteItem); 61 | } 62 | }; 63 | // 设置侧滑删除(如果不设置则不会有侧滑删除,仅有刷新) 64 | mSWRListView.setMenuCreator(creator); 65 | // 点击侧滑按钮的响应事件(如果涉及自定义的侧滑布局,可参考SwipMenuListView的实现方法) 66 | // 侧滑的监听事件 67 | mSWRListView.setOnMenuItemClickListener(new SMListView.OnMenuItemClickListener() { 68 | @Override 69 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 70 | mDataList.remove(i); 71 | mAdapter.notifyDataSetChanged(); 72 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 73 | return false; 74 | } 75 | }); 76 | // 如果有侧滑删除事件,则可根据具体条件设置滑动项是否可以侧滑 77 | mSWRListView.setOnTouchListener(new View.OnTouchListener() { 78 | @Override 79 | public boolean onTouch(View v, MotionEvent event) { 80 | int action = event.getAction(); 81 | switch (action) { 82 | case MotionEvent.ACTION_DOWN: 83 | /** 84 | * oldPos 滑动的所处位置的position 85 | * setSwipeEnable() 是否进行侧滑: 86 | * 设置为false则不会发生侧滑; 87 | * 设置为true则会侧滑 88 | * 默认值为true 89 | * 90 | * 这里可根据具体的条件来判定是否可以进行滑动 91 | */ 92 | int oldPos = mSWRListView.pointToPosition((int) event.getX(), (int) event.getY()); 93 | if (oldPos < 0){ 94 | // 判定当没有找到侧滑的Item时,返回false 95 | return false; 96 | } 97 | if (oldPos < 5) { 98 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 99 | mSWRListView.setSwipeEnable(false); 100 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 101 | } else { 102 | mSWRListView.setSwipeEnable(true); 103 | } 104 | } 105 | return false; 106 | } 107 | }); 108 | ``` 109 | ### Step3. 监听下拉刷新和上拉加载的事件 110 | ```java 111 | // com.chy.srlibrary.PTRLayoutView ====> mPTRLayoutView 112 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 113 | @Override 114 | public void onRefresh(PTRLayoutView ptrLayoutView) { 115 | // 处理下拉刷新 116 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 117 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 118 | } 119 | 120 | @Override 121 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 122 | // 处理上拉加载 123 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 124 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 125 | } 126 | }); 127 | ``` 128 | ### 另: 如果单单需要侧滑删除则加入SMListView的xml后,代码中初始化同listview的step2 129 | 130 | ```xml 131 | 135 | ``` 136 | ## ExpandableListView全部功能的实现 137 | ### Step1. 添加xml 138 | ```xml 139 | 143 | 144 | 148 | 149 | 153 | 154 | /> 158 | 159 | 160 | ``` 161 | ### Step2. 添加初始化侧滑删除(如果不设置则不会有侧滑删除,仅有刷新) 162 | ```java 163 | // 设置侧滑的选项 164 | SwipeMenuCreatorInterfaceUtil creator = new SwipeMenuCreatorInterfaceUtil() { 165 | @Override 166 | public void create(SwipeMenu menu) { 167 | 168 | SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext()); 169 | deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 170 | 0x3F, 0x25))); 171 | deleteItem.setWidth(dp2px(60)); 172 | deleteItem.setTitle("删除"); 173 | deleteItem.setTitleColor(Color.rgb(255, 255, 255)); 174 | deleteItem.setTitleSize(15); 175 | menu.addMenuItem(deleteItem); 176 | } 177 | }; 178 | mSMRExpandView.setMenuCreator(creator); 179 | 180 | // 侧滑的监听事件 181 | mSMRExpandView.setOnMenuItemClickListener(new SMExpandableView.OnMenuItemClickListener() { 182 | @Override 183 | public boolean onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) { 184 | mDataList.remove(i); 185 | mAdapter.notifyDataSetChanged(); 186 | Toast.makeText(getApplicationContext(), "删除成功!", Toast.LENGTH_SHORT).show(); 187 | return false; 188 | } 189 | }); 190 | // 如果有侧滑删除事件,则可根据具体条件设置滑动项是否可以侧滑 191 | mSMRExpandView.setOnTouchListener(new View.OnTouchListener() { 192 | @Override 193 | public boolean onTouch(View v, MotionEvent event) { 194 | int action = event.getAction(); 195 | switch (action) { 196 | case MotionEvent.ACTION_DOWN: 197 | /** 198 | * oldPos 滑动的所处位置的position 199 | * setSwipeEnable() 是否进行侧滑: 200 | * 设置为false则不会发生侧滑; 201 | * 设置为true则会侧滑 202 | * 默认值为true 203 | * 204 | * 这里可根据具体的条件来判定是否可以进行滑动 205 | */ 206 | int oldPos = mSMRExpandView.pointToPosition((int) event.getX(), (int) event.getY()); 207 | if (oldPos < 0){ 208 | // 判定当没有找到侧滑的Item时,返回false 209 | return false; 210 | } 211 | if (oldPos < 5) { 212 | // 根据具体条件设置不滑动项(例如: position小于5时不滑动) 213 | mSMRExpandView.setSwipeEnable(false); 214 | Toast.makeText(getBaseContext(), "前5项不滑动!!", Toast.LENGTH_SHORT).show(); 215 | } else { 216 | mSMRExpandView.setSwipeEnable(true); 217 | } 218 | } 219 | return false; 220 | } 221 | }); 222 | ``` 223 | ### Step3. 监听下拉刷新和上拉加载的事件 224 | ```java 225 | mPTRLayoutView.setOnRefreshListener(new PTRLayoutView.OnRefreshListener() { 226 | @Override 227 | public void onRefresh(PTRLayoutView ptrLayoutView) { 228 | // 处理下拉刷新 229 | mAdapter.notifyDataSetChanged(); 230 | mPTRLayoutView.refreshFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 231 | Toast.makeText(getApplicationContext(), "刷新成功!", Toast.LENGTH_SHORT).show(); 232 | } 233 | 234 | @Override 235 | public void onLoadMore(PTRLayoutView ptrLayoutView) { 236 | // 处理上拉加载 237 | mAdapter.notifyDataSetChanged(); 238 | mPTRLayoutView.loadmoreFinish(PTRLayoutView.SUCCEED); // 此句话必须有(以结束加载) 239 | Toast.makeText(getApplicationContext(), "加载成功!", Toast.LENGTH_SHORT).show(); 240 | } 241 | }); 242 | ``` 243 | ### 另: 如果单单需要侧滑删除则加入SMRExpandView的xml后,代码中初始化同Expandablelistview的step2 244 | 245 | ```xml 246 | 250 | ``` 251 | 252 | ## 感谢以下两个项目的开发者: 253 | 1. https://github.com/jingchenUSTC/PullToRefreshAndLoad 254 | 2. https://github.com/baoyongzhang/SwipeMenuListView 255 | 256 | 257 | License 258 | ------ 259 | Copyright 2017-2019 heynchy 260 | 261 | Licensed under the Apache License, Version 2.0 (the "License"); 262 | you may not use this file except in compliance with the License. 263 | You may obtain a copy of the License at 264 | 265 | http://www.apache.org/licenses/LICENSE-2.0 266 | 267 | Unless required by applicable law or agreed to in writing, software 268 | distributed under the License is distributed on an "AS IS" BASIS, 269 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 270 | See the License for the specific language governing permissions and 271 | limitations under the License. 272 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/expandview/SMExpandableView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary.expandview; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.util.TypedValue; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.animation.Interpolator; 9 | import android.widget.ExpandableListAdapter; 10 | import android.widget.ExpandableListView; 11 | 12 | import com.chy.srlibrary.SwipeMenu; 13 | import com.chy.srlibrary.interfaceutil.SwipeMenuCreatorInterfaceUtil; 14 | 15 | 16 | /** 17 | * SwipeMenuListView 18 | * 19 | * @author CHY 2016/7/29. 20 | * ExpandableListView侧滑的自定义布局 21 | */ 22 | public class SMExpandableView extends ExpandableListView { 23 | 24 | private static final int TOUCH_STATE_NONE = 0; 25 | private static final int TOUCH_STATE_X = 1; 26 | private static final int TOUCH_STATE_Y = 2; 27 | 28 | public static final int DIRECTION_LEFT = 1; 29 | public static final int DIRECTION_RIGHT = -1; 30 | private int mDirection = 1;//swipe from right to left by default 31 | 32 | private int MAX_Y = 5; 33 | private int MAX_X = 3; 34 | private float mDownX; 35 | private float mDownY; 36 | private int mTouchState; 37 | private int mTouchPosition; 38 | private SMLayoutView mTouchView; 39 | private OnSwipeListener mOnSwipeListener; 40 | 41 | private SwipeMenuCreatorInterfaceUtil mMenuCreator; 42 | private OnMenuItemClickListener mOnMenuItemClickListener; 43 | private OnMenuStateChangeListener mOnMenuStateChangeListener; 44 | private Interpolator mCloseInterpolator; 45 | private Interpolator mOpenInterpolator; 46 | private boolean swipeEnable = true; // 标志位---是否允许左滑 47 | 48 | private boolean mState = true; 49 | private int mExpandCount = -1; 50 | 51 | public SMExpandableView(Context context) { 52 | super(context); 53 | init(); 54 | } 55 | 56 | public SMExpandableView(Context context, AttributeSet attrs, int defStyle) { 57 | super(context, attrs, defStyle); 58 | init(); 59 | } 60 | 61 | public SMExpandableView(Context context, AttributeSet attrs) { 62 | super(context, attrs); 63 | init(); 64 | } 65 | 66 | private void init() { 67 | MAX_X = dp2px(MAX_X); 68 | MAX_Y = dp2px(MAX_Y); 69 | mTouchState = TOUCH_STATE_NONE; 70 | } 71 | 72 | @Override 73 | public void setAdapter(ExpandableListAdapter adapter) { 74 | super.setAdapter(new SMExpandableAdapter(getContext(), adapter) { 75 | @Override 76 | public void createMenu(SwipeMenu menu) { 77 | if (mMenuCreator != null) { 78 | mMenuCreator.create(menu); 79 | } 80 | } 81 | 82 | @Override 83 | public void onItemClick(SMExpandView view, SwipeMenu menu, 84 | int index) { 85 | boolean flag = false; 86 | if (mOnMenuItemClickListener != null) { 87 | flag = mOnMenuItemClickListener.onMenuItemClick( 88 | view.getPosition(), menu, index); 89 | } 90 | if (mTouchView != null && !flag) { 91 | mTouchView.smoothCloseMenu(); 92 | } 93 | } 94 | }); 95 | } 96 | 97 | public void setCloseInterpolator(Interpolator interpolator) { 98 | mCloseInterpolator = interpolator; 99 | } 100 | 101 | public void setOpenInterpolator(Interpolator interpolator) { 102 | mOpenInterpolator = interpolator; 103 | } 104 | 105 | public Interpolator getOpenInterpolator() { 106 | return mOpenInterpolator; 107 | } 108 | 109 | public Interpolator getCloseInterpolator() { 110 | return mCloseInterpolator; 111 | } 112 | 113 | @Override 114 | public boolean onInterceptTouchEvent(MotionEvent ev) { 115 | //在拦截处处理,在滑动设置了点击事件的地方也能swip,点击时又不能影响原来的点击事件 116 | int action = ev.getAction(); 117 | switch (action) { 118 | case MotionEvent.ACTION_DOWN: 119 | mDownX = ev.getX(); 120 | mDownY = ev.getY(); 121 | boolean handled = super.onInterceptTouchEvent(ev); 122 | mTouchState = TOUCH_STATE_NONE; 123 | mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); 124 | View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); 125 | 126 | for (int i = 0; i < getCount(); i++) { 127 | if (isGroupExpanded(i)) { 128 | mState = false; 129 | mExpandCount = i; 130 | break; 131 | } else { 132 | mState = true; 133 | } 134 | } 135 | 136 | // 只在空的时候赋值 以免每次触摸都赋值,会有多个open状态 137 | if (view instanceof SMLayoutView) { 138 | // 如果有打开了 就拦截. 139 | if (mTouchView != null && mTouchView.isOpen() && !inRangeOfView(mTouchView.getMenuView(), ev)) { 140 | return true; 141 | } 142 | mTouchView = (SMLayoutView) view; 143 | mTouchView.setSwipeDirection(mDirection); 144 | } 145 | // 如果摸在另外个view 146 | if (mTouchView != null && mTouchView.isOpen() && view != mTouchView) { 147 | handled = true; 148 | } 149 | 150 | if (mTouchView != null) { 151 | mTouchView.onSwipe(ev); 152 | } 153 | return handled; 154 | case MotionEvent.ACTION_MOVE: 155 | float dy = Math.abs((ev.getY() - mDownY)); 156 | float dx = Math.abs((ev.getX() - mDownX)); 157 | if (Math.abs(dy) > MAX_Y || Math.abs(dx) > MAX_X) { 158 | // 每次拦截的down都把触摸状态设置成了TOUCH_STATE_NONE 只有返回true才会走onTouchEvent 所以写在这里就够了 159 | if (mTouchState == TOUCH_STATE_NONE) { 160 | if (Math.abs(dy) > MAX_Y) { 161 | mTouchState = TOUCH_STATE_Y; 162 | } else if (dx > MAX_X) { 163 | mTouchState = TOUCH_STATE_X; 164 | if (mOnSwipeListener != null) { 165 | mOnSwipeListener.onSwipeStart(mTouchPosition); 166 | } 167 | } 168 | } 169 | return true; 170 | } 171 | } 172 | return super.onInterceptTouchEvent(ev); 173 | } 174 | 175 | @Override 176 | public boolean onTouchEvent(MotionEvent ev) { 177 | if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) { 178 | return super.onTouchEvent(ev); 179 | } 180 | int action = ev.getAction(); 181 | switch (action) { 182 | case MotionEvent.ACTION_DOWN: 183 | int oldPos = mTouchPosition; 184 | mDownX = ev.getX(); 185 | mDownY = ev.getY(); 186 | mTouchState = TOUCH_STATE_NONE; 187 | swipeEnable = isSwipeEnable(); 188 | mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); 189 | 190 | if (mTouchPosition == oldPos && mTouchView != null 191 | && mTouchView.isOpen()) { 192 | mTouchState = TOUCH_STATE_X; 193 | mTouchView.onSwipe(ev); 194 | return true; 195 | } 196 | 197 | View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); 198 | if (mTouchView != null && mTouchView.isOpen()) { 199 | mTouchView.smoothCloseMenu(); 200 | mTouchView = null; 201 | // return super.onTouchEvent(ev); 202 | // try to cancel the touch event 203 | MotionEvent cancelEvent = MotionEvent.obtain(ev); 204 | cancelEvent.setAction(MotionEvent.ACTION_CANCEL); 205 | onTouchEvent(cancelEvent); 206 | if (mOnMenuStateChangeListener != null) { 207 | mOnMenuStateChangeListener.onMenuClose(oldPos); 208 | } 209 | return true; 210 | } 211 | if (view instanceof SMLayoutView) { 212 | mTouchView = (SMLayoutView) view; 213 | mTouchView.setSwipeDirection(mDirection); 214 | } 215 | if (mTouchView != null) { 216 | mTouchView.onSwipe(ev); 217 | } 218 | break; 219 | case MotionEvent.ACTION_MOVE: 220 | // 有些可能有header,要减去header再判断 221 | mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()) - getHeaderViewsCount(); 222 | 223 | /* 224 | *如果滑动了一下没完全展现,就收回去,这时候mTouchView已经赋值,再滑动另外一个不可以swip的view 225 | *会导致mTouchView swip 所以要用位置判断是否滑动的是一个view 226 | */ 227 | if (!mTouchView.getSwipEnable() || mTouchPosition != mTouchView.getPosition()) { 228 | if (!mState && (mTouchPosition != (mExpandCount + 1))) { 229 | mTouchPosition = mTouchView.getPosition(); 230 | } else { 231 | break; 232 | } 233 | 234 | } 235 | 236 | float dy = Math.abs((ev.getY() - mDownY)); 237 | float dx = Math.abs((ev.getX() - mDownX)); 238 | if (mTouchState == TOUCH_STATE_X) { 239 | if (mTouchView != null && swipeEnable) { 240 | mTouchView.onSwipe(ev); 241 | } 242 | getSelector().setState(new int[]{0}); 243 | ev.setAction(MotionEvent.ACTION_CANCEL); 244 | super.onTouchEvent(ev); 245 | return true; 246 | } else if (mTouchState == TOUCH_STATE_NONE) { 247 | 248 | if (Math.abs(dy) > MAX_Y) { 249 | mTouchState = TOUCH_STATE_Y; 250 | } else if (dx > MAX_X) { 251 | mTouchState = TOUCH_STATE_X; 252 | if (null != mOnSwipeListener) { 253 | mOnSwipeListener.onSwipeStart(mTouchPosition); 254 | } 255 | } 256 | } 257 | break; 258 | case MotionEvent.ACTION_UP: 259 | if (mTouchState == TOUCH_STATE_X) { 260 | if (mTouchView != null) { 261 | boolean isBeforeOpen = mTouchView.isOpen(); 262 | if (swipeEnable) { 263 | mTouchView.onSwipe(ev); 264 | } 265 | boolean isAfterOpen = mTouchView.isOpen(); 266 | if (isBeforeOpen != isAfterOpen && mOnMenuStateChangeListener != null) { 267 | if (isAfterOpen) { 268 | mOnMenuStateChangeListener.onMenuOpen(mTouchPosition); 269 | } else { 270 | mOnMenuStateChangeListener.onMenuClose(mTouchPosition); 271 | } 272 | } 273 | if (!isAfterOpen) { 274 | mTouchPosition = -1; 275 | mTouchView = null; 276 | } 277 | } 278 | if (mOnSwipeListener != null) { 279 | mOnSwipeListener.onSwipeEnd(mTouchPosition); 280 | } 281 | ev.setAction(MotionEvent.ACTION_CANCEL); 282 | super.onTouchEvent(ev); 283 | return true; 284 | } 285 | break; 286 | } 287 | 288 | return super.onTouchEvent(ev); 289 | } 290 | 291 | public boolean isSwipeEnable() { 292 | return swipeEnable; 293 | } 294 | 295 | public void setSwipeEnable(boolean swipeEnable) { 296 | this.swipeEnable = swipeEnable; 297 | } 298 | 299 | public void smoothOpenMenu(int position) { 300 | if (position >= getFirstVisiblePosition() 301 | && position <= getLastVisiblePosition()) { 302 | View view = getChildAt(position - getFirstVisiblePosition()); 303 | if (view instanceof SMLayoutView) { 304 | mTouchPosition = position; 305 | if (mTouchView != null && mTouchView.isOpen()) { 306 | mTouchView.smoothCloseMenu(); 307 | } 308 | mTouchView = (SMLayoutView) view; 309 | mTouchView.setSwipeDirection(mDirection); 310 | mTouchView.smoothOpenMenu(); 311 | } 312 | } 313 | } 314 | 315 | public void smoothCloseMenu() { 316 | if (mTouchView != null && mTouchView.isOpen()) { 317 | mTouchView.smoothCloseMenu(); 318 | } 319 | } 320 | 321 | private int dp2px(int dp) { 322 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 323 | getContext().getResources().getDisplayMetrics()); 324 | } 325 | 326 | public void setMenuCreator(SwipeMenuCreatorInterfaceUtil menuCreator) { 327 | this.mMenuCreator = menuCreator; 328 | } 329 | 330 | public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) { 331 | this.mOnMenuItemClickListener = onMenuItemClickListener; 332 | } 333 | 334 | public void setOnSwipeListener(OnSwipeListener onSwipeListener) { 335 | this.mOnSwipeListener = onSwipeListener; 336 | } 337 | 338 | 339 | public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeListener) { 340 | mOnMenuStateChangeListener = onMenuStateChangeListener; 341 | } 342 | 343 | public static interface OnMenuItemClickListener { 344 | boolean onMenuItemClick(int position, SwipeMenu menu, int index); 345 | } 346 | 347 | public static interface OnSwipeListener { 348 | void onSwipeStart(int position); 349 | 350 | void onSwipeEnd(int position); 351 | } 352 | 353 | public static interface OnMenuStateChangeListener { 354 | void onMenuOpen(int position); 355 | 356 | void onMenuClose(int position); 357 | 358 | } 359 | 360 | public void setSwipeDirection(int direction) { 361 | mDirection = direction; 362 | } 363 | 364 | /** 365 | * 判断点击事件是否在某个view内 366 | * 367 | * @param view 368 | * @param ev 369 | * @return 370 | */ 371 | public static boolean inRangeOfView(View view, MotionEvent ev) { 372 | int[] location = new int[2]; 373 | view.getLocationOnScreen(location); 374 | int x = location[0]; 375 | int y = location[1]; 376 | if (ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())) { 377 | return false; 378 | } 379 | return true; 380 | } 381 | } 382 | -------------------------------------------------------------------------------- /srlibrary/src/main/java/com/chy/srlibrary/PTRLayoutView.java: -------------------------------------------------------------------------------- 1 | package com.chy.srlibrary; 2 | 3 | /** 4 | * PullToRefreshLayout 5 | * 6 | * @author CHY 2016/7/25. 7 | * 自定义刷新布局 8 | */ 9 | 10 | 11 | import android.content.Context; 12 | import android.os.AsyncTask; 13 | import android.os.Handler; 14 | import android.os.Message; 15 | import android.util.AttributeSet; 16 | import android.util.Log; 17 | import android.view.MotionEvent; 18 | import android.view.View; 19 | import android.view.ViewGroup; 20 | import android.view.animation.AnimationUtils; 21 | import android.view.animation.LinearInterpolator; 22 | import android.view.animation.RotateAnimation; 23 | import android.widget.RelativeLayout; 24 | import android.widget.TextView; 25 | 26 | 27 | import com.chy.srlibrary.interfaceutil.PullableUtil; 28 | 29 | import java.util.Timer; 30 | import java.util.TimerTask; 31 | 32 | /** 33 | * 自定义的布局,用来管理三个子控件,其中一个是下拉头,一个是包含内容的pullableView(可以是实现Pullable接口的的任何View) 34 | */ 35 | public class PTRLayoutView extends RelativeLayout { 36 | public static final String TAG = "PullToRefreshLayout"; 37 | // 初始状态 38 | public static final int INIT = 0; 39 | // 释放刷新 40 | public static final int RELEASE_TO_REFRESH = 1; 41 | // 正在刷新 42 | public static final int REFRESHING = 2; 43 | // 释放加载 44 | public static final int RELEASE_TO_LOAD = 3; 45 | // 正在加载 46 | public static final int LOADING = 4; 47 | // 操作完毕 48 | public static final int DONE = 5; 49 | // 当前状态 50 | private int state = INIT; 51 | // 刷新回调接口 52 | private OnRefreshListener mListener; 53 | // 刷新成功 54 | public static final int SUCCEED = 0; 55 | // 刷新失败 56 | public static final int FAIL = 1; 57 | // 没有数据 58 | public static final int NOINFO = 2; 59 | // 按下Y坐标,上一个事件点Y坐标 60 | private float downY, lastY; 61 | 62 | private float downX; 63 | 64 | // 下拉的距离。注意:pullDownY和pullUpY不可能同时不为0 65 | public float pullDownY = 0; 66 | 67 | // 上拉的距离 68 | private float pullUpY = 0; 69 | 70 | // 释放刷新的距离 71 | private float refreshDist = 200; 72 | // 释放加载的距离 73 | private float loadmoreDist = 200; 74 | 75 | private MyTimer timer; 76 | // 回滚速度 77 | public float MOVE_SPEED = 8; 78 | // 第一次执行布局 79 | private boolean isLayout = false; 80 | // 在刷新过程中滑动操作 81 | private boolean isTouch = false; 82 | // 手指滑动距离与下拉头的滑动距离比,中间会随正切函数变化 83 | private float radio = 2; 84 | 85 | // 下拉箭头的转180°动画 86 | private RotateAnimation rotateAnimation; 87 | // 均匀旋转动画 88 | private RotateAnimation refreshingAnimation; 89 | 90 | // 下拉头 91 | private View refreshView; 92 | // 下拉的箭头 93 | private View pullView; 94 | // 正在刷新的图标 95 | private View refreshingView; 96 | // 刷新结果图标 97 | private View refreshStateImageView; 98 | // 刷新结果:成功或失败 99 | private TextView refreshStateTextView; 100 | 101 | // 上拉头 102 | private View loadmoreView; 103 | // 上拉的箭头 104 | private View pullUpView; 105 | // 正在加载的图标 106 | private View loadingView; 107 | // 加载结果图标 108 | private View loadStateImageView; 109 | // 加载结果:成功或失败 110 | private TextView loadStateTextView; 111 | 112 | // 实现了Pullable接口的View 113 | private View pullableView; 114 | // 过滤多点触碰 115 | private int mEvents; 116 | // 这两个变量用来控制pull的方向,如果不加控制,当情况满足可上拉又可下拉时没法下拉 117 | private boolean canPullDown = true; 118 | private boolean canPullUp = true; 119 | 120 | private Context mContext; 121 | 122 | /** 123 | * 执行自动回滚的handler 124 | */ 125 | Handler updateHandler = new Handler() { 126 | 127 | @Override 128 | public void handleMessage(Message msg) { 129 | // 回弹速度随下拉距离moveDeltaY增大而增大 130 | MOVE_SPEED = (float) (8 + 5 * Math.tan(Math.PI / 2 131 | / getMeasuredHeight() * (pullDownY + Math.abs(pullUpY)))); 132 | if (!isTouch) { 133 | // 正在刷新,且没有往上推的话则悬停,显示"正在刷新..." 134 | if (state == REFRESHING && pullDownY <= refreshDist) { 135 | pullDownY = refreshDist; 136 | Log.i("heyn", "pullDownY: " + pullDownY); 137 | timer.cancel(); 138 | } else if (state == LOADING && -pullUpY <= loadmoreDist) { 139 | pullUpY = -loadmoreDist; 140 | timer.cancel(); 141 | } 142 | 143 | } 144 | if (pullDownY > 0) 145 | pullDownY -= MOVE_SPEED; 146 | else if (pullUpY < 0) 147 | pullUpY += MOVE_SPEED; 148 | if (pullDownY < 0) { 149 | // 已完成回弹 150 | pullDownY = 0; 151 | pullView.clearAnimation(); 152 | // 隐藏下拉头时有可能还在刷新,只有当前状态不是正在刷新时才改变状态 153 | if (state != REFRESHING && state != LOADING) 154 | changeState(INIT); 155 | timer.cancel(); 156 | requestLayout(); 157 | } 158 | if (pullUpY > 0) { 159 | // 已完成回弹 160 | pullUpY = 0; 161 | pullUpView.clearAnimation(); 162 | // 隐藏上拉头时有可能还在刷新,只有当前状态不是正在刷新时才改变状态 163 | if (state != REFRESHING && state != LOADING) 164 | changeState(INIT); 165 | timer.cancel(); 166 | requestLayout(); 167 | } 168 | Log.d("handle", "handle"); 169 | // 刷新布局,会自动调用onLayout 170 | requestLayout(); 171 | // 没有拖拉或者回弹完成 172 | if (pullDownY + Math.abs(pullUpY) == 0) 173 | timer.cancel(); 174 | } 175 | 176 | }; 177 | 178 | public void setOnRefreshListener(OnRefreshListener listener) { 179 | mListener = listener; 180 | } 181 | 182 | public PTRLayoutView(Context context) { 183 | super(context); 184 | initView(context); 185 | } 186 | 187 | public PTRLayoutView(Context context, AttributeSet attrs) { 188 | super(context, attrs); 189 | initView(context); 190 | } 191 | 192 | public PTRLayoutView(Context context, AttributeSet attrs, int defStyle) { 193 | super(context, attrs, defStyle); 194 | initView(context); 195 | } 196 | 197 | private void initView(Context context) { 198 | mContext = context; 199 | timer = new MyTimer(updateHandler); 200 | rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation( 201 | context, R.anim.reverse_anim); 202 | refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation( 203 | context, R.anim.rotating); 204 | // 添加匀速转动动画 205 | LinearInterpolator lir = new LinearInterpolator(); 206 | rotateAnimation.setInterpolator(lir); 207 | refreshingAnimation.setInterpolator(lir); 208 | } 209 | 210 | private void hide() { 211 | timer.schedule(5); 212 | } 213 | 214 | /** 215 | * 完成刷新操作,显示刷新结果。注意:刷新完成后一定要调用这个方法 216 | */ 217 | /** 218 | * @param refreshResult PullToRefreshLayout.SUCCEED代表成功,PullToRefreshLayout.FAIL代表失败 219 | */ 220 | public void refreshFinish(int refreshResult) { 221 | refreshingView.clearAnimation(); 222 | refreshingView.setVisibility(View.GONE); 223 | switch (refreshResult) { 224 | case SUCCEED: 225 | // 刷新成功 226 | refreshStateImageView.setVisibility(View.VISIBLE); 227 | refreshStateTextView.setText(R.string.refresh_succeed); 228 | refreshStateImageView 229 | .setBackgroundResource(R.drawable.refresh_succeed); 230 | break; 231 | case FAIL: 232 | default: 233 | // 刷新失败 234 | refreshStateImageView.setVisibility(View.VISIBLE); 235 | refreshStateTextView.setText(R.string.refresh_fail); 236 | refreshStateImageView 237 | .setBackgroundResource(R.drawable.refresh_failed); 238 | break; 239 | } 240 | if (pullDownY > 0) { 241 | // 刷新结果停留1秒 242 | new Handler() { 243 | @Override 244 | public void handleMessage(Message msg) { 245 | changeState(DONE); 246 | hide(); 247 | } 248 | }.sendEmptyMessageDelayed(0, 1000); 249 | } else { 250 | changeState(DONE); 251 | hide(); 252 | } 253 | } 254 | 255 | /** 256 | * 加载完毕,显示加载结果。注意:加载完成后一定要调用这个方法 257 | * 258 | * @param refreshResult PullToRefreshLayout.SUCCEED代表成功,PullToRefreshLayout.FAIL代表失败 259 | */ 260 | public void loadmoreFinish(int refreshResult) { 261 | loadingView.clearAnimation(); 262 | loadingView.setVisibility(View.GONE); 263 | switch (refreshResult) { 264 | case SUCCEED: 265 | // 加载成功 266 | loadStateImageView.setVisibility(View.VISIBLE); 267 | loadStateTextView.setText(R.string.load_succeed); 268 | loadStateImageView.setBackgroundResource(R.drawable.load_succeed); 269 | break; 270 | case NOINFO: 271 | loadStateImageView.setVisibility(View.VISIBLE); 272 | loadStateTextView.setText("没有更多数据"); 273 | loadStateImageView.setBackgroundResource(R.drawable.load_failed); 274 | break; 275 | case FAIL: 276 | default: 277 | // 加载失败 278 | loadStateImageView.setVisibility(View.VISIBLE); 279 | loadStateTextView.setText(R.string.load_fail); 280 | loadStateImageView.setBackgroundResource(R.drawable.load_failed); 281 | break; 282 | } 283 | if (pullUpY < 0) { 284 | // 刷新结果停留1秒 285 | new Handler() { 286 | @Override 287 | public void handleMessage(Message msg) { 288 | changeState(DONE); 289 | hide(); 290 | } 291 | }.sendEmptyMessageDelayed(0, 1000); 292 | } else { 293 | changeState(DONE); 294 | hide(); 295 | } 296 | } 297 | 298 | private void changeState(int to) { 299 | state = to; 300 | switch (state) { 301 | case INIT: 302 | // 下拉布局初始状态 303 | refreshStateImageView.setVisibility(View.GONE); 304 | refreshStateTextView.setText(R.string.pull_to_refresh); 305 | pullView.clearAnimation(); 306 | pullView.setVisibility(View.VISIBLE); 307 | // 上拉布局初始状态 308 | loadStateImageView.setVisibility(View.GONE); 309 | loadStateTextView.setText(R.string.pullup_to_load); 310 | pullUpView.clearAnimation(); 311 | pullUpView.setVisibility(View.VISIBLE); 312 | break; 313 | case RELEASE_TO_REFRESH: 314 | // 释放刷新状态 315 | refreshStateTextView.setText(R.string.release_to_refresh); 316 | pullView.startAnimation(rotateAnimation); 317 | break; 318 | case REFRESHING: 319 | // 正在刷新状态 320 | pullView.clearAnimation(); 321 | refreshingView.setVisibility(View.VISIBLE); 322 | pullView.setVisibility(View.INVISIBLE); 323 | refreshingView.startAnimation(refreshingAnimation); 324 | refreshStateTextView.setText(R.string.refreshing); 325 | break; 326 | case RELEASE_TO_LOAD: 327 | // 释放加载状态 328 | loadStateTextView.setText(R.string.release_to_load); 329 | pullUpView.startAnimation(rotateAnimation); 330 | break; 331 | case LOADING: 332 | // 正在加载状态 333 | pullUpView.clearAnimation(); 334 | loadingView.setVisibility(View.VISIBLE); 335 | pullUpView.setVisibility(View.INVISIBLE); 336 | loadingView.startAnimation(refreshingAnimation); 337 | loadStateTextView.setText(R.string.loading); 338 | break; 339 | case DONE: 340 | // 刷新或加载完毕,啥都不做 341 | break; 342 | } 343 | } 344 | 345 | /** 346 | * 不限制上拉或下拉 347 | */ 348 | private void releasePull() { 349 | canPullDown = true; 350 | canPullUp = true; 351 | } 352 | 353 | /* 354 | * (非 Javadoc)由父控件决定是否分发事件,防止事件冲突 355 | * 356 | * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent) 357 | */ 358 | @Override 359 | public boolean dispatchTouchEvent(MotionEvent ev) { 360 | switch (ev.getActionMasked()) { 361 | case MotionEvent.ACTION_DOWN: 362 | downY = ev.getY(); 363 | downX = ev.getX(); 364 | lastY = downY; 365 | timer.cancel(); 366 | mEvents = 0; 367 | releasePull(); 368 | break; 369 | case MotionEvent.ACTION_POINTER_DOWN: 370 | case MotionEvent.ACTION_POINTER_UP: 371 | // 过滤多点触碰 372 | mEvents = -1; 373 | break; 374 | case MotionEvent.ACTION_MOVE: 375 | 376 | float dy = Math.abs((ev.getY() - downY)); 377 | float dx = Math.abs((ev.getX() - downX)); 378 | 379 | if (mEvents == 0 && Math.pow(dx, 2) / Math.pow(dy, 2) <= 3) { 380 | if (pullDownY > 0 381 | || (((PullableUtil) pullableView).canPullDown() 382 | && canPullDown && state != LOADING)) { 383 | // 可以下拉,正在加载时不能下拉 384 | // 对实际滑动距离做缩小,造成用力拉的感觉 385 | pullDownY = pullDownY + (ev.getY() - lastY) / radio; 386 | if (pullDownY < 0) { 387 | pullDownY = 0; 388 | canPullDown = false; 389 | canPullUp = true; 390 | } 391 | if (pullDownY > getMeasuredHeight()) 392 | pullDownY = getMeasuredHeight(); 393 | if (state == REFRESHING) { 394 | // 正在刷新的时候触摸移动 395 | isTouch = true; 396 | } 397 | } else if (pullUpY < 0 398 | || (((PullableUtil) pullableView).canPullUp() && canPullUp && state != REFRESHING)) { 399 | // 可以上拉,正在刷新时不能上拉 400 | pullUpY = pullUpY + (ev.getY() - lastY) / radio; 401 | if (pullUpY > 0) { 402 | pullUpY = 0; 403 | canPullDown = true; 404 | canPullUp = false; 405 | } 406 | if (pullUpY < -getMeasuredHeight()) 407 | pullUpY = -getMeasuredHeight(); 408 | if (state == LOADING) { 409 | // 正在加载的时候触摸移动 410 | isTouch = true; 411 | } 412 | } else 413 | releasePull(); 414 | } else 415 | mEvents = 0; 416 | lastY = ev.getY(); 417 | // 根据下拉距离改变比例 418 | radio = (float) (2 + 2 * Math.tan(Math.PI / 2 / getMeasuredHeight() 419 | * (pullDownY + Math.abs(pullUpY)))); 420 | if (pullDownY > 0 || pullUpY < 0) 421 | requestLayout(); 422 | if (pullDownY > 0) { 423 | if (pullDownY <= refreshDist 424 | && (state == RELEASE_TO_REFRESH || state == DONE)) { 425 | // 如果下拉距离没达到刷新的距离且当前状态是释放刷新,改变状态为下拉刷新 426 | changeState(INIT); 427 | } 428 | if (pullDownY >= refreshDist && state == INIT) { 429 | // 如果下拉距离达到刷新的距离且当前状态是初始状态刷新,改变状态为释放刷新 430 | changeState(RELEASE_TO_REFRESH); 431 | } 432 | } else if (pullUpY < 0) { 433 | // 下面是判断上拉加载的,同上,注意pullUpY是负值 434 | if (-pullUpY <= loadmoreDist 435 | && (state == RELEASE_TO_LOAD || state == DONE)) { 436 | changeState(INIT); 437 | } 438 | // 上拉操作 439 | if (-pullUpY >= loadmoreDist && state == INIT) { 440 | changeState(RELEASE_TO_LOAD); 441 | } 442 | 443 | } 444 | // 因为刷新和加载操作不能同时进行,所以pullDownY和pullUpY不会同时不为0,因此这里用(pullDownY + 445 | // Math.abs(pullUpY))就可以不对当前状态作区分了 446 | if ((pullDownY + Math.abs(pullUpY)) > 8) { 447 | // 防止下拉过程中误触发长按事件和点击事件 448 | ev.setAction(MotionEvent.ACTION_CANCEL); 449 | } 450 | break; 451 | case MotionEvent.ACTION_UP: 452 | if (pullDownY > refreshDist || -pullUpY > loadmoreDist) 453 | // 正在刷新时往下拉(正在加载时往上拉),释放后下拉头(上拉头)不隐藏 454 | { 455 | isTouch = false; 456 | } 457 | if (state == RELEASE_TO_REFRESH) { 458 | changeState(REFRESHING); 459 | // 刷新操作 460 | if (mListener != null) 461 | mListener.onRefresh(this); 462 | } else if (state == RELEASE_TO_LOAD) { 463 | changeState(LOADING); 464 | // 加载操作 465 | if (mListener != null) 466 | mListener.onLoadMore(this); 467 | } 468 | hide(); 469 | default: 470 | break; 471 | } 472 | // 事件分发交给父类 473 | super.dispatchTouchEvent(ev); 474 | return true; 475 | } 476 | 477 | /** 478 | * 自动模拟手指滑动的task 479 | */ 480 | private class AutoRefreshAndLoadTask extends 481 | AsyncTask { 482 | 483 | @Override 484 | protected String doInBackground(Integer... params) { 485 | while (pullDownY < 4 / 3 * refreshDist) { 486 | pullDownY += MOVE_SPEED; 487 | publishProgress(pullDownY); 488 | try { 489 | Thread.sleep(params[0]); 490 | } catch (InterruptedException e) { 491 | e.printStackTrace(); 492 | } 493 | } 494 | return null; 495 | } 496 | 497 | @Override 498 | protected void onPostExecute(String result) { 499 | changeState(REFRESHING); 500 | // 刷新操作 501 | if (mListener != null) 502 | mListener.onRefresh(PTRLayoutView.this); 503 | hide(); 504 | } 505 | 506 | @Override 507 | protected void onProgressUpdate(Float... values) { 508 | if (pullDownY > refreshDist) 509 | changeState(RELEASE_TO_REFRESH); 510 | requestLayout(); 511 | } 512 | 513 | } 514 | 515 | /** 516 | * 自动刷新 517 | */ 518 | public void autoRefresh() { 519 | AutoRefreshAndLoadTask task = new AutoRefreshAndLoadTask(); 520 | task.execute(20); 521 | } 522 | 523 | /** 524 | * 自动加载 525 | */ 526 | public void autoLoad() { 527 | pullUpY = -loadmoreDist; 528 | requestLayout(); 529 | changeState(LOADING); 530 | // 加载操作 531 | if (mListener != null) 532 | mListener.onLoadMore(this); 533 | } 534 | 535 | private void initView() { 536 | // 初始化下拉布局 537 | pullView = refreshView.findViewById(R.id.pull_icon); 538 | refreshStateTextView = (TextView) refreshView 539 | .findViewById(R.id.state_tv); 540 | refreshingView = refreshView.findViewById(R.id.refreshing_icon); 541 | refreshStateImageView = refreshView.findViewById(R.id.state_iv); 542 | // 初始化上拉布局 543 | pullUpView = loadmoreView.findViewById(R.id.pullup_icon); 544 | loadStateTextView = (TextView) loadmoreView 545 | .findViewById(R.id.loadstate_tv); 546 | loadingView = loadmoreView.findViewById(R.id.loading_icon); 547 | loadStateImageView = loadmoreView.findViewById(R.id.loadstate_iv); 548 | } 549 | 550 | @Override 551 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 552 | Log.d("Test", "Test"); 553 | if (!isLayout) { 554 | // 这里是第一次进来的时候做一些初始化 555 | refreshView = getChildAt(0); 556 | pullableView = getChildAt(1); 557 | loadmoreView = getChildAt(2); 558 | isLayout = true; 559 | initView(); 560 | refreshDist = ((ViewGroup) refreshView).getChildAt(0) 561 | .getMeasuredHeight(); 562 | loadmoreDist = ((ViewGroup) loadmoreView).getChildAt(0) 563 | .getMeasuredHeight(); 564 | } 565 | // 改变子控件的布局,这里直接用(pullDownY + pullUpY)作为偏移量,这样就可以不对当前状态作区分 566 | refreshView.layout(0, 567 | (int) (pullDownY + pullUpY) - refreshView.getMeasuredHeight(), 568 | refreshView.getMeasuredWidth(), (int) (pullDownY + pullUpY)); 569 | pullableView.layout(0, (int) (pullDownY + pullUpY), 570 | pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY) 571 | + pullableView.getMeasuredHeight()); 572 | loadmoreView.layout(0, 573 | (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(), 574 | loadmoreView.getMeasuredWidth(), 575 | (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight() 576 | + loadmoreView.getMeasuredHeight()); 577 | } 578 | 579 | class MyTimer { 580 | private Handler handler; 581 | private Timer timer; 582 | private MyTask mTask; 583 | 584 | public MyTimer(Handler handler) { 585 | this.handler = handler; 586 | timer = new Timer(); 587 | } 588 | 589 | public void schedule(long period) { 590 | if (mTask != null) { 591 | mTask.cancel(); 592 | mTask = null; 593 | } 594 | mTask = new MyTask(handler); 595 | timer.schedule(mTask, 0, period); 596 | } 597 | 598 | public void cancel() { 599 | if (mTask != null) { 600 | mTask.cancel(); 601 | mTask = null; 602 | } 603 | } 604 | 605 | class MyTask extends TimerTask { 606 | private Handler handler; 607 | 608 | public MyTask(Handler handler) { 609 | this.handler = handler; 610 | } 611 | 612 | @Override 613 | public void run() { 614 | handler.obtainMessage().sendToTarget(); 615 | } 616 | 617 | } 618 | } 619 | 620 | /** 621 | * 刷新加载回调接口 622 | * 623 | * @author chenjing 624 | */ 625 | public interface OnRefreshListener { 626 | /** 627 | * 刷新操作 628 | */ 629 | void onRefresh(PTRLayoutView PTRLayoutView); 630 | 631 | /** 632 | * 加载操作 633 | */ 634 | void onLoadMore(PTRLayoutView PTRLayoutView); 635 | } 636 | 637 | } 638 | 639 | --------------------------------------------------------------------------------