RecyclerView不显示对象列表

时间:2018-10-03 12:32:21

标签: android android-recyclerview

我在这里找到的其他类似帖子上尝试了解决方案,但是它们没有帮助我。我希望有人能提供帮助。

我从Unsplash的API获取数据,一旦得到响应,ArrayList包含10个项目,这就是为什么我认为它必须与视图膨胀的方式有关。

这是代码

public class MainActivity extends AppCompatActivity {

private static final String TAG = "unsplash";
private final String CLIENT_ID = "...myclientID...";
// testing keyword used on the API to search for pictures 
private final String KEYWORD = "stackOverflow";
private final String urlBase = "https://api.unsplash.com/";
private Retrofit retrofit;

private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;

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

    retrofit = new Retrofit.Builder()
            .baseUrl(urlBase)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);

    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);

    recyclerViewAdapter = new RecyclerViewAdapter();
    recyclerView.setAdapter(recyclerViewAdapter);

    getData();
}

private void getData() {
    PictureService service = retrofit.create(PictureService.class);
    Call<PictureResponse> pictureResponseCall = service.getPictureList(KEYWORD, CLIENT_ID);

    pictureResponseCall.enqueue(new Callback<PictureResponse>() {
        @Override
        public void onResponse(Call<PictureResponse> call, Response<PictureResponse> response) {
            if (response.isSuccessful()){

                PictureResponse pictureResponse = response.body();
                ArrayList<UnsplashPic> pictureList = pictureResponse.getResults();

                recyclerViewAdapter.addPictures(pictureList);

            }else{
                Log.e (TAG, " onResponse " + response.errorBody());
            }
        }

        @Override
        public void onFailure(Call<PictureResponse> call, Throwable t) {
            Log.e (TAG, " onFailure " + t.getMessage());
        }
    });
}

}

我的适配器类

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private ArrayList<UnsplashPic> pictureList;

public RecyclerViewAdapter (){
    pictureList = new ArrayList<>();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.item_view_holder, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

    UnsplashPic pic = pictureList.get(i);
    viewHolder.artistName.setText(pic.user.getUsername());
    viewHolder.unsplash.setText("Unsplash");
}

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

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    pictureList.addAll(pictureList);
    notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView imageView;
    private TextView artistName;
    private TextView unsplash;

    public ViewHolder (View itemView){
        super(itemView);

        imageView = itemView.findViewById(R.id.imageview);
        artistName = itemView.findViewById(R.id.artist_name);
        unsplash = itemView.findViewById(R.id.unsplash_name);
    }

}

}

item_view_holder.xml

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

<ImageView
    android:id="@+id/imageview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:background="#ababab"
    android:contentDescription="unsplash picture" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Photo by "/>

    <TextView
        android:id="@+id/artist_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="on "/>

    <TextView
        android:id="@+id/unsplash_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

</LinearLayout>

activity_main.xml

<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Search for pictures"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search"/>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerview">

</android.support.v7.widget.RecyclerView>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这里:

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    pictureList.addAll(pictureList);
    notifyDataSetChanged();
}

您实际上是将作为函数的参数发送的pictureList的内容添加到同一列表中,适配器的列表未更新。

您应该这样做:

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    this.pictureList.addAll(pictureList);
    notifyDataSetChanged();
}