Center Search

This commit is contained in:
Dusk 2022-02-22 22:06:28 +01:00
parent b05857bedf
commit 7f8d8738f8
7 changed files with 68 additions and 59 deletions

View File

@ -3,7 +3,7 @@
package="com.example.testes"> package="com.example.testes">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -12,16 +12,22 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Testes"> android:theme="@style/Theme.Testes">
<activity
android:name=".ImageViewerActivity"
android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" /> <action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/> <meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity> </activity>
</application> </application>

View File

@ -1,40 +0,0 @@
package com.example.testes
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.getDrawable
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class ImageGridAdapter(val imageList: List<BooruImage>) :
RecyclerView.Adapter<ImageGridAdapter.ViewHolder>() {
lateinit var context: Context;
/* Creates and inflates view and return FlowerViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
context = parent.context;
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.image_grid_item, parent, false)
return ViewHolder(view)
}
/* Gets current flower and uses it to bind view. */
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Glide.with(context)
.load(imageList[position].preview_url)
.placeholder(getDrawable(context, R.drawable.ic_launcher_foreground))
.into(holder.imageview)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageview: ImageView = itemView.findViewById(R.id.image)
}
override fun getItemCount(): Int = imageList.size
}

View File

@ -1,12 +1,16 @@
package com.example.testes package com.example.testes
import android.content.Context
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class ImageGridFragment(val images: List<BooruImage>) : Fragment() { class ImageGridFragment(val images: List<BooruImage>) : Fragment() {
@ -27,4 +31,32 @@ class ImageGridFragment(val images: List<BooruImage>) : Fragment() {
return layoutView return layoutView
} }
inner class ImageGridAdapter(val imageList: List<BooruImage>) :
RecyclerView.Adapter<ImageGridAdapter.ViewHolder>() {
lateinit var context: Context;
/* Creates and inflates view and return FlowerViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
context = parent.context;
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.image_grid_item, parent, false)
return ViewHolder(view)
}
/* Gets current flower and uses it to bind view. */
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Glide.with(context)
.load(imageList[position].preview_url)
.placeholder(ContextCompat.getDrawable(context, R.drawable.ic_launcher_foreground))
.into(holder.imageview)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageview: ImageView = itemView.findViewById(R.id.image)
}
override fun getItemCount(): Int = imageList.size
}
} }

View File

@ -0,0 +1,11 @@
package com.example.testes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class ImageViewerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_viewer)
}
}

View File

@ -1,12 +1,9 @@
package com.example.testes package com.example.testes
import android.app.SearchManager
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.TextView
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import com.example.testes.databinding.ActivityMainBinding import com.example.testes.databinding.ActivityMainBinding
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
@ -39,7 +36,7 @@ class MainActivity : AppCompatActivity() {
search("") search("")
binding.search.setOnQueryTextListener(object : binding.search.setOnQueryTextListener(object :
android.widget.SearchView.OnQueryTextListener { SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String): Boolean { override fun onQueryTextChange(newText: String): Boolean {
return false return false
} }

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImageViewerActivity">
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -7,26 +7,20 @@
android:orientation="vertical" android:orientation="vertical"
tools:context=".MainActivity"> tools:context=".MainActivity">
<Toolbar <androidx.appcompat.widget.Toolbar
android:id="@+id/mainToolbar" android:id="@+id/mainToolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@color/design_default_color_primary" android:background="@color/design_default_color_primary"
> app:contentInsetStart="0dp">
<androidx.cardview.widget.CardView <androidx.cardview.widget.CardView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="7.5dp" android:layout_margin="7.5dp"
app:cardCornerRadius="7.5dp" app:cardCornerRadius="7.5dp">
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="8dp">
<androidx.appcompat.widget.SearchView
<SearchView
android:id="@+id/search" android:id="@+id/search"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -38,7 +32,7 @@
</androidx.cardview.widget.CardView> </androidx.cardview.widget.CardView>
</Toolbar> </androidx.appcompat.widget.Toolbar>
<FrameLayout <FrameLayout
android:id="@+id/imageGridRecV" android:id="@+id/imageGridRecV"