Skip to content

Instantly share code, notes, and snippets.

@kyodgorbek
Created October 31, 2018 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyodgorbek/441864cbad718085e3ae81b11ae9bd04 to your computer and use it in GitHub Desktop.
Save kyodgorbek/441864cbad718085e3ae81b11ae9bd04 to your computer and use it in GitHub Desktop.
Android app
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;
}
}
<?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>
<?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>
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) {
switch (educationList.get(position).type) {
case 0:
return Education.INTERNET_TYPE;
case 1:
return Education.SUBJECT_TYPE;
case 2:
return Education.UNIVERSITY_TYPE;
default:
return -1;
}
}
@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().getResources(R.string.university_subjects);
((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;
}
}
}
}
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);
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));
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());
}
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) {
}
});
}
}
<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_subjects"
android:textColor="@color/colorWhite"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
<?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: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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment