recyclerview не отображает элементы
recylerview не отображает элементы,делал по примерам, попробовал через логи посмотреть в onCreateViewHolder и onBindViewHolder они вообще не вызываются.
Main_activity.java
package com.example.disneycharacter;
import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<ItemElement> elements = new ArrayList<ItemElement>();
private Toolbar mToolbar;
@Override
protected void onCreate (Bundle saveInstanceElement) {
super.onCreate(saveInstanceElement);
setContentView (R.layout.activity_main);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
// начальная инициализация списка
setInitialData();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
// создаем адаптер
RecylerAdapter adapter = new RecylerAdapter(this, elements) ;
// устанавливаем для списка адаптер
Log.d(TAG, "before set adapter" );
recyclerView.setAdapter(adapter);
//
}
private void setInitialData(){
Log.d(TAG, "setInitialData" );
elements.add(new ItemElement ("Ариэль", R.drawable.ariel));
elements.add(new ItemElement ("Гайка", R.drawable.gaika));
elements.add(new ItemElement ("Джин", R.drawable.jin));
elements.add(new ItemElement ("Микки Маус", R.drawable.mickey));
elements.add(new ItemElement ("Перри Утконос", R.drawable.perry));
elements.add(new ItemElement ("Пумба", R.drawable.pumba));
}
}
RecylerAdapter.java
package com.example.disneycharacter;
import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class RecylerAdapter extends RecyclerView.Adapter<RecylerAdapter.mViewHolder>{
private ArrayList<ItemElement> elements = new ArrayList<ItemElement>();
private final Context mContext;
public RecylerAdapter(Context context, ArrayList<ItemElement> elements ) {
this.elements = elements;
mContext = context;
Log.d(TAG, "CONSTRUCT" );
}
@NonNull
@Override
public RecylerAdapter.mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder" );
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new mViewHolder(view);
}
@Override
public void onBindViewHolder(mViewHolder holder, @SuppressLint("RecyclerView") final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(elements.get(position).getIcon())
.into(holder.image);
holder.text.setText(elements.get(position).getName());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on: " + elements.get(position).getName());
Toast.makeText(mContext, elements.get(position).getName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mContext, FactsActivity.class);
intent.putExtra("image_url", elements.get(position).getIcon());
intent.putExtra("image_name", elements.get(position).getName());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return 0;
}
public static class mViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView text;
ConstraintLayout parentLayout;
public mViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder" );
image = itemView.findViewById(R.id.icon);
text = itemView.findViewById(R.id.name);
// parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
itemElement.java
package com.example.disneycharacter;
public class ItemElement {
private String name;
private int icon;
public ItemElement(String name, int icon){
this.name=name;
this.icon=icon;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getIcon() {
return this.icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/parent_layout">
<ImageView
android:id="@+id/icon"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="Название"
android:textSize="36sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/icon"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/blue"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
build
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.disneycharacter'
compileSdk 33
defaultConfig {
applicationId "com.example.disneycharacter"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.github.bumptech.glide:glide:4.14.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.14.0'
implementation 'de.hdodenhof:circleimageview:2.2.0'
}