Я пытался загрузить повторное представление фрагмента (MatchFragment) из другого фрагмента (HomeFragment). Оба фрагмента находятся в одном действии, и HomeFragment запускается первым при запуске действия. Я попытался сделать это, определив переменную View в Homefragment, которая содержит макет MatchFragment, и прикрепил к нему recyclerView, но ничего не помогло. У кого-нибудь есть идеи, как решить эту проблему?
HomeFragment. java
public class HomeFragment extends Fragment{
private DatabaseReference databaseReference,databaseReference2,databaseReference3;
private RecyclerView recyclerView;
private FirebaseRecyclerOptions<DataSetFire> options;
private FirebaseRecyclerAdapter<DataSetFire,FirebaseViewHolder> adapter1;
private FirebaseUser user;
private FirebaseAuth auth;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home_fragment, container, false);
View view1= inflater.inflate(R.layout.activity_match_fragment,container,false);
recyclerView = view1.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
auth=FirebaseAuth.getInstance();
user=auth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference().child("UserCrops");
databaseReference.keepSynced(true);
databaseReference2 = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid()).child("AddedCrops");
databaseReference2.keepSynced(true);
databaseReference3 = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid()).child("Current_readings");
databaseReference3.keepSynced(true);
options = new FirebaseRecyclerOptions.Builder<DataSetFire>().setQuery(databaseReference, DataSetFire.class).build();
adapter1 = new FirebaseRecyclerAdapter<DataSetFire, FirebaseViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FirebaseViewHolder holder, int position, @NonNull DataSetFire model) {
holder.setName(model.getName());
holder.setImage(model.getUrl());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String cropname=model.getName();
String cropurl=model.getUrl();
databaseReference2.child(cropname).child("name").setValue(cropname);
databaseReference2.child(cropname).child("url").setValue(cropurl);
databaseReference3.child(cropname).child("Humidity").setValue("N/A");
databaseReference3.child(cropname).child("Temperature").setValue("N/A");
databaseReference3.child(cropname).child("Soil_moisture").setValue("N/A");
Toast.makeText(getContext(), "Crop added successfully !", Toast.LENGTH_SHORT).show();
}
});
}
@NonNull
@Override
public FirebaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.crops, parent, false);
return new FirebaseViewHolder(view);
}
};
recyclerView.setAdapter(adapter1);
SliderView sliderView = view.findViewById(R.id.imageSlider);
SliderAdapterExample adapter = new SliderAdapterExample(this.getActivity());
sliderView.setSliderAdapter(adapter);
sliderView.setIndicatorAnimation(IndicatorAnimations.WORM); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
sliderView.setIndicatorSelectedColor(Color.WHITE);
sliderView.setIndicatorUnselectedColor(Color.GRAY);
sliderView.setScrollTimeInSec(4); //set scroll delay in seconds :
sliderView.startAutoCycle();
return view;
}
@Override
public void onStart() {
super.onStart();
adapter1.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter1.stopListening();
}
}
MatchFragment. java
public class MatchFragment extends Fragment {
private DatabaseReference databaseReference,databaseReference2,databaseReference3;
private RecyclerView recyclerView;
private FirebaseRecyclerOptions<DataSetFire> options;
private FirebaseRecyclerAdapter<DataSetFire,FirebaseViewHolder> adapter;
private FirebaseUser user;
private FirebaseAuth auth;
public MatchFragment() {
}
public static MatchFragment newInstance(String param1, String param2) {
MatchFragment fragment = new MatchFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_match_fragment, container, false);
return view;
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
}
activity_match_fragment. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MatchFragment">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/nunito_black"
android:text="ADD CROPS"
android:textColor="@android:color/black"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.07" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="440dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.75">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.82">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/nunito_bold"
android:gravity="center"
android:padding="10dp"
android:text="Click on any of the below for adding to your library"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
</androidx.constraintlayout.widget.ConstraintLayout>