这是2的“建立一个社会化媒体应用Android Studio中”教程中的一部分,我们将要在本文中介绍了以下功能:
- 我们将创建5个片段(首页,用户,AddBlog,ChatList,个人资料)的底部导航。
- 在HomeFragment上,我们将显示所有添加的博客。
- 在UsersFragment中, 我们将显示所有注册用户。
- 在AddBlogFragment中,我们将添加我们的博客。
- 在ChatlistFragment中,我们将显示与我们聊天的所有用户的聊天列表。
- 在ProfileFragment中,我们将显示用户的Profile,其中将显示用户的数据和用户编写的博客。
分步实施
步骤1:首先,我们将在res文件夹中创建一个菜单目录。请参阅本文“如何在Android Studio中创建菜单文件夹和菜单文件”。并将菜单文件命名为menu_nav.xml,以创建底部导航的布局。以下是menu_nav.xml文件的代码。
XML
XML
Java
package com.example.socialmediaapp;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class DashboardActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
String myuid;
ActionBar actionBar;
BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
actionBar = getSupportActionBar();
actionBar.setTitle("Profile Activity");
firebaseAuth = FirebaseAuth.getInstance();
navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(selectedListener);
actionBar.setTitle("Home");
// When we open the application first
// time the fragment should be shown to the user
// in this case it is home fragment
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment, "");
fragmentTransaction.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
actionBar.setTitle("Home");
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment, "");
fragmentTransaction.commit();
return true;
case R.id.nav_profile:
actionBar.setTitle("Profile");
ProfileFragment fragment1 = new ProfileFragment();
FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.content, fragment1);
fragmentTransaction1.commit();
return true;
case R.id.nav_users:
actionBar.setTitle("Users");
UsersFragment fragment2 = new UsersFragment();
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.content, fragment2, "");
fragmentTransaction2.commit();
return true;
case R.id.nav_chat:
actionBar.setTitle("Chats");
ChatListFragment listFragment = new ChatListFragment();
FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.content, listFragment, "");
fragmentTransaction3.commit();
return true;
case R.id.nav_addblogs:
actionBar.setTitle("Add Blogs");
AddBlogsFragment fragment4 = new AddBlogsFragment();
FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.content, fragment4, "");
fragmentTransaction4.commit();
return true;
}
return false;
}
};
}
步骤2:将依赖项添加到build.gradle(模块:app)
导航到Gradle脚本>构建。 gradle(Module:app)并在“依赖项”部分添加以下依赖项。
implementation ‘com.google.android.material:material:1.2.0’
步骤3:使用activity_dashboard.xml文件
用户登录后,此页面将是我们应用程序中的第一个活动。导航到应用程序> res>布局> activity_dashboard.xml并将以下代码添加到该文件中。以下是activity_dashboard.xml文件的代码。
XML格式
步骤4:点击5个新的空白片段
转到应用程序(右键单击)>新建>片段>片段(空白),并将片段命名为HomeFragment,ProfileFragment,UsersFragment,ChatListFragment和AddBlogsFragment 。
步骤5:使用DashboardActivity。 Java文件
在此文件中,我们只是根据所选的导航项显示片段。然后,我们将显示相应的片段。
HomeFragment fragment=new HomeFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content,fragment,"");
fragmentTransaction.commit();
转到DashboardActivity。 Java文件并参考以下代码。以下是DashboardActivity的代码。 Java文件。
Java
package com.example.socialmediaapp;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class DashboardActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
String myuid;
ActionBar actionBar;
BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
actionBar = getSupportActionBar();
actionBar.setTitle("Profile Activity");
firebaseAuth = FirebaseAuth.getInstance();
navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(selectedListener);
actionBar.setTitle("Home");
// When we open the application first
// time the fragment should be shown to the user
// in this case it is home fragment
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment, "");
fragmentTransaction.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
actionBar.setTitle("Home");
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment, "");
fragmentTransaction.commit();
return true;
case R.id.nav_profile:
actionBar.setTitle("Profile");
ProfileFragment fragment1 = new ProfileFragment();
FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.content, fragment1);
fragmentTransaction1.commit();
return true;
case R.id.nav_users:
actionBar.setTitle("Users");
UsersFragment fragment2 = new UsersFragment();
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.content, fragment2, "");
fragmentTransaction2.commit();
return true;
case R.id.nav_chat:
actionBar.setTitle("Chats");
ChatListFragment listFragment = new ChatListFragment();
FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.content, listFragment, "");
fragmentTransaction3.commit();
return true;
case R.id.nav_addblogs:
actionBar.setTitle("Add Blogs");
AddBlogsFragment fragment4 = new AddBlogsFragment();
FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.content, fragment4, "");
fragmentTransaction4.commit();
return true;
}
return false;
}
};
}
输出:
有关本文中使用的所有可绘制文件,请参考以下链接: https : //drive.google.com/drive/folders/1M_knOH_ugCuwSP5nkYzeD4dRp-Honzbe?usp=sharing
下面是执行这些操作后的文件结构: