我有一个android
应用程序,它是在Kotlin
中开发的,我的一项活动是使用Tab Layout(例如发送和接收标签)。我想在send标签中显示发送数据并在中接收数据接收标签,但是当我运行我的android
应用时,所有标签仅显示发送数据。如果我出错了,请指导我。
Main.kt
override fun onCreate(savedInstanceState: Bundle?) {
//val dateFormat = android.text.format.DateFormat.getDateFormat(applicationContext)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_in_drawer_container)
onboardingController.install()
settings.registerListener(this)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
drawer_layout.addDrawerListener(actionBarDrawerToggle)
receive_button.setOnClickListener {
onboardingController.dismiss()
startActivityFromClass(RequestActivity::class)
}
send_button.setOnClickListener {
startActivityFromClass(CreateTransactionActivity::class)
}
fab.setOnClickListener {
startScanActivityForResult(this)
}
transaction_recycler_out.layoutManager = LinearLayoutManager(this)
transaction_recycler_in.layoutManager = LinearLayoutManager(this)
current_fiat_symbol.setOnClickListener {
startActivityFromClass(SelectReferenceActivity::class)
}
current_token_symbol.setOnClickListener {
startActivityFromClass(SelectTokenActivity::class)
}
syncProgressProvider.observe(this, Observer {
val progress = it!!
if (progress.isSyncing) {
val percent = ((progress.currentBlock.toDouble() / progress.highestBlock) * 100).toInt()
supportActionBar?.subtitle = "Block ${progress.currentBlock}/${progress.highestBlock} ($percent%)"
}
})
val incomingTransactionsObserver = Observer<List<TransactionEntity>> {
Log.e("INDIA","incomingTransactionsObserver : "+it);
if (it != null) {
viewPager = viewpager as ViewPager
setupViewPager(viewPager!!,it)
tabLayout = tabs as TabLayout
tabLayout!!.setupWithViewPager(viewPager)
transaction_recycler_in.adapter = TransactionRecyclerAdapter(it, appDatabase, INCOMING, networkDefinitionProvider)
transaction_recycler_in.setVisibility(!it.isEmpty())
refresh()
}
}
val outgoingTransactionsObserver = Observer<List<TransactionEntity>> {
Log.e("INDIA","outgoingTransactionsObserver : "+it);
if (it != null) {
transaction_recycler_out.adapter = TransactionRecyclerAdapter(it, appDatabase, OUTGOING, networkDefinitionProvider)
refresh()
}
}
var incomingTransactionsForAddress: LiveData<List<TransactionEntity>>? = null
var outgoingTransactionsForAddress: LiveData<List<TransactionEntity>>? = null
fun installTransactionObservers() {
incomingTransactionsForAddress?.removeObserver(incomingTransactionsObserver)
outgoingTransactionsForAddress?.removeObserver(outgoingTransactionsObserver)
currentAddressProvider.value?.let { currentAddress ->
val currentChain = networkDefinitionProvider.getCurrent().chain
incomingTransactionsForAddress = appDatabase.transactions.getIncomingTransactionsForAddressOnChainOrdered(currentAddress, currentChain)
outgoingTransactionsForAddress = appDatabase.transactions.getOutgoingTransactionsForAddressOnChainOrdered(currentAddress, currentChain)
incomingTransactionsForAddress?.observe(this, incomingTransactionsObserver)
outgoingTransactionsForAddress?.observe(this, outgoingTransactionsObserver)
}
}
networkDefinitionProvider.observe(this, Observer {
setCurrentBalanceObservers()
installTransactionObservers()
})
currentAddressProvider.observe(this, Observer {
installTransactionObservers()
})
currentAddressProvider.observe(this, Observer { _ ->
setCurrentBalanceObservers()
})
if (intent.action?.equals("org.walleth.action.SCAN") == true) {
startScanActivityForResult(this)
}
if (savedInstanceState != null) {
lastPastedData = savedInstanceState.getString(KEY_LAST_PASTED_DATA)
}
//progressBar.visibility = View.VISIBLE
}
//tab
private fun setupViewPager(viewPager: ViewPager, it: List<TransactionEntity>?) {
val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(ReceiveFragment(it!!,appDatabase,INCOMING,networkDefinitionProvider), "RECEIVE")
adapter.addFragment(SendingFragment(it,appDatabase,OUTGOING,networkDefinitionProvider), "SEND")
viewPager.adapter = adapter
}
internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence {
return mFragmentTitleList[position]
}
}
适配器:
enum class TransactionAdapterDirection {
INCOMING, OUTGOING
}
class TransactionRecyclerAdapter(private val transactionList: List<TransactionEntity>,
val appDatabase: AppDatabase,
private val direction: TransactionAdapterDirection,
val networkDefinitionProvider: NetworkDefinitionProvider) : RecyclerView.Adapter<TransactionViewHolder>() {
override fun getItemCount() = transactionList.size
override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) = holder.bind(transactionList[position], appDatabase)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.transaction_item, parent,false)
return TransactionViewHolder(itemView,direction,networkDefinitionProvider)
}
}
ViewHolder.kt
class TransactionViewHolder(itemView: View, private val direction: TransactionAdapterDirection,val networkDefinitionProvider: NetworkDefinitionProvider) : RecyclerView.ViewHolder(itemView) {
fun bind(transactionWithState: TransactionEntity, appDatabase: AppDatabase) {
val transaction = transactionWithState.transaction
val relevantAddress = if (direction == TransactionAdapterDirection.INCOMING) {
transaction.from
} else {
transaction.to
}
if (transaction.isTokenTransfer()) {
if (direction == TransactionAdapterDirection.INCOMING) {
itemView.address.text=transaction.from.toString()
val tokenAddress = transaction.to
if (tokenAddress != null) {
{ appDatabase.tokens.forAddress(Address("0xdb3f0d7410736252bb9837940a494f5b94a58970")) }.asyncAwaitNonNull { token ->
itemView.difference.setValue(transaction.getTokenTransferValue(), token)
}
}
}
else
{
val tokenAddress = transaction.to
if (tokenAddress != null) {
{ appDatabase.tokens.forAddress(tokenAddress) }.asyncAwaitNonNull { token ->
itemView.difference.setValue(transaction.getTokenTransferValue(), token)
}
}
appDatabase.addressBook.resolveNameAsync(transaction.getTokenTransferTo()) {
itemView.address.text = it
}
}
} else {
itemView.difference.setValue(transaction.value, getEthTokenForChain(networkDefinitionProvider.getCurrent()))
relevantAddress?.let {
appDatabase.addressBook.resolveNameAsync(it) {
itemView.address.text = it
}
}
}
itemView.transaction_err.setVisibility(transactionWithState.transactionState.error != null)
if (transactionWithState.transactionState.error != null) {
itemView.transaction_err.text = transactionWithState.transactionState.error
}
val epochMillis = (transaction.creationEpochSecond ?: 0) * 1000L
val context = itemView.context
itemView.date.text = DateUtils.getRelativeDateTimeString(context, epochMillis,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
)
itemView.transaction_state_indicator.setImageResource(
when {
!transactionWithState.transactionState.isPending -> R.drawable.ic_lock_black_24dp
transactionWithState.signatureData == null -> R.drawable.ic_lock_open_black_24dp
else -> R.drawable.ic_lock_outline_24dp
}
)
itemView.isClickable = true
itemView.setOnClickListener {
if(transaction.isTokenTransfer()){
if (direction == TransactionAdapterDirection.INCOMING) {
val tokenaddress = "0xdb3f0d7410736252bb9837940a494f5b94a58970"
transactionWithState.hash.let {
context.startActivity(context.getTransactionActivityIntentForHash(it,tokenaddress))
//context.startActivity(context.getTransactionActivityIntentForHash(it))
}
}
else{
val tokenaddress = transaction.to.toString()
transactionWithState.hash.let {
context.startActivity(context.getTransactionActivityIntentForHash(it,tokenaddress))
//context.startActivity(context.getTransactionActivityIntentForHash(it))
}
}
}
else{
transactionWithState.hash.let {
context.startActivity(context.getTransactionActivityIntentForHash(it))
}}
}
}
}
fun <T> (() -> T).asyncAwait(resultCall: (T) -> Unit) {
async(UI) {
resultCall(async(CommonPool) {
invoke()
}.await())
}
}
fun <T> (() -> T?).asyncAwaitNonNull(resultCall: (T) -> Unit) {
async(UI) {
async(CommonPool) {
invoke()
}.await()?.let { resultCall(it) }
}
}
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ScrollView
android:id="@+id/empty_view_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</ScrollView>
<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:gravity="center"/>
<include
layout="@layout/receive_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<include
layout="@layout/send_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>