目前,我尝试将我的Activitis转换为片段。在这样做的同时,我遇到了多个问题。我的第一个也是主要的问题是,在SearchShop.kt
中,搜索texfield中没有键盘。
我的第二个问题是,当我尝试在PC上使用仿真器并使用PC键盘时,我可以输入值,但不能安排FirebaseJob。最后,我如何在Fragment中添加Fab Button的功能?
Grettings损失站 主要活动:
package com.name.shocal
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import kotlinx.android.synthetic.main.content_main.*
import java.sql.DatabaseMetaData
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
//AuthVariable for global use
private lateinit var myAuth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
val test = supportFragmentManager
val frag = LoginFragment()
test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
//Initializing FirebaseAuth Instance
myAuth = FirebaseAuth.getInstance()
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_addShop -> {
//Activity to Add your Shop
val addshopIntent = Intent(this,AddShopActivity::class.java)
startActivity(addshopIntent)
}
R.id.nav_searchShop -> {
//SearchActivity
val searchShopIntent = Intent(this,SearchShop::class.java)
startActivity(searchShopIntent)
}
R.id.nav_shopMap -> {
//MapActivity
val mapintent = Intent(this, ShopMap::class.java)
startActivity(mapintent)
}
R.id.nav_manage -> {
}
R.id.nav_share -> {
}
R.id.nav_send -> {
val test = supportFragmentManager
val frag = test()
test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
}
SearchShopFragment:
package com.name.shocal
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.content.LocalBroadcastManager
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.*
import android.widget.EditText
import com.firebase.jobdispatcher.FirebaseJobDispatcher
import com.firebase.jobdispatcher.GooglePlayDriver
import com.name.shocal.RecyclerAdapters.ShopSearchRecyclerAdapter
import com.name.shocal.Service.SearchShopFirebaseService
import kotlinx.android.synthetic.main.content_search_shop.*
import kotlinx.android.synthetic.main.app_bar_search_shop.*
class test : Fragment() {
//Adding Variable for Recyclerview
private var shop_list_adapter : RecyclerView.Adapter<ShopSearchRecyclerAdapter.ShopSearchViewHolder>? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.content_search_shop,container,false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//Integrating RecyclerView
val recyclerView = view?.findViewById<RecyclerView>(R.id.recyclerView_shop_search)
recyclerView?.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false)
//Broadcast Receiver
val myReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
var data = p1?.getStringArrayListExtra("SHOP_RESULTS")
Log.i("EXTRAS",data.toString())
var shop_result_list = data
Log.i("BROADCAST RECEIVER",shop_result_list.toString())
shop_list_adapter = ShopSearchRecyclerAdapter(activity!!.applicationContext,shop_result_list!!)
recyclerView?.adapter = shop_list_adapter
}
}
//Getting data via Broadcast Manager
LocalBroadcastManager.getInstance(activity!!.applicationContext).registerReceiver(myReceiver, IntentFilter("SearchDATA"))
//Get the ID of the Textfield
//val search_field = view?.findViewById<EditText>(R.id.search_textfield)
//Detect if changed happen in the text Field
search_textfield.addTextChangedListener(object:TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
Log.i("TEXT","TextChanged")
//Create an Dispatcher via GoogleService
val fbDispatcher = FirebaseJobDispatcher(GooglePlayDriver(activity!!.applicationContext))
//Bundle to pass data to Job
var myExtraBundle = Bundle()
myExtraBundle.putString("SEARCH_VALUE",search_textfield.text.toString())
//Create the job with the necessary attributes
var search_shop_job = fbDispatcher.newJobBuilder().setService(SearchShopFirebaseService::class.java).setTag("Search-Shop-Firebase").setExtras(myExtraBundle).build()
//Start FirebaseJob
Log.i("VALUES",search_textfield.text.toString())
fbDispatcher.mustSchedule(search_shop_job)
}
})
}
}