我正在尝试在我想传递列表的RecyclerViewdadapter类中创建cv应用程序,其中一个是我已经通过的模型类,另一个是虚拟数据。我正在关注这个stackoverflow链接Two ArrayList one RecyclerView Adapter
我想实现这个用户界面
第二张当前屏幕截图
在MyAdapter类之下
public class EducationAdapter extends RecyclerView.Adapter<EducationAdapter.ViewHolder> {
final int Internet_TYPE = 0;
final int Dummy_TYPE = 1;
public List<Education> educationList;
public Context context;
public List<FakeData> fakeData;
public int[] subjectImage;
public String[] subjectText;
public EducationAdapter(List<Education> educationList, Context context, List<FakeData> fakeData, int[] subjectImage, String[] subjectText) {
this.educationList = educationList;
this.context = context;
this.fakeData = fakeData;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
}
@NonNull
@Override
public EducationAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == Internet_TYPE) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.education_item, parent, false); // change
return new EducationAdapter.ViewHolder(itemView);
}
if (viewType == Dummy_TYPE) {
return new ViewHolder.FakeViewHolder(itemView);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull EducationAdapter.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
Education education = educationList.get(position);
holder.duration.setText(education.getDuration());
holder.degree.setText(education.getDegree());
holder.institution.setText(education.getInstitution());
}
if (holder instanceof ViewHolder.FakeViewHolder) {
}
}
@Override
public int getItemCount() {
return educationList.size();
}
public 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);
}
public class FakeViewHolder extends RecyclerView.ViewHolder {
public TextView item;
public ImageView icon;
public FakeViewHolder(View itemView) {
super(itemView);
item = (TextView) itemView.findViewById(R.id.item);
icon = (ImageView) itemView.findViewById(R.id.icon);
}
public void populate(FakeData fakeDatas) {
item.setText(fakeDatas.getImage());
icon.set
dataSnippet.setText(imageDataWrapper.getPage_Desc());
Picasso.with(context).load(imageDataWrapper.getPage_ImageThumb()).into(image);
}
}
}
}
below my EducationItem where I have implemented network call and dummy data
public class EducationItem extends AppCompatActivity {
private EducationAdapter educationAdapter;
public List<Education> educationList;
public List<FakeData> fakeData;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.education);
final int [] subjectImage = {R.drawable.computer_science,
R.drawable.data_structure,
};
final String[] subjectText = {
"Computer Science",
"Data Structure",
};
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();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
educationAdapter = new EducationAdapter(educationList, EducationItem.this, fakeData, subjectImage, subjectText); // changes
recyclerView.setAdapter(educationAdapter);
}
@Override
public void onFailure(Call<KitabSawti> call, Throwable t) {
}
});
}
}
在educution_item下面。
<?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>
subjectList以下
<?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: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>
答案 0 :(得分:0)
您应该在包含所有项目的一个列表中加入两个列表。在一个适配器中处理两个不同的列表可能会产生许多不同的问题。
private void setAdapter() {
List<Item> items = new ArrayList<>();
items.add(new EducationItem());
items.add(new FakeItem());
recycler.setAdapter(newAdapter(items));
}
public interface Item {}
class EducationItem implements Item {
//...fields
}
class FakeItem implements Item {
//...fields
}
答案 1 :(得分:0)
正如Marek所说,您应该将两个列表合并为一个。将Education
和FakeData
视为相同的数据类型。
使用这种方法,您还应该重写getViewType
方法以指示项目的ViewType
,以便onCreateViewHolder
可以收到您定义的ViewType
。