Dear Android Developer and I am developing cv and I want to achieve ui below
But current UI looks like this I have implemented multiple view type in RecyclerView
followed this tutorial
But I cannot figure out why UI showing like that below
below my gist https://gist.github.com/kyodgorbek/3bcd877832f127fbf8b750b49c2b2a47
below my EducationAdapter where I have implemented multipleViewTypes public class EducationAdapter extends RecyclerView.Adapter {
public Context context;
public List<Education> educationList;
private EducationItem educationItem;
public static class EducationViewHolder extends RecyclerView.ViewHolder {
public TextView duration, institution, degree;
public EducationViewHolder(View view) {
super(view);
duration = (TextView) itemView.findViewById(R.id.duration);
institution = (TextView) itemView.findViewById(R.id.institution);
degree = (TextView) itemView.findViewById(R.id.degree);
}
}
public static class SubjectViewHolder extends RecyclerView.ViewHolder {
public ImageView subjectImage;
public TextView subjects;
public SubjectViewHolder(@NonNull View itemView) {
super(itemView);
subjectImage = (ImageView) itemView.findViewById(R.id.subjectImage);
subjects = (TextView) itemView.findViewById(R.id.subjects);
}
}
public static class UniversityViewHolder extends RecyclerView.ViewHolder {
public ImageView icon;
public TextView item;
public UniversityViewHolder(@NonNull View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.icon);
item = (TextView) itemView.findViewById(R.id.item);
}
}
public EducationAdapter(List<Education> educationList, EducationItem educationItem, Context context) {
this.educationList = educationList;
this.context = context;
this.educationItem = educationItem;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case INTERNET_TYPE:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.education_item, parent, false);
return new EducationViewHolder(itemView);
case SUBJECT_TYPE:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.subject, parent, false);
return new SubjectViewHolder(itemView);
case UNIVERSITY_TYPE:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.subject_list, parent, false);
return new UniversityViewHolder(itemView);
}
return null;
}
@Override
public int getItemViewType(int position) {
return educationList.get(position).type;
}
@Override
public int getItemCount() {
return educationList.size();
}
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
Education education = educationList.get(position);
if (education != null) {
switch (education.type()) {
case Education.INTERNET_TYPE:
((EducationViewHolder) holder).duration.setText(education.getInstitution());
((EducationViewHolder) holder).degree.setText(education.getDegree());
((EducationViewHolder) holder).institution.setText(education.getInstitution());
break;
case Education.SUBJECT_TYPE:
((SubjectViewHolder) holder).subjects.getContext().getString(R.string.university_subject);
((SubjectViewHolder) holder).subjectImage.getContext().getDrawable(R.drawable.university_subjects);
break;
case Education.UNIVERSITY_TYPE:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(( UniversityViewHolder) holder).icon.setImageResource(education.image);
}
((UniversityViewHolder) holder).item.setText(education.words);
break;
}
}
}
}
below my EducationItem where I have made network call
public class EducationItem extends AppCompatActivity {
private EducationAdapter educationAdapter;
public List<Education> educationList = new ArrayList<>();
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();
educationList.add(new Education("Computer Science", R.drawable.computer_science, Education.UNIVERSITY_TYPE));
educationList.add(new Education("Data Structure", R.drawable.data_structure, Education.UNIVERSITY_TYPE));
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) {
}
});
}
}
below education.xml where I have implemented recyclerview
<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="horizontal"
android:id="@+id/recyclerView"
android:layout_height="match_parent"/>
</RelativeLayout>
below education_list.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="match_parent"
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" />
</LinearLayout>
below subject.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/colorBlust"
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_subject"
android:textColor="@color/colorWhite"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
below subject_list.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:background="@color/colorBlust"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:src="@drawable/computer_science"
android:padding="5dp"
android:layout_marginStart="10dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/computers_science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="@color/colorWhite" />
</LinearLayout>
</LinearLayout>
below Education.java model class
public class Education {
public static final int UNIVERSITY_TYPE=0;
public static final int INTERNET_TYPE = 1;
public static final int SUBJECT_TYPE=2;
public int type;
public int image;
public String words;
public Education(String words, int image, int type) {
this.words = words;
this.image = image;
this.type = type;
}
@SerializedName("duration")
@Expose
private String duration;
@SerializedName("institution")
@Expose
private String institution;
@SerializedName("degree")
@Expose
private String degree;
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getInstitution() {
return institution;
}
public void setInstitution(String institution) {
this.institution = institution;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public int type() {
return type;
}
}
below my Education.java class
public class Education {
public static final int UNIVERSITY_TYPE=0;
public static final int INTERNET_TYPE = 1;
public static final int SUBJECT_TYPE=2;
public int type;
public int image;
public String words;
public Education(String words, int image, int type) {
this.words = words;
this.image = image;
this.type = type;
}
@SerializedName("duration")
@Expose
private String duration;
@SerializedName("institution")
@Expose
private String institution;
@SerializedName("degree")
@Expose
private String degree;
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getInstitution() {
return institution;
}
public void setInstitution(String institution) {
this.institution = institution;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public int type() {
return type;
}
}
I have put break point and run code in debug mode and I got below exception
Cannot cast 'activity.drawer.navigation.com.kitabsawticlone.EducationAdapter$UniversityViewHolder' to 'activity.drawer.navigation.com.kitabsawticlone.EducationAdapter.EducationViewHolder'
First of all, you should not setup you recycler view and adapter at the response of network call. You should set all these up as part of your activity.
Second, you should add/update/remove your models to your adapter as the response of network, and then call notifydatasetchanged or some variation of that.
Third, you have some bug here
case Education.SUBJECT_TYPE: ((SubjectViewHolder)holder).subjects.getContext().getString(R.string.university_subject);
((SubjectViewHolder)holder).subjectImage.getContext().getDrawable(R.drawable.university_subjects); break;