Here is the problem: I create the simplest RecyclerView in the world, but it displays only the first item. I cannot understand why. Thanks for any help.
item_layout.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_detail"/>
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bcit.moonlady.testrecycler.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv_hello"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/rv_details"
android:layout_below="@+id/tv_hello"/>
</RelativeLayout>
MainActivity.java
package com.bcit.moonlady.testrecycler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
public class MainActivity extends AppCompatActivity {
String[] data = {"test1", "test2", "test3"};
RecyclerView mRecView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecView = (RecyclerView)findViewById(R.id.rv_details);
mRecView.setHasFixedSize(true);
mRecView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecView.setAdapter(new DetailAdapter());
}
private class DetailView extends RecyclerView.ViewHolder {
TextView mTextView;
public DetailView(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.tv_detail);
}
public void bindView(String string) {
mTextView.setText(string);
}
}
private class DetailAdapter extends RecyclerView.Adapter<DetailView> {
@Override
public DetailView onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.item_layout, parent, false);
return new DetailView(v);
}
@Override
public void onBindViewHolder(DetailView holder, int position) {
String string = data[position];
holder.bindView(string);
}
@Override
public int getItemCount() {
return data.length;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…