为什么RecyclerView不断重复相同的项目?

时间:2018-10-29 07:20:20

标签: android android-recyclerview

我正在开发cv应用程序,但是当我测试代码Recyclerview项时,请继续重复相同的信息,以便在网络后在Recyclerview中显示数据,但是项内有空白,这就是为什么我在其中添加了虚拟数据填充空间

下面的屏幕截图是我想要的 screenshot I want to achieve

当前ui屏幕截图 current ui current ui

在我实现了教育信息的EducationAdapter下方

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

    public List<Education> educationList;

    public Context context;

    public int[] subjectImage;
    public String[] subjectText;
    private EducationItem educationItem;

    public EducationAdapter(List<Education> educationList, EducationItem educationItem, Context context) {
        this.educationList = educationList;
        this.context = context;

        this.educationItem = educationItem;

    }


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


        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.education_item, parent, false);


        return new EducationAdapter.ViewHolder(itemView);
    }




    @Override
    public void onBindViewHolder(@NonNull EducationAdapter.ViewHolder holder, int position) {

        Education education = educationList.get(position);
        holder.duration.setText(education.getDuration());
        holder.degree.setText(education.getDegree());
        holder.institution.setText(education.getInstitution());

    }


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

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView duration, institution, degree, educationInfo, subjects, computers_science;
        ;
        private ImageView educationImage, subjectImage, computerScience;

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

            duration = (TextView) view.findViewById(R.id.duration);
            institution = (TextView) view.findViewById(R.id.institution);
            degree = (TextView) view.findViewById(R.id.degree);
            educationImage = (ImageView) view.findViewById(R.id.educationImage);
            educationInfo = (TextView) view.findViewById(R.id.education_info);
            subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
            subjects = (TextView) view.findViewById(R.id.subjects);


        }


    }
}

在我实现了网络通话和虚拟数据的EducationItem下面

public class EducationItem extends AppCompatActivity {

    private EducationAdapter educationAdapter;
    public List<Education> educationList;
    Context context;




    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    setContentView(R.layout.education);




    KitabInterface kitabInterface = ApiClient.getApiService();
    Call<KitabSawti> call = kitabInterface.getEducation();

        call.enqueue(new Callback<KitabSawti>() {
        @Override
        public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
            educationList=  response.body().getEducation();
            for(Education e: educationList){
                Log.e("items", e.toString());
            }


            educationList.add(new Education("Computer Science", R.drawable.computer_science));
            educationList.add(new Education("Data Structure", R.drawable.data_structure));
            RecyclerView recyclerView  =  findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            educationAdapter = new EducationAdapter(educationList, EducationItem.this, context ); // changes
            recyclerView.setAdapter(educationAdapter);
        }

        @Override
        public void onFailure(Call<KitabSawti> call, Throwable t) {

        }
    });
}
}

在我声明了项目的education_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:background="@color/colorBlust"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/educationImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:src="@drawable/education_information"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/education_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:text="@string/education_information"
            android:textColor="@color/colorWhite"
            android:textSize="20sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/duration"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:text="@string/text_duration"
        android:textColor="@color/colorWhite"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/institution"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:text="@string/text_institution"
        android:textColor="@color/colorWhite"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/degree"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:text="@string/text_degree"
        android:textColor="@color/colorWhite"
        android:textSize="16sp" />


    <Space
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/subjectImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/university_subjects"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/subjects"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/university_subjects"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <include
                layout="@layout/subject_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/subjects"
                android:layout_marginTop="60dp" />
        </RelativeLayout>


    </LinearLayout>

</LinearLayout>

在我声明了recyclerview的education.xml下面

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




    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/recyclerView"
        android:layout_height="wrap_content"/>



</RelativeLayout>

在我的json数据下面 “教育”: [         {             “持续时间”:“ 2012-2014”,             “机构”:“里加技术大学,拉脱维亚,里加”,             “学位”:“计算机科学学士学位”         }     ],     来自教育班

0 个答案:

没有答案