Android “回收器”视图中的空列表项

Android “回收器”视图中的空列表项,android,api,android-studio,android-recyclerview,retrofit,Android,Api,Android Studio,Android Recyclerview,Retrofit,当应用程序启动时,44个项目中的前6个列表项目(包含在一个半屏幕中)最初是空的。当我向下滚动然后向上滚动时,它们最终显示出来。我希望在应用程序启动后立即加载这些列表 我试图通过添加 mLayoutManager.setStackFromEnd(true); mLayoutManager.setReverseLayout(true); 在设置recyclerview的layoutmanager之前。但后来我发现这还有其他用途 下面是代码的一部分- MainActivity.java publi

当应用程序启动时,44个项目中的前6个列表项目(包含在一个半屏幕中)最初是空的。当我向下滚动然后向上滚动时,它们最终显示出来。我希望在应用程序启动后立即加载这些列表

我试图通过添加

mLayoutManager.setStackFromEnd(true);

mLayoutManager.setReverseLayout(true);
在设置recyclerview的layoutmanager之前。但后来我发现这还有其他用途

下面是代码的一部分-

MainActivity.java

public class MainActivity extends AppCompatActivity {

    RecyclerView rv;
    LinearLayoutManager mLayoutManager;

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

        rv = (RecyclerView) findViewById(R.id.DepList);
        rv.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);

        rv.setLayoutManager(mLayoutManager);
        DepAdapter dep = new DepAdapter();
        rv.setAdapter(dep);
    }
}
DepAdapter.java

public class DepAdapter extends RecyclerView.Adapter<DepAdapter.DepHolder> {

    String[] depName;
    String[] detail;
    int klm=0;

    public DepAdapter()
    {
        depName=new String[44];
        detail=new String[44];

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://data.police.uk/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        JsonHolder jsonHolder = retrofit.create(JsonHolder.class);

        Call<List<FetchData>> call = jsonHolder.getDATA();


        call.enqueue(new Callback<List<FetchData>>() {
            @Override
            public void onResponse(Call<List<FetchData>> call, Response<List<FetchData>> response) {

                if(!response.isSuccessful())
                {
                    System.out.println("Server Error");
                    return;
                }

                List<FetchData>  posts = response.body();

                for (FetchData fetchData : posts ){
                    depName[klm]=fetchData.getName();
                    detail[klm]=fetchData.getId();
                    klm++;
                }
            }

            @Override
            public void onFailure(Call<List<FetchData>> call, Throwable t) {
                System.out.println("FAILED TO FETCH");
            }
        });
    }

    @NonNull
    @Override
    public DepHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.list_theme,viewGroup,false);

        return new DepHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DepHolder depHolder, int i) {

        String title = depName[i];
        String brief = detail[i];

        depHolder.head.setText(title);
        depHolder.text.setText(brief);
    }

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

    public class DepHolder extends RecyclerView.ViewHolder{

        ImageView imgIcon;
        TextView text,head;

        public DepHolder(@NonNull View itemView) {
            super(itemView);

            imgIcon=(ImageView) itemView.findViewById(R.id.imgIcon);
            text=(TextView)itemView.findViewById(R.id.text);
            head=(TextView)itemView.findViewById(R.id.head);
        }
    }
}
public类DepAdapter扩展了RecyclerView.Adapter{
字符串[]depName;
字符串[]详细信息;
int-klm=0;
公共数据适配器()
{
depName=新字符串[44];
细节=新字符串[44];
改装改装=新改装.Builder()
.baseUrl(“https://data.police.uk/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonHolder-JsonHolder=reformation.create(JsonHolder.class);
Call Call=jsonHolder.getDATA();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
如果(!response.issusccessful())
{
System.out.println(“服务器错误”);
返回;
}
List posts=response.body();
for(FetchData FetchData:posts){
depName[klm]=fetchData.getName();
detail[klm]=fetchData.getId();
klm++;
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
System.out.println(“未能获取”);
}
});
}
@非空
@凌驾
public DepHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){
LayoutInflater充气器=LayoutInflater.from(viewGroup.getContext());
视图=充气器。充气(R.layout.list\u主题,视图组,false);
返回新的部门持有人(视图);
}
@凌驾
public void onBindViewHolder(@NonNull DepHolder DepHolder,int i){
字符串title=depName[i];
字符串简短=详细信息[i];
部门负责人.负责人.秘书长(职务);
depHolder.text.setText(简短);
}
@凌驾
public int getItemCount(){
返回depName.length;
}
公共类DepHolder扩展了RecyclerView.ViewHolder{
ImageView imgIcon;
文本查看文本,头部;
公共部门持有者(@NonNull View itemView){
超级(项目视图);
imgIcon=(ImageView)itemView.findViewById(R.id.imgIcon);
text=(TextView)itemView.findViewById(R.id.text);
head=(TextView)itemView.findViewById(R.id.head);
}
}
}
activity_main.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="match_parent"
    android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingStart="5dp"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    >

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/backbutton"
        android:onClick="backfromdep"
        android:id="@+id/backfromdep"
        android:layout_marginBottom="5dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textSize="20sp"
        android:layout_marginLeft="10dp"
        android:text="Search dep option here"
        android:textColor="#000"
        />
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/DepList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border"
    android:scrollbars="vertical"
    >

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

