按钮的设置可见性在 android 中不起作用
setvisibility for button is not working in android
基本上这个想法是在点击时将地址添加到 activity 地址中,它会重定向到下一页,用户在提交时获取地址表单,它将获取地址并添加到上一个 activity回收视图。
场景:--
1-->当 recycleview 在地址活动中为空时,它显示空文本并且按钮不可见 ==>这里没有问题
2-->当 recycleview 在地址活动中不为空时,空文本消失但按钮不可见 ==>我看不到按钮的可见性 ==> 问题就在这里
这是我的代码 -->
class AddressActivity : AppCompatActivity(){
private lateinit var data: LiveData<MutableList<Address>>
private var data1:Int = 0
var adapter: AddressAdapter? =null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address)
var mActionBarToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbartable);
setSupportActionBar(mActionBarToolbar);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
getSupportActionBar()?.setDisplayShowHomeEnabled(true);
getSupportActionBar()?.setHomeAsUpIndicator(R.drawable.ic_keyboard_arrow_left_black_24dp);
//supportActionBar?.setTitle("Tables")
getSupportActionBar()?.setTitle((Html.fromHtml("<font color=\"#FFFFFF\">" + getString(
R.string.address) + "</font>")));
}
addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
IntentFilter("custom-message1")
)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
val adapter = AddressAdapter(applicationContext)
recyclerView.adapter = adapter
recyclerView.addItemDecoration(
DividerItemDecoration(
recyclerView.context,
DividerItemDecoration.VERTICAL
)
)
recyclerView.addOnScrollListener(object :
RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Log.e("RecyclerView", "onScrollStateChanged")
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
}
})
val application = application as CustomApplication
data = application.database.AddressDao().getAddressesWithChanges()
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }
})
if (!data.hasActiveObservers()) {
ordernow.setVisibility(View.GONE)
emptytext.setVisibility(View.VISIBLE)
} else {
ordernow.setVisibility(View.VISIBLE)
emptytext.setVisibility(View.GONE)
}
}
需要帮助谢谢
替换这个
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }
})
if (!data.hasActiveObservers()) {
ordernow.setVisibility(View.GONE)
emptytext.setVisibility(View.VISIBLE)
} else {
ordernow.setVisibility(View.VISIBLE)
emptytext.setVisibility(View.GONE)
}
至
data.observe(this, Observer { words1 ->
if (words1.size > 0) {
ordernow.setVisibility(View.VISIBLE) emptytext . setVisibility (View.GONE)
} else {
ordernow.setVisibility(View.GONE) emptytext . setVisibility (View.VISIBLE)
}
words1?.let { adapter.updateData(it) }
})
基本上这个想法是在点击时将地址添加到 activity 地址中,它会重定向到下一页,用户在提交时获取地址表单,它将获取地址并添加到上一个 activity回收视图。
场景:--
1-->当 recycleview 在地址活动中为空时,它显示空文本并且按钮不可见 ==>这里没有问题
2-->当 recycleview 在地址活动中不为空时,空文本消失但按钮不可见 ==>我看不到按钮的可见性 ==> 问题就在这里
这是我的代码 -->
class AddressActivity : AppCompatActivity(){
private lateinit var data: LiveData<MutableList<Address>>
private var data1:Int = 0
var adapter: AddressAdapter? =null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address)
var mActionBarToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbartable);
setSupportActionBar(mActionBarToolbar);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
getSupportActionBar()?.setDisplayShowHomeEnabled(true);
getSupportActionBar()?.setHomeAsUpIndicator(R.drawable.ic_keyboard_arrow_left_black_24dp);
//supportActionBar?.setTitle("Tables")
getSupportActionBar()?.setTitle((Html.fromHtml("<font color=\"#FFFFFF\">" + getString(
R.string.address) + "</font>")));
}
addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
IntentFilter("custom-message1")
)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
val adapter = AddressAdapter(applicationContext)
recyclerView.adapter = adapter
recyclerView.addItemDecoration(
DividerItemDecoration(
recyclerView.context,
DividerItemDecoration.VERTICAL
)
)
recyclerView.addOnScrollListener(object :
RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Log.e("RecyclerView", "onScrollStateChanged")
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
}
})
val application = application as CustomApplication
data = application.database.AddressDao().getAddressesWithChanges()
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }
})
if (!data.hasActiveObservers()) {
ordernow.setVisibility(View.GONE)
emptytext.setVisibility(View.VISIBLE)
} else {
ordernow.setVisibility(View.VISIBLE)
emptytext.setVisibility(View.GONE)
}
}
需要帮助谢谢
替换这个
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }
})
if (!data.hasActiveObservers()) {
ordernow.setVisibility(View.GONE)
emptytext.setVisibility(View.VISIBLE)
} else {
ordernow.setVisibility(View.VISIBLE)
emptytext.setVisibility(View.GONE)
}
至
data.observe(this, Observer { words1 ->
if (words1.size > 0) {
ordernow.setVisibility(View.VISIBLE) emptytext . setVisibility (View.GONE)
} else {
ordernow.setVisibility(View.GONE) emptytext . setVisibility (View.VISIBLE)
}
words1?.let { adapter.updateData(it) }
})