Я пытаюсь заполнить окно повторной проверки внутри фрагмента, который находится внутри моей основной активности, и он не работает.
Он прекрасно работает, когда я делаю это в деятельности, а не внутри фрагмента.
Я думаю, что это как-то связано с контекстом, отправленным в linearLayoutManager.
Я думаю, что пытался всеми возможными способами получить контекст, такой как: getContext (), getActivity (), view.getContext (), this.getContext () ...
класс фрагмента:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shiftsview_fragment, container, false);
recyclerView = view.findViewById(R.id.recyclerShiftsView);
final TextView dataTV = view.findViewById(R.id.dataTV);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
return view;
}
@Override
public void onStart() {
super.onStart();
Query query = shiftsRef.orderBy("date", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Shift> options = new FirestoreRecyclerOptions.Builder<Shift>()
.setQuery(query, Shift.class)
.build();
mAdapter = new shiftAdapter(options);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(mAdapter);
mAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mAdapter.stopListening();
}
mainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get fragment manager
FragmentManager fm = getSupportFragmentManager();
//add
FragmentTransaction ft = fm.beginTransaction();
Fragment frag1 = new shiftsView();
ft.add(R.id.fragment_content_container, frag1);
//commit change
ft.commit();
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
if (user != null) {
updateUI(user);
mDrawerLayout = findViewById(R.id.drawer_layout);
Toolbar toolbar = findViewById(R.id.appBar);
//setting the appBar
setSupportActionBar(toolbar);
//setting the menu hamburger button
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar
, R.string.nav_open, R.string.nav_close);
//connection the menu button to the drawer
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
//setting the drawer menu select listener
NavigationView navigationView = findViewById(R.id.nav_drawer);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
// Addition code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
} else {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
finish();
startActivity(intent);
}
}
адаптер:
public class shiftAdapter extends FirestoreRecyclerAdapter<Shift, shiftAdapter.shiftHolder> {
private static final String TAG = "Shifter";
public shiftAdapter(@NonNull FirestoreRecyclerOptions<Shift> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull shiftHolder holder, int position, @NonNull Shift model) {
holder.dateTV.setText(model.getDate());
holder.intervalTV.setText(model.intervalString());
holder.wageTV.setText("150$");
Log.d(TAG, "onBindViewHolder: " + model.toString());
}
@NonNull
@Override
public shiftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.single_shift_view, viewGroup, false);
return new shiftHolder(view);
}
class shiftHolder extends RecyclerView.ViewHolder {
TextView dateTV, intervalTV, wageTV;
public shiftHolder(@NonNull View itemView) {
super(itemView);
dateTV = itemView.findViewById(R.id.shiftDateTV);
intervalTV = itemView.findViewById(R.id.shiftIntervalTV);
wageTV = itemView.findViewById(R.id.shiftWageTV);
Log.d(TAG, "shiftHolder: ");
}
}
}