Skip to content

Commit

Permalink
registerLifecycle(lifecycleOwner: LifecycleOwner) method added & Java…
Browse files Browse the repository at this point in the history
… sample converted into a Fragment example
  • Loading branch information
ImaginativeShohag committed May 31, 2021
1 parent 1bc7ec7 commit 2551a97
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 274 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Change Log
==========

## Version 2.0.0, 2.0.1 *(2021-05-22)* 🚀
## Version 2.0.2 *(2021-05-31)* 🚀

* 🆕 `registerLifecycle(lifecycleOwner: LifecycleOwner)` method added.
* 🛠️ Java sample converted into a `Fragment` example.

## Version 2.0.0, 2.0.1 *(2021-05-22)*

* 🆕 Previous boring custom layout system removed. And view-binding supported custom layout system added. See `CarouselListener` in the sample app for details. 🎉
* 🆕 The carousel is now supported Infinite ∞ looping (Infinite Carousel) 🥳! It's default now. You can disable it by setting `infiniteCarousel` to `false`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ dependencies {
// Material Components for Android. Replace the version with the latest version of Material Components library.
implementation 'com.google.android.material:material:1.3.0'
// Optional: Circle Indicator (To fix the xml preview "Missing classes" error)
// Circle Indicator (To fix the xml preview "Missing classes" error)
implementation 'me.relex:circleindicator:2.1.6'
implementation 'org.imaginativeworld.whynotimagecarousel:whynotimagecarousel:2.0.1'
implementation 'org.imaginativeworld.whynotimagecarousel:whynotimagecarousel:2.0.2'
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kotlin.code.style=official
# ----------------
GROUP=org.imaginativeworld.whynotimagecarousel
POM_ARTIFACT_ID=whynotimagecarousel
VERSION_NAME=2.0.1
VERSION_NAME=2.0.2
# ----------------
POM_NAME=Why Not! Image Carousel!
POM_DESCRIPTION=An easy, super simple and customizable image carousel view for Android.
Expand Down
3 changes: 3 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

// Kotlin
implementation "androidx.fragment:fragment-ktx:1.3.4"

// debugImplementation because LeakCanary should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
}
3 changes: 2 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".JavaActivity" />
<activity android:name=".KotlinActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".JavaActivity" />
<activity android:name=".FragmentExampleActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,187 +1,19 @@
package org.imaginativeworld.whynotimagecarousel.sample;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.google.android.material.button.MaterialButton;

import org.imaginativeworld.whynotimagecarousel.listener.CarouselListener;
import org.imaginativeworld.whynotimagecarousel.listener.CarouselOnScrollListener;
import org.imaginativeworld.whynotimagecarousel.model.CarouselGravity;
import org.imaginativeworld.whynotimagecarousel.model.CarouselItem;
import org.imaginativeworld.whynotimagecarousel.model.CarouselType;
import org.imaginativeworld.whynotimagecarousel.sample.databinding.ActivityJavaBinding;
import org.imaginativeworld.whynotimagecarousel.utils.Utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.relex.circleindicator.CircleIndicator2;

