在我的应用程序中,我有两个片段Fragment Card,片段注释。我正在使用ViewPager和TabLayout在两个片段之间滑动。我想在我的应用程序中实现搜索功能。
这是我在Kotlin中的MainActivity类
class MainActivity : AppCompatActivity() {
private lateinit var viewPager:ViewPager
private lateinit var tabLayout:TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
//toolbar.title = "Cards"
initComponent()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val sv: SearchView = menu!!.findItem(R.id.action_bar_search).actionView as SearchView
val sm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
sv.setSearchableInfo(sm.getSearchableInfo(componentName))
sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item != null) {
when (item.itemId) {
R.id.sort -> {
Toast.makeText(context, "sort note", Toast.LENGTH_SHORT).show()
}
R.id.settings -> {
Toast.makeText(context, "settings note", Toast.LENGTH_SHORT).show()
}
}
}
return super.onOptionsItemSelected(item)
}
private fun initComponent() {
viewPager = findViewById(R.id.view_pager)
setupViewPager(viewPager)
tabLayout = findViewById(R.id.tab_layout)
tabLayout.setupWithViewPager(viewPager)
viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
changeFabIcon(position)
toolbar.title = tabLayout.getTabAt(position)!!.text.toString()
super.onPageSelected(position)
}
})
fabT.setOnClickListener {
//var text = ""
when (viewPager.currentItem) {
0 -> {
// open add new card page
startActivity(Intent(this, AddCardActivity::class.java))
//text = "Add Card"
}
1 -> {
// open add new note page
startActivity(Intent(this, AddNoteActivity::class.java))
//text = "Add note"
}
}
//Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
}
}
private fun changeFabIcon(index: Int) {
fabT.hide()
Handler().postDelayed({
when(index) {
0 -> {
fabT.setImageResource(R.drawable.ic_card)
}
1 -> {
fabT.setImageResource(R.drawable.ic_note)
}
}
fabT.show()
}, 400)
}
private fun setupViewPager(view_pager: ViewPager) {
val mAdapter = ViewPagerAdapter(supportFragmentManager)
mAdapter.addFragment(FragmentCard(), "Cards")
mAdapter.addFragment(FragmentNote(), "Notes")
view_pager.adapter = mAdapter
}
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)
//fragment.arguments = args
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
}
}
这是我在Kotlin中的FragmentNote类
class FragmentNote: Fragment() {
private var listNotes = ArrayList<Note> ()
private lateinit var mRecyclerViewNote: RecyclerView
private lateinit var mAdapterNote: RecyclerViewAdapterNote
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.note_fragment, container, false)
mAdapterNote = RecyclerViewAdapterNote(context!!, listNotes, this)
mRecyclerViewNote = v.findViewById(R.id.noteRecView) as RecyclerView
mRecyclerViewNote.layoutManager = LinearLayoutManager(context)
mRecyclerViewNote.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
mRecyclerViewNote.adapter = mAdapterNote
setHasOptionsMenu(true)
NoteLoadQuery("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun NoteLoadQuery(title: String) {
var dbManager = DbManagerNote(context!!)
val projections = arrayOf("ID", "Title", "Description")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "ID like ?", selectionArgs, "ID")
listNotes.clear()
// ascending
if (cursor.moveToLast()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val Title = cursor.getString(cursor.getColumnIndex("Title"))
val Description = cursor.getString(cursor.getColumnIndex("Description"))
listNotes.add(Note(ID, Title, Description))
} while (cursor.moveToPrevious())
}
mAdapterNote.notifyDataSetChanged()
}
}
最后这是我的FragmentCard类
class FragmentCard: Fragment() {
private var listCards = ArrayList<Card>()
private lateinit var myRecyclerViewCard: RecyclerView
private lateinit var mAdapter: RecyclerViewAdapterCard
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.card_fragment, container, false)
mAdapter = RecyclerViewAdapterCard(context!!,listCards, this)
myRecyclerViewCard = v.findViewById(R.id.cardRecView) as RecyclerView
myRecyclerViewCard.layoutManager = LinearLayoutManager(context)
myRecyclerViewCard.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
myRecyclerViewCard.adapter = mAdapter
setHasOptionsMenu(true)
CardLoadQueryAscending("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun CardLoadQueryAscending(title: String) {
var dbManager = DbManagerCard(context!!)
val projections = arrayOf("ID", "CardName", "CardNum")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "CardName like ?", selectionArgs, "CardName")
listCards.clear()
// ascending
if (cursor.moveToFirst()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val CardName = cursor.getString(cursor.getColumnIndex("CardName"))
val CardNum = cursor.getString(cursor.getColumnIndex("CardNum"))
listCards.add(Card(ID, CardName, CardNum))
} while (cursor.moveToNext())
}
mAdapter.notifyDataSetChanged()
}
}
我正在尝试在MainActivity中实现我的搜索视图,但无法执行。任何人都可以给我您的宝贵建议,以解决我的问题。提前致谢。
答案 0 :(得分:0)
我已经做到了,这很简单。 只需让您的MainActivity拥有SearchView。就像您已经做过的那样。
然后创建一个简单的界面
ISearchChangedListener
onTextChanged(newString:String?)
onSearchSubmitted()
设置您的BaseFragment来实现此接口。
在您的子片段中重写BaseFragment的实现
然后在PageChanged上跟踪mSelectedFragment。
然后您只需致电
mSelectedFragment?.onTextChanged(newString)
每当父母获得文本更改并适当处理时。