Android Java中的回收视图

时间:2018-06-22 11:35:27

标签: java android android-recyclerview

Recycler View没有回收到阵列的长度。 这是代码,我缺少什么。我遵循了描述整个过程的指南,该指南在列表视图中可见数组的所有项目。 这是承载recyclerView的主要活动文件,该文件位于适配器java类下面,该适配器java类过滤整个数组并弹出View视图。请帮助我

PickupActivity.java

package com.demo.driverconsole;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerViewAccessibilityDelegate;
import android.util.Log;
import android.widget.Adapter;
import android.widget.Toast;
import java.util.ArrayList;

public class pickupActivity extends AppCompatActivity {
    private static final String TAG = "pickupActivity";
    private RecyclerView studentList;
    private  RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<String> myDataset = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pickup);
        myDataset.add("John Doe");
        myDataset.add("Jane Doe");
        myDataset.add("Susan");
        initRecycler();

    }

    public void initRecycler() {
        Log.d(TAG, "Called");
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        StudentListAdapter adapter = new StudentListAdapter(myDataset);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}

StudentListAdapter.java

package com.datastoneglobal.driverconsole;

import android.support.annotation.NonNull;
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.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;

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

    private static final String TAG = "StudentListAdapter";
    private ArrayList<String> names = new ArrayList<>();

    public StudentListAdapter(ArrayList<String> names) {
        Log.d(TAG, "StudentListAdapter: " + names);
        this.names = names;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.text.setText(names.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;
        RelativeLayout layout;
        public ViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.textView);
        }
    }


}

ActivityPickup.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

StudentItem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/layout"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Loading ...">
    </TextView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

代码看起来正确,我发现的唯一问题是您在RelativeLayoutStudentItem.xml的最高位置

RelativeLayoutandroid:layout_height="wrap_content"的高度更改为 StudentItem.xml

示例代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/layout"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Loading ...">
    </TextView>
</RelativeLayout>

编辑

使用此

private  StudentListAdapter adapter;

代替这个

private  RecyclerView.Adapter adapter;

答案 1 :(得分:1)

⬇删除此代码⬇

public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ViewHolder>
{
    // ....
}

⬇使用此代码⬇

public class StudentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    {
        // ....
    }

因为您扩展了:- 公共类StudentListAdapter

extends RecyclerView.Adapter<StudentListAdapter.ViewHolder>

您将在这里扩展:-公共类ViewHolder extends RecyclerView.ViewHolder