diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..71dc813 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +/captures + +# Built application files +*.apk +*.ap_ + + + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +/build +/*/build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Eclipse project files +.classpath +.project +.settings/ + +# Intellij project files +*.iml +*.ipr +*.iws +.idea/ + +# System files +.DS_Store \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..4af0110 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + buildToolsVersion "26.0.1" + defaultConfig { + applicationId "com.donkingliang.headerviewadapterdemo" + minSdkVersion 15 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:26.+' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + testCompile 'junit:junit:4.12' + + compile project(":headerviewadapter") +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..4ce37d1 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\Administrator\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/donkingliang/headerviewadapterdemo/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/donkingliang/headerviewadapterdemo/ExampleInstrumentedTest.java new file mode 100644 index 0000000..81ba7d1 --- /dev/null +++ b/app/src/androidTest/java/com/donkingliang/headerviewadapterdemo/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.donkingliang.headerviewadapterdemo; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.donkingliang.headerviewadapterdemo", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8414072 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/donkingliang/headerviewadapterdemo/GridAdapter.java b/app/src/main/java/com/donkingliang/headerviewadapterdemo/GridAdapter.java new file mode 100644 index 0000000..c9ba1a4 --- /dev/null +++ b/app/src/main/java/com/donkingliang/headerviewadapterdemo/GridAdapter.java @@ -0,0 +1,49 @@ +package com.donkingliang.headerviewadapterdemo; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * Depiction: + * Author:lry + * Dat:2017/11/10 + */ +public class GridAdapter extends RecyclerView.Adapter { + + private Context mContext; + + public GridAdapter(Context context) { + mContext = context; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_goods_grid, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(ViewHolder holder, final int position) { + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Log.e("TAG", "position = " + position); + } + }); + } + + @Override + public int getItemCount() { + return 10; + } + + static class ViewHolder extends RecyclerView.ViewHolder { + ViewHolder(View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/com/donkingliang/headerviewadapterdemo/LinearAdapter.java b/app/src/main/java/com/donkingliang/headerviewadapterdemo/LinearAdapter.java new file mode 100644 index 0000000..89fcf0d --- /dev/null +++ b/app/src/main/java/com/donkingliang/headerviewadapterdemo/LinearAdapter.java @@ -0,0 +1,49 @@ +package com.donkingliang.headerviewadapterdemo; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * Depiction: + * Author:lry + * Dat:2017/11/10 + */ +public class LinearAdapter extends RecyclerView.Adapter { + + private Context mContext; + + public LinearAdapter(Context context) { + mContext = context; + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_goods_linear, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(ViewHolder holder, final int position) { + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Log.e("TAG", "position = " + position); + } + }); + } + + @Override + public int getItemCount() { + return 10; + } + + static class ViewHolder extends RecyclerView.ViewHolder { + ViewHolder(View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/com/donkingliang/headerviewadapterdemo/MainActivity.java b/app/src/main/java/com/donkingliang/headerviewadapterdemo/MainActivity.java new file mode 100644 index 0000000..720fbe7 --- /dev/null +++ b/app/src/main/java/com/donkingliang/headerviewadapterdemo/MainActivity.java @@ -0,0 +1,52 @@ +package com.donkingliang.headerviewadapterdemo; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; + +import com.donkingliang.headerviewadapter.HeaderViewAdapter; +import com.donkingliang.headerviewadapter.HeaderViewGridLayoutManager; + +public class MainActivity extends AppCompatActivity { + + private RecyclerView rvList; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + rvList = (RecyclerView) findViewById(R.id.rv_list); + showLinearList(); +// showGridList(); + } + + private void showLinearList() { + LinearAdapter adapter = new LinearAdapter(this); + HeaderViewAdapter headerViewAdapter = new HeaderViewAdapter(adapter); + rvList.setLayoutManager(new LinearLayoutManager(this)); + View hv1 = LayoutInflater.from(this).inflate(R.layout.layout_header, rvList, false); + View hv2 = LayoutInflater.from(this).inflate(R.layout.layout_header_view, rvList, false); + View fv = LayoutInflater.from(this).inflate(R.layout.layout_footer_view, rvList, false); + headerViewAdapter.addHeaderView(hv1); + headerViewAdapter.addHeaderView(hv2); + headerViewAdapter.addFooterView(fv); + rvList.setAdapter(headerViewAdapter); + } + + private void showGridList() { + GridAdapter adapter = new GridAdapter(this); + HeaderViewAdapter headerViewAdapter = new HeaderViewAdapter(adapter); + rvList.setLayoutManager(new HeaderViewGridLayoutManager(this, 2, headerViewAdapter)); + View hv1 = LayoutInflater.from(this).inflate(R.layout.layout_header, rvList, false); + View hv2 = LayoutInflater.from(this).inflate(R.layout.layout_header_view, rvList, false); + View fv = LayoutInflater.from(this).inflate(R.layout.layout_footer_view, rvList, false); + headerViewAdapter.addHeaderView(hv1); + headerViewAdapter.addHeaderView(hv2); + headerViewAdapter.addFooterView(fv); + rvList.setAdapter(headerViewAdapter); + } +} diff --git a/app/src/main/res/drawable-xhdpi/goods_image.jpg b/app/src/main/res/drawable-xhdpi/goods_image.jpg new file mode 100644 index 0000000..aed6122 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/goods_image.jpg differ diff --git a/app/src/main/res/drawable-xhdpi/header_image.jpg b/app/src/main/res/drawable-xhdpi/header_image.jpg new file mode 100644 index 0000000..4d7756b Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/header_image.jpg differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..74074e0 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/app/src/main/res/layout/adapter_goods_grid.xml b/app/src/main/res/layout/adapter_goods_grid.xml new file mode 100644 index 0000000..c943fac --- /dev/null +++ b/app/src/main/res/layout/adapter_goods_grid.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/adapter_goods_linear.xml b/app/src/main/res/layout/adapter_goods_linear.xml new file mode 100644 index 0000000..640222f --- /dev/null +++ b/app/src/main/res/layout/adapter_goods_linear.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/layout/layout_footer_view.xml b/app/src/main/res/layout/layout_footer_view.xml new file mode 100644 index 0000000..6d5b6d8 --- /dev/null +++ b/app/src/main/res/layout/layout_footer_view.xml @@ -0,0 +1,18 @@ + + + + + + diff --git a/app/src/main/res/layout/layout_header.xml b/app/src/main/res/layout/layout_header.xml new file mode 100644 index 0000000..58a36c2 --- /dev/null +++ b/app/src/main/res/layout/layout_header.xml @@ -0,0 +1,6 @@ + + diff --git a/app/src/main/res/layout/layout_header_view.xml b/app/src/main/res/layout/layout_header_view.xml new file mode 100644 index 0000000..660067d --- /dev/null +++ b/app/src/main/res/layout/layout_header_view.xml @@ -0,0 +1,18 @@ + + + + + + diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..9a078e3 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..efc028a Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..3af2608 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..9bec2e6 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..34947cd Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..3ab3e9c --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3F51B5 + #303F9F + #FF4081 + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..448f1c7 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + HeaderViewAdapter + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..5885930 --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/app/src/test/java/com/donkingliang/headerviewadapterdemo/ExampleUnitTest.java b/app/src/test/java/com/donkingliang/headerviewadapterdemo/ExampleUnitTest.java new file mode 100644 index 0000000..9951aa4 --- /dev/null +++ b/app/src/test/java/com/donkingliang/headerviewadapterdemo/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.donkingliang.headerviewadapterdemo; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() throws Exception { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..d0aa704 --- /dev/null +++ b/build.gradle @@ -0,0 +1,23 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.3.2' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..aac7c9b --- /dev/null +++ b/gradle.properties @@ -0,0 +1,17 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..13372ae Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..06102c1 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Nov 10 09:14:28 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..9d82f78 --- /dev/null +++ b/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..8a0b282 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/headerviewadapter/.gitignore b/headerviewadapter/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/headerviewadapter/.gitignore @@ -0,0 +1 @@ +/build diff --git a/headerviewadapter/build.gradle b/headerviewadapter/build.gradle new file mode 100644 index 0000000..2406aea --- /dev/null +++ b/headerviewadapter/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 26 + buildToolsVersion "26.0.1" + + defaultConfig { + minSdkVersion 15 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:26.+' + testCompile 'junit:junit:4.12' + compile 'com.android.support:recyclerview-v7:26.+' +} diff --git a/headerviewadapter/proguard-rules.pro b/headerviewadapter/proguard-rules.pro new file mode 100644 index 0000000..4ce37d1 --- /dev/null +++ b/headerviewadapter/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\Administrator\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/headerviewadapter/src/androidTest/java/com/donkingliang/headerviewadapter/ExampleInstrumentedTest.java b/headerviewadapter/src/androidTest/java/com/donkingliang/headerviewadapter/ExampleInstrumentedTest.java new file mode 100644 index 0000000..045b6f8 --- /dev/null +++ b/headerviewadapter/src/androidTest/java/com/donkingliang/headerviewadapter/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.donkingliang.headerviewadapter; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.donkingliang.headerviewadapter.test", appContext.getPackageName()); + } +} diff --git a/headerviewadapter/src/main/AndroidManifest.xml b/headerviewadapter/src/main/AndroidManifest.xml new file mode 100644 index 0000000..3d511d3 --- /dev/null +++ b/headerviewadapter/src/main/AndroidManifest.xml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewAdapter.java b/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewAdapter.java new file mode 100644 index 0000000..fdd3b3c --- /dev/null +++ b/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewAdapter.java @@ -0,0 +1,354 @@ +package com.donkingliang.headerviewadapter; + +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.StaggeredGridLayoutManager; +import android.view.View; +import android.view.ViewGroup; + +import java.util.ArrayList; +import java.util.List; + +/** + * Depiction: HeaderViewAdapter是对已有的RecyclerView.Adapter进行包装, + * 使其具备给列表添加头部({@link HeaderViewAdapter#addHeaderView(View)}) + * 和添加尾部({@link HeaderViewAdapter#addFooterView(View)})的功能。 + * 它的效果就类似已ListView的{@link android.widget.ListView#addHeaderView(View)} + * 和 {@link android.widget.ListView#addFooterView(View)} (View)} + *

