Может быть, вы можете использовать абстрактный метод для создания адаптера
Это может не решить вашу проблему, но вы можете использовать логику адаптера в своем классе подфрагмента, этот способ будет более понятным
public abstract class BaseFragment extends Fragment {
private ArrayList<CholesterolMonitor> objectList;
private int mColumnCount = 1;
public void setObjectList(String listKey){
if (getArguments()!=null){
this.objectList= getArguments().getParcelableArrayList(listKey);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanseState)
{
View view = provideYourFragmentView(inflater, parent, savedInstanseState);
// Set to recycler view
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(this.createAdapter());
}
return view;
}
abstract <ViewHolderT extends RecyclerView.ViewHolder, AdapterT extends RecyclerView.Adapter<ViewHolderT>> AdapterT createAdapter();
}
SubFragmentA
public class SubFragmentA extends BaseFragment {
@Override
SubAdapterA createAdapter() {
return new SubAdapterA();
}
public static class SubAdapterA extends RecyclerView.Adapter<SubViewHolderA> {
@NonNull
@Override
public SubViewHolderA onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return null;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolderA subViewHolder, int i) {
}
@Override
public int getItemCount() {
return 0;
}
}
public static class SubViewHolderA extends RecyclerView.ViewHolder {
public SubViewHolderA(@NonNull View itemView) {
super(itemView);
}
}
}
SubFragmentB
public class SubFragmentB extends BaseFragment {
@Override
SubAdapterB createAdapter() {
return new SubAdapterB();
}
public static class SubAdapterB extends RecyclerView.Adapter<SubViewHolderB> {
@NonNull
@Override
public SubViewHolderB onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return null;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolderB subViewHolder, int i) {
}
@Override
public int getItemCount() {
return 0;
}
}
public static class SubViewHolderB extends RecyclerView.ViewHolder {
public SubViewHolderB(@NonNull View itemView) {
super(itemView);
}
}
}
Если вы получили несколько типов ViewHolder, вы можете сделать это
public static class BaseViewHolder extends RecyclerView.ViewHolder {
protected TextView commonTextView;
public BaseViewHolder(@NonNull View itemView) {
super(itemView);
//init commonTextView;
}
}
public static class SubViewHolderB extends BaseViewHolder {
private TextView textViewB;
public SubViewHolderB(@NonNull View itemView) {
super(itemView);
//init textViewB
}
public void bindData(DataModelB data) {
commonTextView.setText("Common text set Integer data :" + data.intField);
textViewB.setText("This is Integer :" + data.intField);
}
}
public static class SubViewHolderA extends BaseViewHolder {
private TextView textViewA;
public SubViewHolderA(@NonNull View itemView) {
super(itemView);
//init textViewB
}
public void bindData(DataModelA data) {
commonTextView.setText("Common text set String data :" + data.strField);
textViewA.setText("This is String :" + data.strField);
}
}
public static class DataModelB {
int intField;
}
public static class DataModelA {
String strField;
}