list_theme.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="horizontal"
    android:background="@drawable/border"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        android:layout_margin="10dp"
        android:id="@+id/imgIcon"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kent Police"
        android:textColor="#000"
        android:textSize="18sp"
        android:layout_marginTop="10dp"
        android:id="@+id/head"
        />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kent Police"
            android:textColor="#000"
            android:textSize="13sp"
            android:layout_marginTop="5dp"
            android:id="@+id/text"
            />


    </LinearLayout>

</LinearLayout>

list_theme.xml

您的代码存在一些架构问题(比如您不应该从适配器发出网络请求,尤其是不应该在构造函数中),但忽略这些问题来回答您的实际问题

如果由于适配器中要显示新数据而希望recyclerview刷新,则需要调用
notify
方法之一。最容易使整个列表无效

public void onResponse(Call<List<FetchData>> call, Response<List<FetchData>> response) {

    if (!response.isSuccessful()) {
        System.out.println("Server Error");
        return;
    }

    List<FetchData>  posts = response.body();

    for (FetchData fetchData : posts ) {
        depName[klm]=fetchData.getName();
        detail[klm]=fetchData.getId();
        klm++;
    }

    notifyDataSetChanged(); // Trigger an update to the recyclerview this is attached to
}
public void onResponse(调用、响应){
如果(!response.issusccessful()){
System.out.println(“服务器错误”);
返回;
}
List posts=response.body();
for(FetchData FetchData:posts){
depName[klm]=fetchData.getName();
detail[klm]=fetchData.getId();
klm++;
}
notifyDataSetChanged();//触发对附加到的recyclerview的更新
}

希望有帮助

您的代码存在一些架构问题(比如您不应该从适配器发出网络请求,尤其是不应该在构造函数中),但忽略这些问题来回答您的实际问题

如果由于适配器中要显示新数据而希望recyclerview刷新,则需要调用
notify
方法之一。最容易使整个列表无效

public void onResponse(Call<List<FetchData>> call, Response<List<FetchData>> response) {

    if (!response.isSuccessful()) {
        System.out.println("Server Error");
        return;
    }

    List<FetchData>  posts = response.body();

    for (FetchData fetchData : posts ) {
        depName[klm]=fetchData.getName();
        detail[klm]=fetchData.getId();
        klm++;
    }

    notifyDataSetChanged(); // Trigger an update to the recyclerview this is attached to
}
public void onResponse(调用、响应){
如果(!response.issusccessful()){
System.out.println(“服务器错误”);
返回;
}
List posts=response.body();
for(FetchData FetchData:posts){
depName[klm]=fetchData.getName();
detail[klm]=fetchData.getId();
klm++;
}
notifyDataSetChanged();//触发对附加到的recyclerview的更新
}

希望有帮助

在适配器构造函数中调用服务方法不是一个好主意。在您的活动中调用它,然后将结果传递给您的适配器这是对异步调用进行改造的调用吗?也许您有竞争条件。在适配器构造函数中调用服务方法不是一个好主意。在您的活动中调用它,然后将结果传递给您的适配器这是对异步调用进行改造的调用吗?也许你有一个竞争条件。我想如果回调需要一段时间,他可能仍然有一段时间的空白项。onCreateViewHolder仍然会在OnResponseEyes之前被调用,我确实有一段时间像一秒钟,其中所有44项都是空白的。无论如何也要解决这个问题?当然,但这超出了你的问题范围。简而言之,适配器的
getItemCount
现在总是返回44。在数据准备好之前,它应该返回0。我认为如果回调需要一段时间,他可能仍然有一段时间的空白项。onCreateViewHolder仍然会在OnResponseEyes之前被调用,我确实有一段时间像一秒钟,其中所有44项都是空白的。无论如何也要解决这个问题?当然,但这超出了你的问题范围。简而言之,适配器的
getItemCount
现在总是返回44。在数据准备就绪之前,它应该返回0。