+ * Author:donkingliang QQ:1043214265 + * Dat:2017/11/10 + */ +public class HeaderViewAdapter extends RecyclerView.Adapter { + + //被包装的Adapter。 + private RecyclerView.Adapter mAdapter; + + //用于存放HeaderView + private final List mHeaderViewInfos = new ArrayList<>(); + + //用于存放FooterView + private final List mFooterViewInfos = new ArrayList<>(); + + //用于监听被包装的Adapter的数据变化的监听器。它将被包装的Adapter的数据变化映射成HeaderViewAdapter的变化。 + private RecyclerView.AdapterDataObserver mObserver = new RecyclerView.AdapterDataObserver() { + @Override + public void onChanged() { + notifyDataSetChanged(); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount) { + super.onItemRangeChanged(positionStart, itemCount); + } + + @Override + public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { + notifyItemRangeChanged(getHeadersCount() + positionStart, itemCount, payload); + } + + @Override + public void onItemRangeInserted(int positionStart, int itemCount) { + notifyItemRangeInserted(getHeadersCount() + positionStart, itemCount); + } + + @Override + public void onItemRangeRemoved(int positionStart, int itemCount) { + notifyItemRangeRemoved(getHeadersCount() + positionStart, itemCount); + } + + @Override + public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { + notifyItemMoved(getHeadersCount() + fromPosition, getHeadersCount() + toPosition); + } + }; + + public HeaderViewAdapter(RecyclerView.Adapter adapter) { + this.mAdapter = adapter; + if (mAdapter != null) { + mAdapter.registerAdapterDataObserver(mObserver); + } + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + // 根据viewType查找对应的HeaderView 或 FooterView。如果没有找到则表示该viewType是普通的列表项。 + View view = findViewForInfos(viewType); + if (view != null) { + return new ViewHolder(view); + } else { + return mAdapter.onCreateViewHolder(parent, viewType); + } + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + // 如果是HeaderView 或者是 FooterView则不绑定数据。 + // 因为HeaderView和FooterView是由外部传进来的,它们不由列表去更新。 + if (isHeader(position) || isFooter(position)) { + return; + } + //将列表实际的position调整成mAdapter对应的position。 + int adjPosition = position - getHeadersCount(); + mAdapter.onBindViewHolder(holder, adjPosition); + } + + @Override + public int getItemCount() { + return mHeaderViewInfos.size() + mFooterViewInfos.size() + + (mAdapter == null ? 0 : mAdapter.getItemCount()); + } + + @Override + public int getItemViewType(int position) { + //如果当前item是HeaderView,则返回HeaderView对应的itemViewType。 + if (isHeader(position)) { + return mHeaderViewInfos.get(position).itemViewType; + } + + //如果当前item是HeaderView,则返回HeaderView对应的itemViewType。 + if (isFooter(position)) { + return mFooterViewInfos.get(position - mHeaderViewInfos.size() - mAdapter.getItemCount()).itemViewType; + } + + //将列表实际的position调整成mAdapter对应的position。 + int adjPosition = position - getHeadersCount(); + return mAdapter.getItemViewType(adjPosition); + } + + /** + * 判断当前位置是否是头部View。 + * + * @param position 这里的position是整个列表(包含HeaderView和FooterView)的position。 + * @return + */ + public boolean isHeader(int position) { + return position < getHeadersCount(); + } + + /** + * 判断当前位置是否是尾部View。 + * + * @param position 这里的position是整个列表(包含HeaderView和FooterView)的position。 + * @return + */ + public boolean isFooter(int position) { + return getItemCount() - position <= getFootersCount(); + } + + /** + * 获取HeaderView的个数 + * + * @return + */ + public int getHeadersCount() { + return mHeaderViewInfos.size(); + } + + /** + * 获取FooterView的个数 + * + * @return + */ + public int getFootersCount() { + return mFooterViewInfos.size(); + } + + /** + * 添加HeaderView + * + * @param view + */ + public void addHeaderView(View view) { + addHeaderView(view, generateUniqueViewType()); + } + + private void addHeaderView(View view, int viewType) { + //包装HeaderView数据并添加到列表 + FixedViewInfo info = new FixedViewInfo(); + info.view = view; + info.itemViewType = viewType; + mHeaderViewInfos.add(info); + notifyDataSetChanged(); + } + + /** + * 删除HeaderView + * + * @param view + * @return 是否删除成功 + */ + public boolean removeHeaderView(View view) { + for (FixedViewInfo info : mHeaderViewInfos) { + if (info.view == view) { + mHeaderViewInfos.remove(info); + notifyDataSetChanged(); + return true; + } + } + return false; + } + + /** + * 添加FooterView + * + * @param view + */ + public void addFooterView(View view) { + addFooterView(view, generateUniqueViewType()); + } + + private void addFooterView(View view, int viewType) { + // 包装FooterView数据并添加到列表 + FixedViewInfo info = new FixedViewInfo(); + info.view = view; + info.itemViewType = viewType; + mFooterViewInfos.add(info); + notifyDataSetChanged(); + } + + /** + * 删除FooterView + * + * @param view + * @return 是否删除成功 + */ + public boolean removeFooterView(View view) { + for (FixedViewInfo info : mFooterViewInfos) { + if (info.view == view) { + mFooterViewInfos.remove(info); + notifyDataSetChanged(); + return true; + } + } + return false; + } + + /** + * 生成一个唯一的数,用于标识HeaderView或FooterView的type类型,并且保证类型不会重复。 + * + * @return + */ + private int generateUniqueViewType() { + int count = getItemCount(); + while (true) { + int viewType = (int) (Math.random() * Integer.MAX_VALUE) + 1; + boolean isExist = false; + for (int i = 0; i < count; i++) { + if (viewType == getItemViewType(i)) { + isExist = true; + break; + } + } + + if (!isExist) { + return viewType; + } + } + } + + /** + * 根据viewType查找对应的HeaderView 或 FooterView。没有找到则返回null。 + * + * @param viewType 查找的viewType + * @return + */ + private View findViewForInfos(int viewType) { + for (FixedViewInfo info : mHeaderViewInfos) { + if (info.itemViewType == viewType) { + return info.view; + } + } + + for (FixedViewInfo info : mFooterViewInfos) { + if (info.itemViewType == viewType) { + return info.view; + } + } + + return null; + } + + @Override + public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) { + if (holder instanceof ViewHolder) { + super.onViewAttachedToWindow(holder); + } else { + mAdapter.onViewAttachedToWindow(holder); + } + + //处理StaggeredGridLayout,保证HeaderView和FooterView占满一行。 + if (isStaggeredGridLayout(holder)) { + handleLayoutIfStaggeredGridLayout(holder, holder.getLayoutPosition()); + } + } + + private boolean isStaggeredGridLayout(RecyclerView.ViewHolder holder) { + ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); + if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) { + return true; + } + return false; + } + + private void handleLayoutIfStaggeredGridLayout(RecyclerView.ViewHolder holder, int position) { + if (isHeader(position) || isFooter(position)) { + StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) + holder.itemView.getLayoutParams(); + p.setFullSpan(true); + } + } + + @Override + public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) { + if (holder instanceof ViewHolder) { + super.onViewDetachedFromWindow(holder); + } else { + mAdapter.onViewDetachedFromWindow(holder); + } + } + + @Override + public boolean onFailedToRecycleView(RecyclerView.ViewHolder holder) { + if (holder instanceof ViewHolder) { + return super.onFailedToRecycleView(holder); + } else { + return mAdapter.onFailedToRecycleView(holder); + } + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + mAdapter.onAttachedToRecyclerView(recyclerView); + } + + @Override + public void onDetachedFromRecyclerView(RecyclerView recyclerView) { + mAdapter.onDetachedFromRecyclerView(recyclerView); + } + + @Override + public void onViewRecycled(RecyclerView.ViewHolder holder) { + if (holder instanceof ViewHolder) { + super.onViewRecycled(holder); + } else { + mAdapter.onViewRecycled(holder); + } + } + + /** + * 用于包装HeaderView和FooterView的数据类 + */ + private class FixedViewInfo { + //保存HeaderView或FooterView + View view; + + //保存HeaderView或FooterView对应的viewType。 + int itemViewType; + } + + private static class ViewHolder extends RecyclerView.ViewHolder { + ViewHolder(View itemView) { + super(itemView); + } + } +} diff --git a/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewGridLayoutManager.java b/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewGridLayoutManager.java new file mode 100644 index 0000000..ecdcd1d --- /dev/null +++ b/headerviewadapter/src/main/java/com/donkingliang/headerviewadapter/HeaderViewGridLayoutManager.java @@ -0,0 +1,73 @@ +package com.donkingliang.headerviewadapter; + +import android.content.Context; +import android.support.v7.widget.GridLayoutManager; +import android.util.AttributeSet; + +/** + * Depiction: HeaderViewGridLayoutManager是为适配RecyclerView同时使用{@link HeaderViewAdapter} + * 和{@link GridLayoutManager}而提供的一个GridLayoutManager子类。它保证了RecyclerView在使用GridLayoutManager + * 的时候,HeaderView 和 FooterView 依然可以占满一行。所以如果你要同时使用HeaderViewAdapter和GridLayoutManager, + * 你就要使用HeaderViewGridLayoutManager。 + *

+ * Author:donkingliang QQ:1043214265 + * Dat:2017/11/10 + */ +public class HeaderViewGridLayoutManager extends GridLayoutManager { + + private HeaderViewAdapter mAdapter; + + public HeaderViewGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes, HeaderViewAdapter adapter) { + super(context, attrs, defStyleAttr, defStyleRes); + this.mAdapter = adapter; + setSpanSizeLookup(); + } + + public HeaderViewGridLayoutManager(Context context, int spanCount, HeaderViewAdapter adapter) { + super(context, spanCount); + this.mAdapter = adapter; + setSpanSizeLookup(); + } + + public HeaderViewGridLayoutManager(Context context, int spanCount, int orientation, + boolean reverseLayout, HeaderViewAdapter adapter) { + super(context, spanCount, orientation, reverseLayout); + this.mAdapter = adapter; + setSpanSizeLookup(); + } + + private void setSpanSizeLookup() { + super.setSpanSizeLookup(new SpanSizeLookup() { + @Override + public int getSpanSize(int position) { + if (mAdapter != null) { + if (mAdapter.isHeader(position) || mAdapter.isFooter(position)) { + return getSpanCount(); + } else { + int adjPosition = position - mAdapter.getHeadersCount(); + return getChildSpanSize(adjPosition); + } + } else { + return 1; + } + } + }); + } + + /** + * 提供这个方法可以使外部改变普通列表项的SpanSize。 + * 这个方法的作用跟{@link SpanSizeLookup#getSpanSize(int)}一样。 + * + * @param position 去掉HeaderView和FooterView后的position。 + * @return + */ + public int getChildSpanSize(int position) { + return 1; + } + + // 不允许外部设置SpanSizeLookup。 + @Override + public void setSpanSizeLookup(SpanSizeLookup spanSizeLookup) { + } +} diff --git a/headerviewadapter/src/main/res/values/strings.xml b/headerviewadapter/src/main/res/values/strings.xml new file mode 100644 index 0000000..1d51d9a --- /dev/null +++ b/headerviewadapter/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + headerviewadapter + diff --git a/headerviewadapter/src/test/java/com/donkingliang/headerviewadapter/ExampleUnitTest.java b/headerviewadapter/src/test/java/com/donkingliang/headerviewadapter/ExampleUnitTest.java new file mode 100644 index 0000000..c578595 --- /dev/null +++ b/headerviewadapter/src/test/java/com/donkingliang/headerviewadapter/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.donkingliang.headerviewadapter; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() throws Exception { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..48bc01f --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +include ':app', ':headerviewadapter'