From 9d84205a7573b5729040c27d9ce67778c4c2deb3 Mon Sep 17 00:00:00 2001 From: C Date: Tue, 15 Jun 2021 16:58:59 +0800 Subject: [PATCH] Migrate to AndroidX, flex box demo: #26 [ci skip] --- app/build.gradle | 23 ++-- app/src/main/AndroidManifest.xml | 1 + .../github/nukc/sample/AnotherActivity.java | 7 +- .../github/nukc/sample/FlexboxActivity.java | 99 ++++++++++++++++++ .../github/nukc/sample/Issue9Activity.java | 9 +- .../com/github/nukc/sample/MainActivity.java | 7 +- ...howFooterWhenNotCoveredScreenActivity.java | 7 +- .../github/nukc/sample/SampleActivity.java | 14 +-- .../github/nukc/sample/SampleFragment.java | 15 +-- .../github/nukc/sample/ScrollingActivity.java | 13 +-- app/src/main/res/layout/activity_another.xml | 2 +- app/src/main/res/layout/activity_flexbox.xml | 14 +++ app/src/main/res/layout/activity_issue9.xml | 6 +- app/src/main/res/layout/activity_main.xml | 8 ++ app/src/main/res/layout/activity_sample.xml | 4 +- .../main/res/layout/activity_scrolling.xml | 18 ++-- app/src/main/res/layout/fragment_sample.xml | 2 +- app/src/main/res/layout/item_flex.xml | 20 ++++ app/src/main/res/layout/view_flex_loading.xml | 17 +++ app/src/main/res/layout/view_flex_no_more.xml | 20 ++++ app/src/main/res/layout/view_flex_retry.xml | 21 ++++ app/src/main/res/values/strings.xml | 1 + build.gradle | 2 +- gradle.properties | 4 +- gradle/wrapper/gradle-wrapper.jar | Bin 53636 -> 54417 bytes gradle/wrapper/gradle-wrapper.properties | 3 +- gradlew | 72 +++++++------ gradlew.bat | 14 +-- loadmorewrapper/build.gradle | 20 ++-- .../nukc/LoadMoreWrapper/LoadMoreAdapter.java | 15 +-- .../nukc/LoadMoreWrapper/LoadMoreHelper.java | 5 +- .../nukc/LoadMoreWrapper/LoadMoreWrapper.java | 5 +- 32 files changed, 344 insertions(+), 124 deletions(-) create mode 100644 app/src/main/java/com/github/nukc/sample/FlexboxActivity.java create mode 100644 app/src/main/res/layout/activity_flexbox.xml create mode 100644 app/src/main/res/layout/item_flex.xml create mode 100644 app/src/main/res/layout/view_flex_loading.xml create mode 100644 app/src/main/res/layout/view_flex_no_more.xml create mode 100644 app/src/main/res/layout/view_flex_retry.xml diff --git a/app/build.gradle b/app/build.gradle index 79bd84e..a889b43 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 28 - buildToolsVersion '28.0.3' + compileSdkVersion 30 + buildToolsVersion '30.0.3' defaultConfig { applicationId "com.github.nukc.loadmorewrapper.sample" - minSdkVersion 14 - targetSdkVersion 28 + minSdkVersion 16 + targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -26,12 +26,13 @@ ext { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "com.android.support:appcompat-v7:${supportLibVersion}" - implementation "com.android.support:design:${supportLibVersion}" - implementation "com.android.support:recyclerview-v7:${supportLibVersion}" - implementation "com.android.support:support-v4:${supportLibVersion}" + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' + implementation 'com.google.android.flexbox:flexbox:3.0.0' implementation project(':loadmorewrapper') - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a0802ac..ab2d422 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> + diff --git a/app/src/main/java/com/github/nukc/sample/AnotherActivity.java b/app/src/main/java/com/github/nukc/sample/AnotherActivity.java index daff0a9..01f2f91 100644 --- a/app/src/main/java/com/github/nukc/sample/AnotherActivity.java +++ b/app/src/main/java/com/github/nukc/sample/AnotherActivity.java @@ -1,14 +1,15 @@ package com.github.nukc.sample; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.github.nukc.LoadMoreWrapper.LoadMoreAdapter; import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; diff --git a/app/src/main/java/com/github/nukc/sample/FlexboxActivity.java b/app/src/main/java/com/github/nukc/sample/FlexboxActivity.java new file mode 100644 index 0000000..e0092f1 --- /dev/null +++ b/app/src/main/java/com/github/nukc/sample/FlexboxActivity.java @@ -0,0 +1,99 @@ +package com.github.nukc.sample; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.RecyclerView; + +import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; +import com.google.android.flexbox.FlexDirection; +import com.google.android.flexbox.FlexboxLayoutManager; +import com.google.android.flexbox.JustifyContent; + +public class FlexboxActivity extends AppCompatActivity { + + private boolean mShowLoadFailedEnabled = true; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_flexbox); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this); + layoutManager.setFlexDirection(FlexDirection.ROW); + layoutManager.setJustifyContent(JustifyContent.FLEX_END); + recyclerView.setLayoutManager(layoutManager); + + FlexAdapter flexAdapter = new FlexAdapter(20); +// recyclerView.setAdapter(flexAdapter); + LoadMoreWrapper.with(flexAdapter) +// .setFooterView(R.layout.view_flex_loading) +// .setNoMoreView(R.layout.view_flex_no_more) +// .setLoadFailedView(R.layout.view_flex_retry) + .setShowNoMoreEnabled(true) + .setListener(enabled -> { + int itemCount = flexAdapter.getItemCount(); + if (itemCount > 20 && mShowLoadFailedEnabled) { + mShowLoadFailedEnabled = false; + recyclerView.postDelayed(() -> enabled.setLoadFailed(true), 800); + } else { + //not enable load more + if (itemCount >= 80) { + enabled.setLoadMoreEnabled(false); + } + + recyclerView.postDelayed(flexAdapter::addItem, 1200); + } + }) + .into(recyclerView); + } + + private static class FlexAdapter extends RecyclerView.Adapter { + + private int mCount; + + public FlexAdapter(int count) { + mCount = count; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_flex, parent, false); + return new SampleHolder(view); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + ((SampleHolder) holder).mTextView.setText(position + ""); + ViewGroup.LayoutParams lp = ((SampleHolder) holder).itemView.getLayoutParams(); + if (lp instanceof com.google.android.flexbox.FlexboxLayoutManager.LayoutParams) { + ((FlexboxLayoutManager.LayoutParams) lp).setFlexGrow(1); + } + } + + @Override + public int getItemCount() { + return mCount; + } + + public void addItem() { + final int positionStart = mCount; + mCount+= 5; + notifyItemRangeInserted(positionStart, 5); + } + + static class SampleHolder extends RecyclerView.ViewHolder { + TextView mTextView; + + public SampleHolder(View itemView) { + super(itemView); + mTextView = (TextView) itemView.findViewById(R.id.text); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/nukc/sample/Issue9Activity.java b/app/src/main/java/com/github/nukc/sample/Issue9Activity.java index b72e0f2..3bb9f20 100644 --- a/app/src/main/java/com/github/nukc/sample/Issue9Activity.java +++ b/app/src/main/java/com/github/nukc/sample/Issue9Activity.java @@ -1,15 +1,16 @@ package com.github.nukc.sample; import android.os.Bundle; -import android.support.v4.widget.SwipeRefreshLayout; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + import com.github.nukc.LoadMoreWrapper.LoadMoreAdapter; import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; diff --git a/app/src/main/java/com/github/nukc/sample/MainActivity.java b/app/src/main/java/com/github/nukc/sample/MainActivity.java index 880c7b3..31904de 100644 --- a/app/src/main/java/com/github/nukc/sample/MainActivity.java +++ b/app/src/main/java/com/github/nukc/sample/MainActivity.java @@ -2,9 +2,10 @@ import android.content.Intent; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; import android.view.View; +import androidx.appcompat.app.AppCompatActivity; + public class MainActivity extends AppCompatActivity { @Override @@ -15,6 +16,7 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.btn_another).setOnClickListener(mOnClickListener); findViewById(R.id.btn_issues).setOnClickListener(mOnClickListener); findViewById(R.id.btn_not_show_footer_when_not_covered_screen).setOnClickListener(mOnClickListener); + findViewById(R.id.btn_flex_box).setOnClickListener(mOnClickListener); } private View.OnClickListener mOnClickListener = new View.OnClickListener() { @@ -34,6 +36,9 @@ public void onClick(View v) { case R.id.btn_not_show_footer_when_not_covered_screen: cls = NotShowFooterWhenNotCoveredScreenActivity.class; break; + case R.id.btn_flex_box: + cls = FlexboxActivity.class; + break; } startActivity(new Intent(MainActivity.this, cls)); } diff --git a/app/src/main/java/com/github/nukc/sample/NotShowFooterWhenNotCoveredScreenActivity.java b/app/src/main/java/com/github/nukc/sample/NotShowFooterWhenNotCoveredScreenActivity.java index 5d55807..d603402 100644 --- a/app/src/main/java/com/github/nukc/sample/NotShowFooterWhenNotCoveredScreenActivity.java +++ b/app/src/main/java/com/github/nukc/sample/NotShowFooterWhenNotCoveredScreenActivity.java @@ -1,14 +1,15 @@ package com.github.nukc.sample; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.github.nukc.LoadMoreWrapper.LoadMoreAdapter; import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; diff --git a/app/src/main/java/com/github/nukc/sample/SampleActivity.java b/app/src/main/java/com/github/nukc/sample/SampleActivity.java index 2721666..9e4c8dd 100644 --- a/app/src/main/java/com/github/nukc/sample/SampleActivity.java +++ b/app/src/main/java/com/github/nukc/sample/SampleActivity.java @@ -1,13 +1,15 @@ package com.github.nukc.sample; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager.widget.ViewPager; + +import com.google.android.material.tabs.TabLayout; + public class SampleActivity extends AppCompatActivity { @Override diff --git a/app/src/main/java/com/github/nukc/sample/SampleFragment.java b/app/src/main/java/com/github/nukc/sample/SampleFragment.java index 0d17338..cc5f791 100644 --- a/app/src/main/java/com/github/nukc/sample/SampleFragment.java +++ b/app/src/main/java/com/github/nukc/sample/SampleFragment.java @@ -2,18 +2,19 @@ import android.os.Bundle; -import android.support.annotation.IntDef; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.annotation.IntDef; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.StaggeredGridLayoutManager; + import com.github.nukc.LoadMoreWrapper.LoadMoreAdapter; import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; diff --git a/app/src/main/java/com/github/nukc/sample/ScrollingActivity.java b/app/src/main/java/com/github/nukc/sample/ScrollingActivity.java index 842352b..b602d3a 100644 --- a/app/src/main/java/com/github/nukc/sample/ScrollingActivity.java +++ b/app/src/main/java/com/github/nukc/sample/ScrollingActivity.java @@ -1,16 +1,17 @@ package com.github.nukc.sample; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.Toolbar; import android.view.View; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.github.nukc.LoadMoreWrapper.LoadMoreAdapter; import com.github.nukc.LoadMoreWrapper.LoadMoreWrapper; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.snackbar.Snackbar; public class ScrollingActivity extends AppCompatActivity { diff --git a/app/src/main/res/layout/activity_another.xml b/app/src/main/res/layout/activity_another.xml index 72336ce..20472d5 100644 --- a/app/src/main/res/layout/activity_another.xml +++ b/app/src/main/res/layout/activity_another.xml @@ -1,5 +1,5 @@ - diff --git a/app/src/main/res/layout/activity_flexbox.xml b/app/src/main/res/layout/activity_flexbox.xml new file mode 100644 index 0000000..c9a4987 --- /dev/null +++ b/app/src/main/res/layout/activity_flexbox.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_issue9.xml b/app/src/main/res/layout/activity_issue9.xml index a80be4f..9efcc06 100644 --- a/app/src/main/res/layout/activity_issue9.xml +++ b/app/src/main/res/layout/activity_issue9.xml @@ -1,11 +1,11 @@ - - - + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a1a8b2d..4774836 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -39,4 +39,12 @@ android:text="@string/not_show_footer_when_not_covered_screen" app:textAllCaps="false" /> +