android - Android 中的 RecyclerView 在没有任何错误消息的情况下无法正常工作

标签 android android-layout android-recyclerview recyclerview-layout

我已经按照不同的教程和官方文档尝试实现 RecyclerView,但它无法正常工作,而且我没有收到没有错误消息我希望有人能看到我遗漏了什么:

我的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

使用 RecyclerView 的 Activity,我正在使用库 butterknife 来声明 View ,并且我已经确保数组 Film[] filmography 已填充:

public class ActorFilmographyActivity extends AppCompatActivity {

    public static final String TAG = ActorFilmographyActivity.class.getSimpleName();

    @Bind(R.id.actorNameLabel) TextView actorLabel;

    @Bind(R.id.actorProfileImage) ImageView profileImage;

    @Bind(R.id.recyclerView) RecyclerView filmsRecyclerView;

    private Film[] filmography;
    private FilmographyAdapter adapter;

    private Actor actor;

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

        ButterKnife.bind(this);

        Intent intent = getIntent();
        if (intent != null) {
            actor = (Actor) intent.getSerializableExtra("actor");
            actorLabel.setText(actor.getName().toString());
            loadActorProfilePicture();
            getFilmographyBasedOn(actor.getId());
        }

        adapter = new FilmographyAdapter(this, filmography);
        filmsRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this);

        filmsRecyclerView.setLayoutManager(layoutManager);

        filmsRecyclerView.setHasFixedSize(true);//TODO chequear si sirve con esto

    }

这是我的 FilmographyAdapter 类:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cooervo.filmography.R;
import com.cooervo.filmography.models.Film;


/**
 * Create the basic adapter extending from RecyclerView.Adapter
 * note that we specify the custom ViewHolder which gives us access to our
 */
public class FilmographyAdapter extends  RecyclerView.Adapter<FilmographyAdapter.ViewHolder>{

    private Film[] films;
    private Context context;

    public FilmographyAdapter(Context ctx, Film[] filmsArray){
        context = ctx;
        films = filmsArray;
    }

    /**
     * Provides a direct reference to each of the views within a data item
     * Used to cache the views within the item layout for fast access
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView filmTitle;

        public ViewHolder(View itemView){
            super(itemView);
            filmTitle = (TextView) itemView.findViewById(R.id.filmTitleLabel);
        }
    }

    //This method inflates a layout from XML and returning the holder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        // Inflate the custom layout
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.filmography_list_item,
                        parent,
                        false);

        return new ViewHolder(view);
    }

    // Populates data into the item through holder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Film film = films[position];

        holder.filmTitle.setText(film.getTitle());
    }

    @Override
    public int getItemCount() {
        return 0;
    }

}

这是用作 RecyclerView 自定义布局的 filmography_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingBottom="8dp"
              android:paddingTop="8dp"
              tools:background="#10000000"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="100">


        <ImageView
            android:id="@+id/filmPosterImage"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:src="@mipmap/ic_launcher"
            android:layout_weight="30"/>

        <TextView
            android:id="@+id/filmTitleLabel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:layout_weight="30"
            android:textColor="#ff000000"
            android:textStyle="bold"
            android:gravity="center_vertical"
            android:textSize="25sp"/>

    </LinearLayout>

</LinearLayout>

我看不出有什么问题,没有任何错误消息???

最佳答案

你忘了放这个。

@Override
public int getItemCount() {
    return films.length;
}

关于android - Android 中的 RecyclerView 在没有任何错误消息的情况下无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774272/

相关文章:

Android AsyncHttpClient,无法找到符号类 header

java - Jsoup URL.get()/post() 内存不足错误

android - 内容提供者的应用程序如何指定客户端应用程序访问提供者数据所需的权限?

java - 在 Android 中以编程方式了解设备屏幕尺寸?

android - 单击“删除图像”图标从 recyclerView 中删除所选项目

Android fragment onResume 与 onCreateView

android-layout - android中的不同纵向布局

android - ConstraintLayout 中带有drawableEnd 的 TextView 换行问题

android - 从 recyclerview 中的 firebase 实时数据库访问数据

android - 如何为我的自定义对象创建滚轮选择器?