gpt4 book ai didi

java - 如何在应用程序启动时使用 recyclerview 加载 fragment ?

转载 作者:行者123 更新时间:2023-12-02 00:24:26 26 4
gpt4 key购买 nike

我在 android 中构建了一个底部导航选项卡栏,它有 5 个 fragment ,但问题是在应用程序启动时它不会从 firestore 数据库加载 recyclerview 列表。这个 fragment 就是Home Fragment。但如果我随机打开另一个选项卡,然后进入主页选项卡,主页选项卡列表会自行加载,但不会在应用程序启动时自动加载

下面是代码。请协助

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "DocSnippets";

private FirebaseAuth mauth;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference UsersRef = db.collection("Users");
private DocumentReference noteRef = db.document("Notebook/My First Note");
private MySharedPreferences sp;
private DocumentReference currentUserRef;

private Button notificationList;


private String mUserId, tokenId, currentUserProfilePic, currentUserName, currentUserSpinnerC;




String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = MySharedPreferences.getInstance(this);
setContentView(R.layout.activity_main);

mauth = FirebaseAuth.getInstance();
// currentUserID = mauth.getCurrentUser().getUid();
UsersRef =FirebaseFirestore.getInstance().collection("Users");
mUserId = mauth.getUid();
tokenId = FirebaseInstanceId.getInstance().getToken();


Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottom_nav_view);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);



}





private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();

break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
// Intent intent = new Intent(MainActivity.this, AddPostActivity.class);
// startActivity(intent);
fragment = new NotificationsFragment();
break;
case R.id.navigation_post:
fragment = new ProfileFragment();
break;
case R.id.navigation_postlist:
fragment = new ProfileFragment();
break;

}
return loadFragment(fragment);
}
};


private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}


@Override
protected void onStart()
{
super.onStart();

FirebaseUser currentUser = mauth.getCurrentUser();

if(currentUser == null)
{
SendUserToSignInActivity();
}
else
{
retrieveCurrentUserInfo();
checkUserExistence();
}

}

private void checkUserExistence() {

final String current_user_id = mauth.getCurrentUser().getUid();
}


private void SendUserToSignInActivity() {

Intent signInIntent = new Intent(MainActivity.this, SignInActivity.class);
signInIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(signInIntent);
finish();

}

@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void retrieveCurrentUserInfo(){
currentUserRef = db.collection("users").document(mUserId);
currentUserRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()){
currentUserName = (String) document.getString("username");
currentUserProfilePic =(String) document.getString("profile_pic");
currentUserSpinnerC = (String) document.getString("spinnerC");


CircleImageView UserImage = (CircleImageView) findViewById(R.id.current_user_Image);
Picasso.get()
.load(currentUserProfilePic)
.resize(50, 50)
.centerCrop()
.into(UserImage);

TextView username = (TextView) findViewById(R.id.current_user_name);
TextView spinnerC = (TextView) findViewById(R.id.current_user_spinnerC);
username.setText(currentUserName);
spinnerC.setText(currentUserSpinnerC);

}
}


}
});

}


}

最佳答案

启动 Activity 时不会加载 fragment ,因此请在 onCreate() 方法中添加以下代码

loadFragment(new HomeFragment());

关于java - 如何在应用程序启动时使用 recyclerview 加载 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com