Closed isarYMB closed 3 years ago
I'm trying this code. but after run app. The app cant open in my android. whats wrong in my code?
CarouselPreviewActivity.java
package com.mig35.carousellayoutmanager.sample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<Integer> images;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View itemView) {
super(itemView);
thumbnail = itemView.findViewById(R.id.thumbnail);
}
}
public GalleryAdapter(Context context, List<Integer> images) {
mContext = context;
this.images = images;
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
Integer image = images.get(position);
Glide.with(mContext).load(images.get(position))
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.thumbnail);
}
@Override
public int getItemCount() {
return images.size();
}
public long getItemId(final int position) {
return position;
}
}
GalleryAdapter.java
package com.mig35.carousellayoutmanager.sample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<Integer> images;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View itemView) {
super(itemView);
thumbnail = itemView.findViewById(R.id.thumbnail);
}
}
public GalleryAdapter(Context context, List<Integer> images) {
mContext = context;
this.images = images;
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
Integer image = images.get(position);
Glide.with(mContext).load(images.get(position))
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.thumbnail);
}
@Override
public int getItemCount() {
return images.size();
}
public long getItemId(final int position) {
return position;
}
}
activity_carousel_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".CarouselPreviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="952dp"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_horizontal"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.mig35.carousellayoutmanager.sample"
minSdkVersion 14
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0'
implementation 'com.mig35:carousellayoutmanager:+'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
// implementation project(':carousellayoutmanager')
}
build.gradle
buildscript {
repositories {
google()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'io.github.gradle-nexus.publish-plugin'
@isarYMB does image showing if you change CarouselLayoutManager
with any other LayoutManager?
btw perhaps you need to change:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
to
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent" />
btw also I don't think that this question is related to the library, but more about android. you can ask such question is SO. If you find that other LM works fine, but this one not - welcome for the issue.
@isarYMB apakah gambar ditampilkan jika Anda mengubah
CarouselLayoutManager
dengan LayoutManager lain?
No, i dont use another Layout Manager
btw juga saya tidak berpikir bahwa pertanyaan ini terkait dengan perpustakaan, tetapi lebih banyak tentang Android. Anda dapat mengajukan pertanyaan seperti itu adalah SO. Jika Anda menemukan bahwa LM lain berfungsi dengan baik, tetapi yang ini tidak - selamat datang untuk masalah ini.
my question is related to the library. my app is always force close and when i open, the app just exits by itself
you should check logs and find the reason there
How to insert image to carousel or is there an example of the source code?