我试图设置一个叠加片段,该片段会在触摸按钮时出现,并在再次单击该按钮时消失。我没有使用支持库片段,而是常规类型。因此,当您单击按钮以附加片段时,它会这样做,这里是代码:
boolean hasNativeFrag = false;
boolean isNativeFragShown = false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.native_chat) {
Log.v("NativeChat", "NativeChat");
//Instantiate fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
NativeFragment nativeFragment = new NativeFragment();
//Show/Hide
if (isNativeFragShown) {
fragmentTransaction.hide(nativeFragment);
Log.d("Hidden", "Hidden");
isNativeFragShown = false;
} else {
fragmentTransaction.show(nativeFragment);
Log.d("Shown", "Shown");
isNativeFragShown = true;
}
//Add/remove fragment
if (!hasNativeFrag) {
getFragmentManager().executePendingTransactions();
fragmentTransaction.add(R.id.FragmentContainer, nativeFragment);
isNativeFragShown = true;
hasNativeFrag = true;
}
fragmentTransaction.commit();
} else if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
但是当我再次点击该按钮时,我希望if条件能够运行,因为它现在可见了,它应该隐藏它。然而,nativeFragment.isVisible()始终返回false。我做错了什么?
非常感谢
更新1: 无法使用答案提出的任何方法,但是一个简单的布尔值至少会在它们之间切换,如下所示:
# Special characters - dollar signs, spaces inside of quotes, etc. -
# should be escaped with a single backslash or can cause deploy failures.
server {
listen [::]:80;
listen 80;
server_name $NOSSL_SERVER_NAME;
access_log /var/log/nginx/${APP}-access.log;
error_log /var/log/nginx/${APP}-error.log;
# set a custom header for requests
# add_header X-Served-By www-ec2-01;
location / {
proxy_pass http://$APP;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header X-Forwarded-Port \$server_port;
proxy_set_header X-Request-Start \$msec;
}
include $DOKKU_ROOT/$APP/nginx.conf.d/*.conf;
# Proxy download
location ~* ^/internal_redirect/(.*?)/(.*) {
# Do not allow people to mess with this location directly
# Only internal redirects are allowed
internal;
# Location-specific logging
access_log logs/internal_redirect.access.log main;
error_log logs/internal_redirect.error.log warn;
# Extract download url from the request
set $download_uri \$2;
set $download_host \$1;
# Compose download url
set $download_url http://\$download_host/\$download_uri;
# Set download request headers
proxy_set_header Host \$download_host;
proxy_set_header Authorization '';
# The next two lines could be used if your storage
# backend does not support Content-Disposition
# headers used to specify file name browsers use
# when save content to the disk
proxy_hide_header Content-Disposition;
add_header Content-Disposition 'attachment; filename="\$args"';
# Do not touch local disks when proxying
# content to clients
proxy_max_temp_file_size 0;
# Download the file and send it to client
proxy_pass \$download_url;
}
}
日志说它在函数之间切换,这很好,但是片段仍然可见,即使在使用.hide后
我引用了错误的东西吗?
答案 0 :(得分:5)
您可以使用以下方法getUserVisibleHint()
来确定您的片段对用户是否可见
答案 1 :(得分:1)
每次按下native_chat按钮时都会创建一个新片段,这个新片段在添加/显示之前永远不可见。尝试添加一个字段以保留此片段的引用,并确定已创建的字段的可见性。另外,我认为你想要一个if / else for action_settings按钮。
NativeFragment nativeFragment = null;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.native_chat) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (nativeFragment == null) {
// fragment doesn't exist, lets create and add one
nativeFragment = new NativeFragment();
getFragmentManager().executePendingTransactions();
fragmentTransaction.add(R.id.FragmentContainer, nativeFragment);
}
if(nativeFragment.isVisible()){
fragmentTransaction.hide(nativeFragment);
} else {
fragmentTransaction.show(nativeFragment);
}
fragmentTransaction.commit();
} else if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}