#android-recyclerview #navigation-drawer #browser-tab
Вопрос:
Я пытаюсь создать браузер с навигационным ящиком и вкладками для повторного просмотра для нового веб-сайта. Навигационный ящик выскакивает, но recyclerview не отображается.
вот мой код адаптера:
class NavigationTabAdapter (context: Context, contentData:ArrayList<Tab>) :
RecyclerView.Adapter<NavigationTabAdapter.NavigationViewHolder>() {
var mcontext:Context?=null
var contentList:ArrayList<Tab>?=null
var tabInterface:TabInterface?=null
init {
mcontext=context
contentList=contentData
tabInterface=context as TabInterface
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NavigationViewHolder {
val itemView=LayoutInflater.from(parent.context).inflate(R.layout.row_custom_recycler_tab, parent, true)
return NavigationViewHolder(itemView)
}
override fun onBindViewHolder(holder: NavigationViewHolder, position: Int) {
holder.row_text?.setText(contentList?.get(position)?.url)
holder.row_img_remove?.setOnClickListener {
if (contentList!!.size>1)
{
tabInterface?.deleteItem(contentList?.get(position)?.id!!,position)
}
}
вот мой основной xml-файл активности:
enter code //Navigation drawer
var drawerLayout:DrawerLayout?=null
var tabsDatabase:TabsDatabase?=null
var navigationRecyclerView:RecyclerView?=null
var navigationTabAdapter:NavigationTabAdapter?=null
var listWebLinks:ArrayList<Tab>?=null
var itemClickedPosition:Int=-1
var idForClickedPosition:Int=-1
var url:String?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar=findViewById<Toolbar>(R.id.appBar)
setSupportActionBar(toolbar)
drawerLayout=findViewById(R.id.drawer_layout)
val toggle=ActionBarDrawerToggle(this@MainActivity,drawerLayout,toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close)
drawerLayout?.addDrawerListener(toggle)
toggle.syncState()
tabsDatabase= TabsDatabase(this)
navigationRecyclerView=findViewById(R.id.nav_recycler_view)
navigationRecyclerView?.layoutManager=LinearLayoutManager(this)
navigationRecyclerView?.itemAnimator=DefaultItemAnimator()
listWebLinks= ArrayList()
searchView = findViewById(R.id.search_View)
webview = findViewById(R.id.webImage)
progressBar = findViewById(R.id.progressBar)
inputMethodManager=getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
mainScreen=findViewById(R.id.mainScreen)
webview!!.settings.javaScriptEnabled = true
webview!!.webViewClient = object :WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?,
url:String
): Boolean {
view!!.loadUrl(url)
return true
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
if (url.equals("about:blank"))
{
mainScreen?.visibility=View.VISIBLE
webview?.visibility
return
}
if (!(progressBar!!.isShown))
{
progressBar?.visibility = View.VISIBLE
view!!.visibility=View.GONE
}
searchView!!.onActionViewExpanded()
searchView!!.setQuery(webview!!.url,false)
searchView!!.clearFocus()
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (progressBar!!.isShown)
{
progressBar!!.visibility = View.GONE
view!!.visibility=View.VISIBLE
}
findViewById<ImageView>(R.id.backwardImage)!!.isEnabled=true
findViewById<ImageView>(R.id.backwardImage)!!.setImageResource(R.drawable.undo_blue)
if (!(webview!!.canGoForward()))
{
findViewById<ImageView>(R.id.forwardImage)?.isEnabled=false
findViewById<ImageView>(R.id.forwardImage)?.setImageResource(R.drawable.redo_blue)
}
findViewById<ImageView>(R.id.homeImage)?.isEnabled=true
findViewById<ImageView>(R.id.homeImage)?.setImageResource(R.drawable.home_orange)
if (itemClickedPosition!=-1)
{
updateItem(idForClickedPosition,url)
}
}
}
searchView?.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String): Boolean {
try {
val bool:Boolean=URLUtil.isValidUrl(query)
if (bool)
{
webview!!.loadUrl(query)
}
else
{
webview!!.loadUrl("https://" query.replace("",""))
findViewById<LinearLayout>(R.id.mainScreen).visibility = View.GONE
}
findViewById<ImageView>(R.id.backwardImage).isEnabled=true
findViewById<ImageView>(R.id.backwardImage).setImageResource(R.drawable.undo_orange)
findViewById<ImageView>(R.id.homeImage)?.isEnabled=true
findViewById<ImageView>(R.id.homeImage).setImageResource(R.drawable.home_orange)
inputMethodManager!!.hideSoftInputFromWindow(currentFocus?.windowToken,0)
}
catch (e:Exception)
{
Toast.makeText(this@MainActivity,"" e.printStackTrace(),Toast.LENGTH_SHORT).show()
}
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
try {
listWebLinks = tabsDatabase?.readData()
if (listWebLinks!!.size == 0)
{
tabsDatabase?.insertData("Home Page")
listWebLinks = tabsDatabase?.readData()
itemClickedPosition = 0
} else {
itemClickedPosition = listWebLinks!!.size - 1
}
navigationTabAdapter = NavigationTabAdapter(this, listWebLinks!!)
navigationRecyclerView?.adapter = navigationTabAdapter
navigationTabAdapter?.notifyDataSetChanged()
navigationRecyclerView?.setHasFixedSize(true)
} catch (e: Exception) {
e.printStackTrace()
}
файл базы данных
class TabsDatabase:SQLiteOpenHelper {
constructor(context: Context) : super(context, DB_NAME, null, DB_VERSION)
override fun onCreate(sqLiteDatabase: SQLiteDatabase?) {
sqLiteDatabase?.execSQL("CREATE TABLE" TABLE_NAME "(" COL_ID
" INTEGER PRIMARY KEY," COL_URL "TEXT);")
}
override fun onUpgrade(sqLiteDatabase: SQLiteDatabase?, p1: Int, p2: Int) {
sqLiteDatabase?.execSQL("Drop table IF EXISTS" TABLE_NAME)
}
fun insertData(url:String):Boolean?{
var result = true
try{
val db= this.writableDatabase
var cv=ContentValues()
cv.put(COL_URL,url)
db.insert(TABLE_NAME,null,cv)>0
db.close()
}catch (e:Exception) {
result = false
}
return result
}
fun updateData(id:Int,url:String):Boolean?{
var result = true
try{
val db= this.writableDatabase
var cv=ContentValues()
cv.put(COL_URL,url)
db.update(TABLE_NAME,cv, "id=?", arrayOf(id.toString())) >0
db.close()
}catch (e:Exception)
{
result= false
}
return true
}
fun readData():ArrayList<Tab>{
var list=ArrayList<Tab>()
var db=this.readableDatabase
val query="select * from" TABLE_NAME "ORDER BY id ASC"
val result=db.rawQuery(query,null)
if(result.moveToFirst())
{
do{
var tab = Tab()
tab.id=result.getInt(result.getColumnIndex(COL_ID).toInt())
tab.url=result.getString(result.getColumnIndex(COL_URL))
list.add(tab)
}while (result.moveToNext())
}
result.close()
db.close()
return list
}
fun deleteData(id:Int) {
val db=this.writableDatabase
db.delete(TABLE_NAME, COL_ID "=" id,null)
db.close()
}