我正在使用带有片段的recyclerview。我的一个片段开始了一个新片段,其中显示了医生个人资料,其中包含图像和关于我的文字很长。除了垂直scrollview之外,其他所有东西都可以正常工作。 这是完全学习的目的,对此不是新手。尝试学习这些工作原理。我附加了片段类,适配器和布局。任何类型的参考,建议,纠正都可以救我。 预先感谢。
适配器
public class DoctorProfileAdapter extends RecyclerView.Adapter<DoctorProfileAdapter.DoctorProfileViewHolder> {
private Context mContext;
private List<Doctors> doctorsList;
public DoctorProfileAdapter(Context mContext, List<Doctors> doctorsList) {
this.mContext = mContext;
this.doctorsList = doctorsList;
}
@NonNull
@Override
public DoctorProfileViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_doctor_profile, viewGroup, false);
return new DoctorProfileViewHolder(view);
}
@Override
public void onBindViewHolder(final DoctorProfileViewHolder holder, final int position) {
Doctors doctors = doctorsList.get(position);
holder.name.setText(doctors.getName());
holder.degree.setText(doctors.getDegree());
holder.specialty.setText(doctors.getSpecialty());
holder.departments.setText(doctors.getDepartments());
holder.bio.setText(doctors.getBio());
Glide.with(mContext).load(doctors.getThumbnail()).into(holder.thumbnail);
}
@Override
public int getItemCount() {
return doctorsList.size();
}
public class DoctorProfileViewHolder extends RecyclerView.ViewHolder {
public TextView name, degree, departments, specialty, bio;
public ImageView thumbnail;
public DoctorProfileViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
degree = view.findViewById(R.id.degree);
departments = view.findViewById(R.id.department);
specialty = view.findViewById(R.id.specialty);
bio = view.findViewById(R.id.bio);
thumbnail = view.findViewById(R.id.thumbnail);
}
}
}
片段
public class DoctorProfileFragment extends Fragment {
private String ID, name, specialty, degree, thumbnail, departments, bio;
private DatabaseReference databaseReference;
private ValueEventListener listener;
private DoctorProfileAdapter adapter;
private List<Doctors> list;
public DoctorProfileFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle("Profile");
Bundle bundle = getArguments();
if (bundle != null) {
ID = (String) bundle.getSerializable("id");
name = (String) bundle.getSerializable("name");
specialty = (String) bundle.getSerializable("specialty");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
databaseReference = FirebaseDatabase.getInstance().getReference();
list = new ArrayList<>();
View view = inflater.inflate(R.layout.blank_fragment, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
adapter = new DoctorProfileAdapter(getContext(), list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
asyncTask();
return view;
}
private void asyncTask(){
databaseReference = databaseReference.child("doctors").child(ID);
listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
degree = dataSnapshot.child("degree").getValue(String.class);
departments = dataSnapshot.child("departments").getValue(String.class);
bio = dataSnapshot.child("bio").getValue(String.class);
thumbnail = dataSnapshot.child("thumbnail").getValue(String.class);
Doctors doctors = new Doctors(name, degree, departments, specialty, thumbnail, bio);
list.add(doctors);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
databaseReference.addValueEventListener(listener);
}
}
布局
<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:orientation="vertical">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:textAppearance="@android:style/TextAppearance.Material.Headline"
android:textSize="18sp"
tools:text="This is me" />
<View
android:layout_width="125dp"
android:layout_height="2dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="5dp"
android:background="@color/colorPrimaryDark" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Degree:"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin"/>
<TextView
android:id="@+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="@dimen/activity_horizontal_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Specialty:"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin" />
<TextView
android:id="@+id/specialty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Department:"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/department"
android:layout_marginTop="5dp"
android:layout_marginStart="@dimen/activity_horizontal_margin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bio"/>
</LinearLayout>
</ScrollView>
</LinearLayout>