public class JavaActivity extends AppCompatActivity {

private ActivityJavaBinding binding;

private Context context;

private boolean isStarted = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityJavaBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

context = this;

binding.carousel.registerLifecycle(getLifecycle());

binding.carousel.setShowTopShadow(false);
binding.carousel.setTopShadowAlpha(0.6f); // 0 to 1, 1 means 100%
binding.carousel.setTopShadowHeight(Utils.dpToPx(32, context)); // px value of dp

binding.carousel.setShowBottomShadow(true);
binding.carousel.setBottomShadowAlpha(0.7f); // 0 to 1, 1 means 100%
binding.carousel.setBottomShadowHeight(Utils.dpToPx(48, context)); // px value of dp

binding.carousel.setShowCaption(true);
binding.carousel.setCaptionMargin(Utils.dpToPx(8, context)); // px value of dp
binding.carousel.setCaptionTextSize(Utils.spToPx(16, context)); // px value of sp

binding.carousel.setShowIndicator(false);
binding.carousel.setIndicatorMargin(Utils.dpToPx(0, context)); // px value of dp

binding.carousel.setImageScaleType(ImageView.ScaleType.CENTER_CROP);

binding.carousel.setCarouselBackground(new ColorDrawable(Color.parseColor("#333333")));
binding.carousel.setImagePlaceholder(ContextCompat.getDrawable(
this,
R.drawable.ic_wb_cloudy_with_padding
));

binding.carousel.setCarouselPadding(Utils.dpToPx(0, context));
binding.carousel.setCarouselPaddingStart(Utils.dpToPx(0, context));
binding.carousel.setCarouselPaddingTop(Utils.dpToPx(0, context));
binding.carousel.setCarouselPaddingEnd(Utils.dpToPx(0, context));
binding.carousel.setCarouselPaddingBottom(Utils.dpToPx(0, context));

binding.carousel.setShowNavigationButtons(false);
binding.carousel.setPreviousButtonLayout(R.layout.custom_previous_button_layout);
binding.carousel.setPreviousButtonId(R.id.custom_btn_previous);
binding.carousel.setPreviousButtonMargin(Utils.dpToPx(8, context)); // px value of dp
binding.carousel.setNextButtonLayout(R.layout.custom_next_button_layout);
binding.carousel.setNextButtonId(R.id.custom_btn_next);
binding.carousel.setNextButtonMargin(Utils.dpToPx(8, context)); // px value of dp

binding.carousel.setCarouselType(CarouselType.SHOWCASE);

binding.carousel.setCarouselGravity(CarouselGravity.CENTER);

binding.carousel.setScaleOnScroll(false);
binding.carousel.setScalingFactor(.15f);
binding.carousel.setAutoWidthFixing(true);
binding.carousel.setAutoPlay(false);
binding.carousel.setAutoPlayDelay(3000); // Milliseconds
binding.carousel.setInfiniteCarousel(true);
binding.carousel.setTouchToPause(true);

binding.carousel.setOnScrollListener(new CarouselOnScrollListener() {
@Override
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy, int position, @Nullable CarouselItem carouselItem) {
// ...
}

@Override
public void onScrollStateChanged(@NotNull RecyclerView recyclerView, int newState, int position, @Nullable CarouselItem carouselItem) {
// ...
}
});

binding.carousel.setCarouselListener(new CarouselListener() {
@Nullable
@Override
public ViewBinding onCreateViewHolder(@NotNull LayoutInflater layoutInflater, @NotNull ViewGroup parent) {
// ...
return null;
}

@Override
public void onBindViewHolder(@NotNull ViewBinding binding, @NotNull CarouselItem item, int position) {
// ...
}

@Override
public void onLongClick(int position, @NotNull CarouselItem carouselItem) {
// ...
}

@Override
public void onClick(int position, @NotNull CarouselItem carouselItem) {
// ...
}
});

CircleIndicator2 indicator = findViewById(R.id.custom_indicator);
binding.carousel.setIndicator(indicator);

MaterialButton previousBtn = findViewById(R.id.btn_goto_previous);
previousBtn.setOnClickListener(v -> binding.carousel.previous());

MaterialButton nextBtn = findViewById(R.id.btn_goto_next);
nextBtn.setOnClickListener(v -> binding.carousel.next());

List<CarouselItem> list = new ArrayList<>();

// Dummy header
Map<String, String> headers = new HashMap<>();
headers.put("header_key", "header_value");

int index = 1;
for (String item : DataSet.INSTANCE.getOne()) {
list.add(
new CarouselItem(
item,
"Image " + index++ + " of " + DataSet.INSTANCE.getOne().size(),
headers
)
);
}

binding.carousel.setData(list);

// ----------------------------------------------------------------

binding.fabPlay.setOnClickListener(v -> {
if (isStarted) {

isStarted = false;
binding.carousel.stop();

binding.fabPlay.setText("Start");

} else {

isStarted = true;
binding.carousel.start();

binding.fabPlay.setText("Stop");

}
});

}
}
Loading

0 comments on commit 2551a97

Please sign in to comment.