在 fragments android studio 中处理 appbar 中的点击事件
handle clickevent in appbar in fragments android studio
我的 fragment
中的自定义 appbar
遇到问题,当我单击应用栏中的任何项目时没有任何反应。我已经按照 this document 在我的片段中创建了一个应用栏,但仍然很成功。
我有一个 one activity,它只包含一个片段容器,我从中切换片段。在其中一个片段中,我想添加一个自定义片段拥有的应用栏。我首先创建了一个这样的菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="never">
</item>
<item
android:id="@+id/about"
android:title="About"
android:icon="@drawable/ic_about"
app:showAsAction="never">
</item>
<item
android:id="@+id/logout"
android:title="Logout"
android:icon="@drawable/ic_logout"
app:showAsAction="never">
</item>
</menu>
然后在我的 profile.xml 中添加了一个这样的工具栏:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainFragments.AddFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/Bar_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/darkBlue"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
//alot of code in between....
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_prof"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
//more stuff implemented
</RelativeLayout>
但是,我的菜单没有膨胀,如果我通过 xml 膨胀它,当我点击任何项目时没有任何反应,这是怎么回事?谢谢!
这是我的个人资料片段 looks like:
编辑
此处已满profilefragment.java:
package se.umu.moad0012.myservice.MainFragments;
import se.umu.moad0012.myservice.Adapters.FragmentPagerAdapter;
import se.umu.moad0012.myservice.MainActivity;
import se.umu.moad0012.myservice.Models.User;
import se.umu.moad0012.myservice.ProfileFragments.AboutFragment;
import se.umu.moad0012.myservice.ProfileFragments.SettingsFragment;
import se.umu.moad0012.myservice.R;
import se.umu.moad0012.myservice.StartFragments.LoginFragment;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.makeramen.roundedimageview.RoundedImageView;
public class ProfileFragment extends Fragment {
private RoundedImageView mProfileRoundedImageView;
private TextView mPosts, mFollowers, mFollowing, mUserName;
private TabLayout mTabLayout;
private String profileId;
private FirebaseDatabase mFirebaseDatabase;
private ViewPager2 mViewPager2;
private final String[] titles = new String[]{"Posts", "Reviews"};
private final int[] icons = new int[]{R.drawable.ic_grid, R.drawable.feedback};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_profile, container, false);
//declare variables
declareVariables(v);
//adapter for viewpager2
mViewPager2.setAdapter(new FragmentPagerAdapter(this));
//configure tabs here
tabMediator();
//FirebaseAuth.getInstance().signOut();
//set profile info
setProfileInfo();
return v;
}
private void tabMediator() {
new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles[position]);
tab.setIcon(icons[position]);
}
}).attach();
}
private void setProfileInfo() {
mFirebaseDatabase.getReference().child("Users").child(profileId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
mUserName.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void declareVariables(View v) {
mViewPager2 = v.findViewById(R.id.viewpager_profile);
mTabLayout = v.findViewById(R.id.tabLayout_profile);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mUserName = v.findViewById(R.id.userNameProfileFragment);
mFollowers = v.findViewById(R.id.followers);
mPosts = v.findViewById(R.id.posts);
mFollowing = v.findViewById(R.id.following);
//mProfileRoundedImageView = v.findViewById(R.id.profileImage);
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
profileId = fUser.getUid();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar_prof);
toolbar.inflateMenu(R.menu.popup_menu_profile);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
Log.d("TAG", "onOptionsItemSelected: settings");
return true;
case R.id.about:
Log.d("TAG", "onOptionsItemSelected: about");
return true;
case R.id.logout:
Log.d("TAG", "onOptionsItemSelected: logout");
return true;
}
return false;
}
});
}
}
编辑2
我刚才注意到它可以工作,只是我的文字是白色的,因此没有显示出来。抱歉给您带来不便。
把这个放在你的片段中
public class ProfileFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = view.findViewById(R.id.my_toolbar);
toolbar.inflateMenu(R.menu.your_menu);
toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
//handle click
return true;
case R.id.new_pic:
// handle here
return true;
default:
return false;
}
}
});
}
}
我的 fragment
中的自定义 appbar
遇到问题,当我单击应用栏中的任何项目时没有任何反应。我已经按照 this document 在我的片段中创建了一个应用栏,但仍然很成功。
我有一个 one activity,它只包含一个片段容器,我从中切换片段。在其中一个片段中,我想添加一个自定义片段拥有的应用栏。我首先创建了一个这样的菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="never">
</item>
<item
android:id="@+id/about"
android:title="About"
android:icon="@drawable/ic_about"
app:showAsAction="never">
</item>
<item
android:id="@+id/logout"
android:title="Logout"
android:icon="@drawable/ic_logout"
app:showAsAction="never">
</item>
</menu>
然后在我的 profile.xml 中添加了一个这样的工具栏:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainFragments.AddFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/Bar_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/darkBlue"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
//alot of code in between....
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_prof"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
//more stuff implemented
</RelativeLayout>
但是,我的菜单没有膨胀,如果我通过 xml 膨胀它,当我点击任何项目时没有任何反应,这是怎么回事?谢谢!
这是我的个人资料片段 looks like:
编辑
此处已满profilefragment.java:
package se.umu.moad0012.myservice.MainFragments;
import se.umu.moad0012.myservice.Adapters.FragmentPagerAdapter;
import se.umu.moad0012.myservice.MainActivity;
import se.umu.moad0012.myservice.Models.User;
import se.umu.moad0012.myservice.ProfileFragments.AboutFragment;
import se.umu.moad0012.myservice.ProfileFragments.SettingsFragment;
import se.umu.moad0012.myservice.R;
import se.umu.moad0012.myservice.StartFragments.LoginFragment;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.makeramen.roundedimageview.RoundedImageView;
public class ProfileFragment extends Fragment {
private RoundedImageView mProfileRoundedImageView;
private TextView mPosts, mFollowers, mFollowing, mUserName;
private TabLayout mTabLayout;
private String profileId;
private FirebaseDatabase mFirebaseDatabase;
private ViewPager2 mViewPager2;
private final String[] titles = new String[]{"Posts", "Reviews"};
private final int[] icons = new int[]{R.drawable.ic_grid, R.drawable.feedback};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_profile, container, false);
//declare variables
declareVariables(v);
//adapter for viewpager2
mViewPager2.setAdapter(new FragmentPagerAdapter(this));
//configure tabs here
tabMediator();
//FirebaseAuth.getInstance().signOut();
//set profile info
setProfileInfo();
return v;
}
private void tabMediator() {
new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles[position]);
tab.setIcon(icons[position]);
}
}).attach();
}
private void setProfileInfo() {
mFirebaseDatabase.getReference().child("Users").child(profileId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
mUserName.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void declareVariables(View v) {
mViewPager2 = v.findViewById(R.id.viewpager_profile);
mTabLayout = v.findViewById(R.id.tabLayout_profile);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mUserName = v.findViewById(R.id.userNameProfileFragment);
mFollowers = v.findViewById(R.id.followers);
mPosts = v.findViewById(R.id.posts);
mFollowing = v.findViewById(R.id.following);
//mProfileRoundedImageView = v.findViewById(R.id.profileImage);
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
profileId = fUser.getUid();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar_prof);
toolbar.inflateMenu(R.menu.popup_menu_profile);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
Log.d("TAG", "onOptionsItemSelected: settings");
return true;
case R.id.about:
Log.d("TAG", "onOptionsItemSelected: about");
return true;
case R.id.logout:
Log.d("TAG", "onOptionsItemSelected: logout");
return true;
}
return false;
}
});
}
}
编辑2
我刚才注意到它可以工作,只是我的文字是白色的,因此没有显示出来。抱歉给您带来不便。
把这个放在你的片段中
public class ProfileFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = view.findViewById(R.id.my_toolbar);
toolbar.inflateMenu(R.menu.your_menu);
toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
//handle click
return true;
case R.id.new_pic:
// handle here
return true;
default:
return false;
}
}
});
}
}