Android OnClickListener 在片段中不工作
Android OnClickListener not working in fragment
我有一个片段显示了我从 firebase 中提取的用户详细信息。我正在尝试设置一个 OnClickListener 以便登录的用户可以更改他们的图像,但我似乎无法让它工作。我查看了 Cat 日志,没有看到任何错误。我看过其他人在做类似的事情,但似乎无法找出为什么我的行不通。
编辑:我发现了问题,但现在有一个不同的问题当片段第一次加载时,Onlick 不起作用,直到我刷新片段然后 Onclick 起作用,但我不知道为什么片段需要刷新才能让 Onclick 工作?
片段;
ImageView adminImage;
adminImage = v.findViewById(R.id.AdminProfilePicture);
adminImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),"Long hold to change image",Toast.LENGTH_SHORT).show();
}
});
adminImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGallery,1000);
return false;
}
});
Main Activity设置片段的地方;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_Account_admin:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileAdminFragment()).commit();
break;
case R.id.nav_Games:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new GamesAdminFragment()).commit();
break;
case R.id.nav_Users:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AllUsersAdminFragment()).commit();
break;
case R.id.nav_Fixture:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FixturesAdminFragment()).commit();
break;
case R.id.nav_News:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new NewsAdminFragment()).commit();
break;
case R.id.nav_Contact:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ContactAdminFragment()).commit();
break;
default:
}
draw.closeDrawer(GravityCompat.START);
return true;
}
});
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, draw, toolbar,
R.string.nav_app_bar_open_drawer_description, R.string.navigation_drawer_close);
draw.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
navigationView.setCheckedItem(R.id.nav_Account);
}
}
XML;
<RelativeLayout
android:id="@+id/rellay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/grad"
android:paddingBottom="20dp">
<RelativeLayout
android:id="@+id/imageViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/circle_border">
<ImageView
android:id="@+id/AdminProfilePicture"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="9dp"
android:adjustViewBounds="true"
android:background="@drawable/circle"
android:padding="3dp"
android:scaleType="centerInside"
android:src="@drawable/ic_user" />
</RelativeLayout>
Img of XML
完整片段代码
public class ProfileAdminFragment extends Fragment {
FirebaseAuth fAuth;
FirebaseFirestore fStore;
TextView uName, uEmail, uPhone;
FusedLocationProviderClient fusedLocationProviderClient;
TextView userlocation;
ImageView adminImage;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile_admin, container, false);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
userlocation = v.findViewById(R.id.tv_location);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// getLocation();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
uName = v.findViewById(R.id.profileFullName);
uEmail = v.findViewById(R.id.profileEmail);
uPhone = v.findViewById(R.id.profilePhone);
DocumentReference docR = fStore.collection("Users").document(fAuth.getCurrentUser().getUid());
docR.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
uName.setText(documentSnapshot.getString("FullName"));
uEmail.setText(documentSnapshot.getString("UserEmail"));
uPhone.setText(documentSnapshot.getString("PhoneNumber"));
}
}
});
getLocation();
// profile image
adminImage = v.findViewById(R.id.AdminProfilePicture);
adminImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Long hold to change image", Toast.LENGTH_SHORT).show();
}
});
adminImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGallery, 1000);
return false;
}
});
return v;
}
请尝试在 ImageView 中添加 android:clickable="true"。
<RelativeLayout
android:id="@+id/rellay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/grad"
android:paddingBottom="20dp">
<RelativeLayout
android:id="@+id/imageViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/circle_border">
<ImageView
android:id="@+id/AdminProfilePicture"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="9dp"
android:adjustViewBounds="true"
android:background="@drawable/circle"
android:padding="3dp"
android:scaleType="centerInside"
android:src="@drawable/ic_user"
android:clickable="true" />
</RelativeLayout>
例如,如果您从 activity a 中调用片段,
然后 :
将片段中的 v 初始化为 public static 。之后从 activity a.
调用 setOnClickListener
在你的 activity 中:
YourFragment.v.findViewById("AdminProfilePicture").setOnClickListener (...);
如果解决了您的问题请回复
已解决
所以我发现我正在加载我的用户配置文件而不是我的管理员配置文件,所以在加载片段时发生冲突所以为了让 Onclick 工作我必须手动刷新片段
由此而来;
new ProfileFragment()).commit();
对此;
new ProfileAdminFragment()).commit();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileAdminFragment()).commit();
navigationView.setCheckedItem(R.id.nav_Account_admin);
}
我有一个片段显示了我从 firebase 中提取的用户详细信息。我正在尝试设置一个 OnClickListener 以便登录的用户可以更改他们的图像,但我似乎无法让它工作。我查看了 Cat 日志,没有看到任何错误。我看过其他人在做类似的事情,但似乎无法找出为什么我的行不通。
编辑:我发现了问题,但现在有一个不同的问题当片段第一次加载时,Onlick 不起作用,直到我刷新片段然后 Onclick 起作用,但我不知道为什么片段需要刷新才能让 Onclick 工作?
片段;
ImageView adminImage;
adminImage = v.findViewById(R.id.AdminProfilePicture);
adminImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),"Long hold to change image",Toast.LENGTH_SHORT).show();
}
});
adminImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGallery,1000);
return false;
}
});
Main Activity设置片段的地方;
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_Account_admin:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileAdminFragment()).commit();
break;
case R.id.nav_Games:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new GamesAdminFragment()).commit();
break;
case R.id.nav_Users:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AllUsersAdminFragment()).commit();
break;
case R.id.nav_Fixture:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FixturesAdminFragment()).commit();
break;
case R.id.nav_News:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new NewsAdminFragment()).commit();
break;
case R.id.nav_Contact:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ContactAdminFragment()).commit();
break;
default:
}
draw.closeDrawer(GravityCompat.START);
return true;
}
});
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, draw, toolbar,
R.string.nav_app_bar_open_drawer_description, R.string.navigation_drawer_close);
draw.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
navigationView.setCheckedItem(R.id.nav_Account);
}
}
XML;
<RelativeLayout
android:id="@+id/rellay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/grad"
android:paddingBottom="20dp">
<RelativeLayout
android:id="@+id/imageViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/circle_border">
<ImageView
android:id="@+id/AdminProfilePicture"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="9dp"
android:adjustViewBounds="true"
android:background="@drawable/circle"
android:padding="3dp"
android:scaleType="centerInside"
android:src="@drawable/ic_user" />
</RelativeLayout>
Img of XML
完整片段代码
public class ProfileAdminFragment extends Fragment {
FirebaseAuth fAuth;
FirebaseFirestore fStore;
TextView uName, uEmail, uPhone;
FusedLocationProviderClient fusedLocationProviderClient;
TextView userlocation;
ImageView adminImage;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile_admin, container, false);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
userlocation = v.findViewById(R.id.tv_location);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// getLocation();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
uName = v.findViewById(R.id.profileFullName);
uEmail = v.findViewById(R.id.profileEmail);
uPhone = v.findViewById(R.id.profilePhone);
DocumentReference docR = fStore.collection("Users").document(fAuth.getCurrentUser().getUid());
docR.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
uName.setText(documentSnapshot.getString("FullName"));
uEmail.setText(documentSnapshot.getString("UserEmail"));
uPhone.setText(documentSnapshot.getString("PhoneNumber"));
}
}
});
getLocation();
// profile image
adminImage = v.findViewById(R.id.AdminProfilePicture);
adminImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Long hold to change image", Toast.LENGTH_SHORT).show();
}
});
adminImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGallery, 1000);
return false;
}
});
return v;
}
请尝试在 ImageView 中添加 android:clickable="true"。
<RelativeLayout
android:id="@+id/rellay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/grad"
android:paddingBottom="20dp">
<RelativeLayout
android:id="@+id/imageViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/circle_border">
<ImageView
android:id="@+id/AdminProfilePicture"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="9dp"
android:adjustViewBounds="true"
android:background="@drawable/circle"
android:padding="3dp"
android:scaleType="centerInside"
android:src="@drawable/ic_user"
android:clickable="true" />
</RelativeLayout>
例如,如果您从 activity a 中调用片段, 然后 : 将片段中的 v 初始化为 public static 。之后从 activity a.
调用 setOnClickListener在你的 activity 中:
YourFragment.v.findViewById("AdminProfilePicture").setOnClickListener (...);
如果解决了您的问题请回复
已解决 所以我发现我正在加载我的用户配置文件而不是我的管理员配置文件,所以在加载片段时发生冲突所以为了让 Onclick 工作我必须手动刷新片段
由此而来;
new ProfileFragment()).commit();
对此;
new ProfileAdminFragment()).commit();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileAdminFragment()).commit();
navigationView.setCheckedItem(R.id.nav_Account_admin);
}