列表视图中的白色屏幕?

时间:2018-03-01 19:00:11

标签: android android-recyclerview adapter

有人能告诉我这个适配器有什么问题吗?我得到一个空的RecyclerView,但列表中有11个项目?我们检查了客户类和MainActivity,看起来是正确的。我们在getitems中记录customers.size并发现应该没有问题。可能是viewHolder类有问题吗?

package com.example.jenso.test1yay;

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

import java.util.List;

import static android.content.ContentValues.TAG;



public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  private List<Customer> customers;
  private Context context;

public MyAdapter(List<Customer> listItems ,Context context){
    this.customers = listItems;
    this.context = context;





}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item,parent, false);


    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
        Customer customer = customers.get(position);

        if (holder != null) {
            holder.textViewHead.setText(customer.getName());
            holder.textViewDesc.setText(customer.getLastName());
        }

}

@Override
public int getItemCount() {

    Log.d(TAG, "getItemCount: "+ customers.size());





    return customers.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView textViewHead;
    public TextView textViewDesc;

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


}


package com.example.jenso.test1yay;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<Customer> customers;

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

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    customers = new ArrayList<>();

    for(int i = 1; i <= 10; i++){
        Customer customer = new Customer("Jens" + (i+1),
                "Svensson"
                );
        customers.add(customer);
    }

    adapter = new MyAdapter(customers, this);
    recyclerView.setAdapter(adapter);
}



}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <TextView
            android:id="@+id/textViewDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="2dp"
            android:text="Description"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textViewHead" />

        <TextView
            android:id="@+id/textViewHead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="2dp"
            android:text="Heading"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.jenso.test1yay.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:listitem="@layout/list_item" />
</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

尝试将TextView android:layout_width="0dp"更改为100dp

答案 1 :(得分:0)

晚会,但尝试将活动的Context传递到适配器,然后在LayoutInflater.from中使用该上下文而不是parent.getContext()

答案 2 :(得分:0)

我在你的代码中做了一些修改尝试。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Customer> customers=new ArrayList<>();
private Context context;

public MyAdapter(List<Customer> listItems, Context context) {
    this.customers = listItems;
    this.context = context;


}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);


    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Customer customer = customers.get(position);

    if (holder != null) {
        holder.textViewHead.setText(customer.getName());
        holder.textViewDesc.setText(customer.getLastName());
    }

}

@Override
public int getItemCount() {

    Log.d(TAG, "getItemCount: " + customers.size());


    return customers.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView textViewHead;
    public TextView textViewDesc;

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

}

在主要活动中..

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private List<Customer> customers;

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

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        customers = new ArrayList<>();

        for(int i = 1; i <= 10; i++){
            Customer customer = new Customer("Jens" + (i+1),
                    "Svensson"
            );
            customers.add(customer);
        }

        adapter = new MyAdapter(customers, this);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }



}