我遵循了不同的教程和官方文档来尝试实现 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 的活动,我正在使用库黄油刀来声明视图,并且我已经确保填充了数组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>
我看不出有什么问题,没有错误信息什么都没有???