RecyclerView无法显示卡片/文本。

huangapple 未分类评论47阅读模式
英文:

Recycler view is not displaying cards/text

问题

  1. Gradle文件:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
  1. MainActivity.java:
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 1", "Line 2"));
        // ... (add more items)

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}
  1. ExampleAdapter.java:
package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList;

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;

        public ExampleViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
        }
    }

    public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
        mExampleList = exampleList;
    }

    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);

        holder.mImageView.setImageResource(currentItem.getImageResource());
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
    }

    @Override
    public int getItemCount() {
        return mExampleList.size();
    }
}
  1. ExampleItem.java:
package com.example.myapplication;

public class ExampleItem {
    private int mImageResource;
    private String mText1;
    private String mText2;

    public ExampleItem(int imageResource, String text1, String text2) {
        mImageResource = imageResource;
        mText1 = text1;
        mText2 = text2;
    }

    public int getImageResource() {
        return mImageResource;
    }

    public String getText1() {
        return mText1;
    }

    public String getText2() {
        return mText2;
    }
}
  1. example_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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:layout_marginBottom="4dp"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/imageView"
            android:layout_toRightOf="@+id/imageView"
            android:text="Line 1"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_toEndOf="@+id/imageView"
            android:layout_toRightOf="@+id/imageView"
            android:text="Line 2"
            android:textSize="15sp" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>
  1. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:scrollbars="vertical" />

</RelativeLayout>
英文:

I have synced the gradle,checked dependencies,removed warning or any errors,rewritten multiple times and even tried different methods/tutorials i could find.Hope someone here could help me.

This is my code:

  1. Gradle file:
apply plugin: &#39;com.android.application&#39;

android {
    compileSdkVersion 29
    buildToolsVersion &quot;29.0.2&quot;
    defaultConfig {
        applicationId &quot;com.example.myapplication&quot;
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName &quot;1.0&quot;
        testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
        }
    }
}

dependencies {
    implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])
    implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
    implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
    testImplementation &#39;junit:junit:4.12&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
    androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;
    implementation &#39;androidx.cardview:cardview:1.0.0&#39;
    implementation &#39;androidx.recyclerview:recyclerview:1.1.0&#39;
}

1.MainActivity.java


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList&lt;ExampleItem&gt; exampleList = new ArrayList&lt;&gt;();
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 1&quot;, &quot;Line 2&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 3&quot;, &quot;Line 4&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 5&quot;, &quot;Line 6&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 7&quot;, &quot;Line 8&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 9&quot;, &quot;Line 10&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 11&quot;, &quot;Line 12&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 13&quot;, &quot;Line 14&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 15&quot;, &quot;Line 16&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 17&quot;, &quot;Line 18&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 19&quot;, &quot;Line 20&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 21&quot;, &quot;Line 22&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 23&quot;, &quot;Line 24&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 25&quot;, &quot;Line 26&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 27&quot;, &quot;Line 28&quot;));
        exampleList.add(new ExampleItem(R.drawable.ic_android, &quot;Line 29&quot;, &quot;Line 30&quot;));

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

2.ExampleAdapter.java

package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ExampleAdapter extends RecyclerView.Adapter&lt;ExampleAdapter.ExampleViewHolder&gt; {
    private ArrayList&lt;ExampleItem&gt; mExampleList;

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;

        public ExampleViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
            mTextView1 = itemView.findViewById(R.id.textView);
            mTextView2 = itemView.findViewById(R.id.textView2);
        }
    }

    public ExampleAdapter(ArrayList&lt;ExampleItem&gt; exampleList) {
        mExampleList = exampleList;
    }

    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);

        holder.mImageView.setImageResource(currentItem.getImageResource());
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
    }

    @Override
    public int getItemCount() {
        return mExampleList.size();
    }
}

3.ExampleItem

package com.example.myapplication;

public class ExampleItem {
    private int mImageResource;
    private String mText1;
    private String mText2;

    public ExampleItem(int imageResource, String text1, String text2) {
        mImageResource = imageResource;
        mText1 = text1;
        mText2 = text2;
    }

    public int getImageResource() {
        return mImageResource;
    }

    public String getText1() {
        return mText1;
    }

    public String getText2() {
        return mText2;
    }
}

4.example_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;4dp&quot;
app:cardCornerRadius=&quot;4dp&quot;&gt;

&lt;RelativeLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_margin=&quot;4dp&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/imageView&quot;
        android:layout_width=&quot;50dp&quot;
        android:layout_height=&quot;50dp&quot;
        android:padding=&quot;2dp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/textView&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:layout_toEndOf=&quot;@+id/imageView&quot;
        android:layout_toRightOf=&quot;@+id/imageView&quot;
        android:text=&quot;Line 1&quot;
        android:textColor=&quot;@android:color/black&quot;
        android:textSize=&quot;20sp&quot;
        android:textStyle=&quot;bold&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/textView2&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/textView&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        android:layout_toEndOf=&quot;@+id/imageView&quot;
        android:layout_toRightOf=&quot;@+id/imageView&quot;
        android:text=&quot;Line 2&quot;
        android:textSize=&quot;15sp&quot; /&gt;

&lt;/RelativeLayout&gt;

&lt;/androidx.cardview.widget.CardView&gt;

5.activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@android:color/darker_gray&quot;
        android:padding=&quot;4dp&quot;
        android:scrollbars=&quot;vertical&quot; /&gt;

&lt;/RelativeLayout&gt;

huangapple
  • 本文由 发表于 2020年4月9日 09:43:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61112673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定