My RecyclerView loads in the middle of the list instead of the most top item first. The list should load the topmost item. In this case, it's the "latest" post. Which I have set to the reverse layout to true and that works.
My RecyclerView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:viewBindingIgnore="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/postsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="5dp"
tools:listitem="#layout/post_item" />
</FrameLayout>
My Post list fragment
// Set up FirebaseRecyclerAdapter with the Query
val postsQuery = database.child(Globals().POSTS_ROOT_PATH)
// This configuration comes from the Paging Support Library
// https://developer.android.com/reference/androidx/paging/PagedList.Config
val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(5)
.setPageSize(10)
.build()
// The options for the adapter combine the paging configuration with query information
// and application-specific options for lifecycle, etc.
val options = DatabasePagingOptions.Builder<Post>()
.setLifecycleOwner(this)
.setQuery(postsQuery, config, Post::class.java)
.build()
adapter = object : FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): PostViewHolder {
val inflater = LayoutInflater.from(viewGroup.context)
return PostViewHolder(inflater.inflate(R.layout.post_item, viewGroup, false))
}
override fun onBindViewHolder(viewHolder: PostViewHolder, position: Int, model: Post) {
val postRef = getRef(position)
// Set click listener for the whole post view
val postKey = postRef.key
// Log.d("PostListFragment", "post key id is: " + postKey)
viewHolder.itemView.setOnClickListener {
// Launch PostDetailActivity
val intent = Intent(activity, PostDetailActivity::class.java)
intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey)
startActivity(intent)
}
}
override fun onLoadingStateChanged(state: LoadingState) {
when (state) {
LoadingState.LOADING_INITIAL -> {
Log.d(TAG, "Initial data loading")
}
LoadingState.LOADING_MORE -> {
}
LoadingState.LOADED -> {
}
LoadingState.FINISHED -> {
Log.d(TAG, "All data loaded.")
}
LoadingState.ERROR -> {
Log.d(TAG, "Error occured while loading data")
}
}
}
override fun onError(databaseError: DatabaseError) {
super.onError(databaseError)
databaseError.toException().printStackTrace()
}
}
// Set up Layout Manager, reverse layout
manager = LinearLayoutManager(activity)
manager.reverseLayout = true
manager.stackFromEnd = true
recyclerList.layoutManager = manager
recyclerList.adapter = adapter
}
When I set the setPageSize() to a very large number then it loads the most top item. What do I need to do so this always loads the most top list item first?
Thanks
Edit:
post_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
tools:viewBindingIgnore="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/postTextLayout"
layout="#layout/post_text_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginStart="2dp" />
<include
android:id="#+id/postThumbnailImg"
layout="#layout/post_video_thumbnail_include"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/postTextLayout"
android:layout_alignParentStart="true" />
<include
android:id="#+id/postAuthorLayout"
layout="#layout/post_author_thumbnail_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/postThumbnailImg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Sub layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/postTitle"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textStyle="bold"
android:textColor="#color/colorPrimaryDark"
tools:text="My First Post"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:viewBindingIgnore="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/videoThumbnailImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/main_background" />
/>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="#+id/playButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_baseline_play_arrow_24" />
</RelativeLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="40dp"
android:layout_height="40dp"
app:cardCornerRadius="20dp">
<ImageView
android:id="#+id/postAuthorPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/ic_person_outline_40" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/postAuthor"
style="#style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textColor="#color/colorPrimaryDark"
tools:text="someauthor#email.com" />
</LinearLayout>
You need to set height wrap_content rather than match_parent for sublayouts.
Related
I have Recyclerview multiple view types and have Recyclerview in Recyclerview when scroll lag first time I do not know why scroll lag first time
here my layout fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.LottoFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/teal_700"
android:padding="20dp">
<LinearLayout
android:id="#+id/searchLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/menuTextInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="56dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_list_header_background"
android:clickable="true"
android:elevation="4dp"
android:orientation="horizontal"
android:padding="20dp">
<TextView
android:id="#+id/lotto_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/kanit_light"
android:text="0 items(s)" />
<ImageView
android:id="#+id/filterIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:src="#android:drawable/arrow_up_float"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/front_layer_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/lotto_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="64dp"
tools:itemCount="3"
tools:listitem="#layout/item_lotto_prizes_one" />
</LinearLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
here my fragment
lottoRecyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = lottoLatestAdapter
}
here my Adapter:
class LottoLatestAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
private const val VIEW_TYPE_LOTTO_PRIZE_ONE = 1
private const val VIEW_TYPE_LOTTO_PRIZE_RUNNING_NUMBER = 2
private const val VIEW_TYPE_LOTTO_PRIZE_ONE_NEAR = 3
private const val VIEW_TYPE_LOTTO_PRIZE_OTHER = 4
}
var latestList: MutableList<LottoPrizesLatestModel> = mutableListOf()
set(value) {
field = value
notifyDataSetChanged()
}
private val viewPool : RecycledViewPool = RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
VIEW_TYPE_LOTTO_PRIZE_ONE -> {
LottoPrizeOneHolder(ItemLottoPrizesOneBinding.inflate(inflater, parent, false))
}
VIEW_TYPE_LOTTO_PRIZE_RUNNING_NUMBER -> {
LottoPrizeRunningNumberHolder(
ItemLottoRunningNumbersBinding.inflate(
inflater,
parent,
false
)
)
}
VIEW_TYPE_LOTTO_PRIZE_ONE_NEAR -> {
LottoPrizeOneNearHolder(
ItemLottoPrizesOneNearBinding.inflate(
inflater,
parent,
false
)
)
}
VIEW_TYPE_LOTTO_PRIZE_OTHER -> {
LottoPrizeOtherListHolder(
ItemLottoPrizesOtherListBinding.inflate(
inflater,
parent,
false
),
LottoLatestPrizeOtherAdapter()
)
}
else -> throw NullPointerException("Not have view Type")
}
}
override fun getItemViewType(position: Int): Int {
return when (latestList[position]) {
is LottoPrizesLatestModel.LottoLatestPrizeOne -> VIEW_TYPE_LOTTO_PRIZE_ONE
is LottoPrizesLatestModel.LottoLatestRunningNumbers -> VIEW_TYPE_LOTTO_PRIZE_RUNNING_NUMBER
is LottoPrizesLatestModel.LottoLatestPrizeFirstNear -> VIEW_TYPE_LOTTO_PRIZE_ONE_NEAR
is LottoPrizesLatestModel.LottoLatestPrizeOther -> VIEW_TYPE_LOTTO_PRIZE_OTHER
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is LottoPrizeOneHolder -> holder.bind(latestList[position] as LottoPrizesLatestModel.LottoLatestPrizeOne)
is LottoPrizeRunningNumberHolder -> holder.bind(latestList[position] as LottoPrizesLatestModel.LottoLatestRunningNumbers)
is LottoPrizeOneNearHolder -> holder.bind(latestList[position] as LottoPrizesLatestModel.LottoLatestPrizeFirstNear)
is LottoPrizeOtherListHolder -> {
viewPool.putRecycledView(holder)
holder.bind(latestList[position] as LottoPrizesLatestModel.LottoLatestPrizeOther, viewPool)
}
}
}
override fun getItemCount(): Int = latestList.size
}
here my layout holder
<?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="wrap_content"
android:layout_margin="8dp">
<TextView
android:id="#+id/title_prize_other_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/kanit_light"
android:gravity="center"
android:textSize="26sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/prize_other_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_prize_other_text_view"
tools:itemCount="10"
tools:listitem="#layout/item_lotto_prizes_other" />
<GridLayout
android:id="#+id/prize_other_grip_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_prize_other_text_view" />
<TextView
android:id="#+id/text_prize_one_near_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/kanit_light"
android:text="#string/lotto_prize_per_baht"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/prize_other_recycler_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here my holder recyclerView in RecyclerView
class LottoPrizeOtherListHolder(
private val binding: ItemLottoPrizesOtherListBinding,
private val lottoLatestPrizeOtherAdapter: LottoLatestPrizeOtherAdapter
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(
lottoLatestPrizeOther: LottoPrizesLatestModel.LottoLatestPrizeOther,
viewPool: RecyclerView.RecycledViewPool
) = with(binding) {
prizeOtherRecyclerView.apply {
layoutManager = GridLayoutManager(
context,
2,
GridLayoutManager.VERTICAL,
false
).apply {
initialPrefetchItemCount = lottoLatestPrizeOther.prizeModel.number.size
}
this.adapter = lottoLatestPrizeOtherAdapter
setRecycledViewPool(viewPool)
}
lottoLatestPrizeOtherAdapter.numberList =
lottoLatestPrizeOther.prizeModel.number.toMutableList()
titlePrizeOtherTextView.text = lottoLatestPrizeOther.prizeModel.name
textPrizeOneNearTextView.text = itemView.context.getString(
R.string.lotto_prize_per_baht,
lottoLatestPrizeOther.prizeModel.reward
)
}
}
my problem is : When i scroll recyclerview for first time it is not smooth and logcat show recyclerview Skipped 51 frames! adapter but when I reach end of recyclerView and scroll to up then scroll down it is very smooth and my recyclerview have TextView Only !!! how can I solve this issue?
this my video App https://youtu.be/Li7aKWmEfXQ
In your fragment add this after applying LayoutManager:
lottoRecyclerView.setDrawingCacheEnabled(true);
lottoRecyclerView.setItemViewCacheSize(myCacheSize);
The main reason Jank occurs the first time because on the first time it is loading the values onto memory dynamically. while once it is down it already has a few elements pre loaded
You can handle it using
mRecyclerView.setHasFixedSize(true);
mRecyclerview.setNestedScrollingEnabled(false);
in your kotlin code
or add android:nestedScrollingEnabled="false" in your RecyclerView xml
You can reach some performance improvements by applying the following:
recyclerView.setHasFixedSize(true);
recyclerView.setDrawingCacheEnabled(true);
..in combination with lite mode of MapsView:
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="200dp"
app:liteMode="true"
app:mapType="normal" />
For it was not an option to disable nestedScrollingEnabled as mentioned by #Narendra_Nath, which further improves performance.
I have a navigation header as bellow
I want to update TextView in nav_header.xml from MainActivity.kt by Kotlin
I want to update text of TextView(id=drawer_user_name) from MainActivity.kt
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45dp"
android:background="#android:drawable/screen_background_light_transparent"
tools:listitem="#layout/item_list" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#4cAF50"
android:orientation="vertical"
android:layout_gravity="top"
android:paddingTop="35dp"
android:paddingLeft="15dp"
android:paddingBottom="15dp"
android:id="#+id/ddd">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:layout_marginBottom="15dp"/>
<TextView
android:id="#+id/drawer_user_name" //--> i want to update this TextView from MainActivity.kt
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this our navigation drawer"
android:textColor="#FFFFFF" />
</LinearLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/colorPrimary"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="#+id/home_page_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat"
android:textColor="#FFFFFF" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is my content"/>
</LinearLayout>
</LinearLayout>
Please Help me,I really need a way to solve this problem
thank you
Babbab forces us to write stories here to allow us to post a question(It looks like your post is mostly code; please add some more details)
Edit
my MainActivity.kt
package com.example.myapplication
...
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main_page.*
import kotlinx.android.synthetic.main.nav_header.view.*
class MainPageActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener,CustomAdapter.OnItemClickListener {
lateinit var toolbar:androidx.appcompat.widget.Toolbar
lateinit var drawerLayout: DrawerLayout
lateinit var navView:NavigationView
private var data = arrayListOf<UserChanel>()
private lateinit var myadapter:CustomAdapter
private val gson = Gson()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar =findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
// val userNameTextView = nav_view.findViewById<TextView>(R.id.drawer_user_name)
// userNameTextView.text = "SOME TEXT" // error
nav_view.drawer_user_name.text = "SOME TEXT" // Error
val recyclerview = findViewById<RecyclerView>(R.id.recycler_view)
// for insert line divider to recyclerview items
recyclerview.addItemDecoration(SimpleDividerItemDecoration(this)) // for insert line divider to recyclerview items
send_request()
val adapter = CustomAdapter(this,data,this)
recyclerview.layoutManager = LinearLayoutManager(this)
recyclerview.adapter = adapter
recyclerview.setHasFixedSize(true)
myadapter = adapter
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.nav_profile->{
Toast.makeText(this,"profile clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_message->{
Toast.makeText(this,"message clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_frinds->{
Toast.makeText(this,"frinds clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_update->{
Toast.makeText(this,"update clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_logout->{
Toast.makeText(this,"logout clicked",Toast.LENGTH_SHORT).show()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun send_request(){
...
}
override fun onItemClick(position: Int,adapter: CustomAdapter,v:View) {
...
}
}
You can find the target view from its parent in your activity like either of these ways:
nav_view.getHeaderView(0).findViewById<TextView>(R.id.drawer_user_name).text = "SOME TEXT"
or
import kotlinx.android.synthetic.main.nav_header.view.*
...
nav_view.getHeaderView(0).drawer_user_name.text = "SOME TEXT"
It's possible to access the header view using getheaderview(int index) according to the documentation. As you have one header view in the NavigationView, it is accessible by index 0.
I have used #bindingAdapter to set Avatar in my layout but when change some value the bindingAdapter is not loading the image. This is my viwmodel
class ProfileViewModel constructor(val repository: ProfileRepository, val mContext: Context, photo: Photo = Photo("")): ViewModel() {
var avatar: ObservableField<String> = ObservableField("")
var responseListener: ResponseListener? = null
var fileUploadEvent: FileEvents? = null
fun getAvatarUrl() : ObservableField<String> {
return avatar
}
companion object {
#BindingAdapter("avatarUrl")
#JvmStatic
fun loadAvatar(imageView: ImageView, avatarUrl: String) {
Glide.with(imageView.context)
.load(avatarUrl)
.placeholder(R.mipmap.profile)
.apply(RequestOptions.circleCropTransform())
.into(imageView)
}
}
fun onProfileImageUpload(uri: Uri) {
fileUploadEvent!!.onUpload()
Coroutines.main {
try {
val imageResponse = repository.updateProfileAvatar(
getImageForUpload(uri, "avatar")
)
avatar.set(imageResponse.data.avatar.thumbnail)
fileUploadEvent!!.onUploadSuccess(avatar.get()!!)
} catch(e : Exception) {
fileUploadEvent!!.onUploadFailure(e.toString())
}
}
}
}
In my XML I defined the namespace like this
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.kreeti.gogal.ui.profile.ProfileViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/profile_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/welcome_header"
style="#style/glacial_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textColor="#color/violet"
android:textSize="32sp" />
<LinearLayout
android:id="#+id/profile_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
>
<ImageView
android:id="#+id/avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:onClick="#{viewmodel::onAvatarClick}"
app:avatarUrl="#{viewmodel.avatarUrl}" />
<ProgressBar
android:id="#+id/avatar_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
I am getting photo url from api, I want change the photo when I get the phot through the viewbinding. How can I load the image when I am changing the url? Do I have to make some listener?
I have some text field and a recyclerview in my activity. I need to access textfield values from the recycler view adapter class and access a method in activity from the adapter class. but I couldn't do the same using Kotlin help me to solve this
Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_addstaff)
try {
sprefs = this!!.getSharedPreferences(
Config.PACKAGE_NAME, Context.MODE_PRIVATE)
token = sprefs!!.getString("token", "No")!!
} catch (e: Exception) {
}
try {
layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
outletnameslistrecycle!!.layoutManager = layoutManager
Outnameslist = ArrayList()
adapter = outletname_adapter(Outnameslist as ArrayList<Outletnamelist>, this,btneditstaff as Button)
outletnameslistrecycle!!.adapter = adapter
getoutletlist();
} catch (e: Exception) {
e.printStackTrace()
}
}
Adapterclass
class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
try {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.lay_outletnamelist, parent, false)
return ViewHolder(v, context, Outnamelist)
} catch (e: Exception) {
e.printStackTrace()
}
return null!!
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
try {
button.setOnClickListener {
}
var OutletList =Outnamelist[position]
holder.outlet_name!!.text=OutletList.name
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun getItemCount(): Int {
return Outnamelist.size
}
inner class ViewHolder(v: View, var context: Context, private val outnamelist: List<Outletnamelist>)
: RecyclerView.ViewHolder(v), View.OnClickListener {
var outlet_checkbox: CheckBox? = null
var outlet_name: TextView?=null
var layTop: LinearLayout?= null
init {
itemView.setOnClickListener(this)
try {
outlet_checkbox=v.findViewById<View>(R.id.checkBox) as CheckBox
outlet_name=v.findViewById<View>(R.id.txt_in)as TextView
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onClick(v: View) {
val position = adapterPosition
}
}
}
XML`
<ScrollView 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"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/diagonal_bg"
tools:context=".Activity_addstaff">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/img_staff"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="30dp"
android:layout_marginTop="137dp"
android:src="#drawable/subadmin"
app:civ_border_color="#ffffff"
app:civ_border_width="2dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:layout_marginTop="250dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffname_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffname_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_staffname" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffaddress_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffaddress_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_address" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffplace_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffplace_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_place" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffpin_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffpin_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_pin" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffmob_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffmob_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_mob"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/lay_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/lay_root2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/outletnameslistrecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="20dp">
<Button
android:id="#+id/btneditstaff"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
style="#style/DefaultButton"
android:background="#drawable/btn_border"
android:foreground="?attr/selectableItemBackground"
android:text="Save"
android:textAllCaps="false"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
From the activity, I have passed my button reference to adapter class but the same way I am getting an error when trying to pass textfield references.
you can access the activity method or others on this way in adapter:
if(context is XXXActivity){
context.xxx()
context.xx
}
but this isn't the good code.you can try like this 2 methods:
you can define and set a Listener to adapter to post the event(such as click?) to activity,
make the activity deal with it initiative(activity can get the data from itself and adapter)。
if not an event make the adapter need the data from activity.you can provide the data from activity with interface like this.
class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button , val xxxtextProvider:()->String={""}/*it return the data you need*/ ) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {
...
}
I have this layout and am trying to switch on a corresponding LL based on a position. However the first Linear Layout will only show up and be drawn if I give the text view inside an absolute dimension such as 200dp for the width, I have tried 0dp with constraints as well.
<?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:id="#+id/draft_rider_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="lost"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/section"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone">
<TextView
android:id="#+id/fake"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
tools:text="Andrea Dovizioso's"></TextView>
</LinearLayout>
<LinearLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone">
<TextView
android:id="#+id/rider_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
tools:text="Andrea Dovizioso's"></TextView>
</LinearLayout>
This layout is rendered from a recycler view that is on my fragment which is placed like this :
<?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=".roster.RosterFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/player_rider_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:src="#drawable/baseline_edit_white_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:rippleColor="#color/colorPrimary" />
And here is my adapter :
class PlayerRidersAdapter : RecyclerView.Adapter<PlayerRidersAdapter.ViewHolder>() {
var riderList = ArrayList<Rider>()
override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.rider_item_layout, null, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return riderList.count() + 2
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
when(position) {
0 -> showHeader(holder)
3 -> showSection(holder)
else -> showRow(holder)
}
}
fun showHeader(holder: ViewHolder) {
holder.headerContainer.visibility = View.VISIBLE
holder.sectionContainer.visibility = View.GONE
holder.rowContainer.visibility = View.GONE
}
fun showRow(holder: ViewHolder) {
holder.headerContainer.visibility = View.GONE
holder.sectionContainer.visibility = View.GONE
holder.rowContainer.visibility = View.VISIBLE
//holder.name.text = rider.name
}
fun showSection(holder: ViewHolder) {
holder.headerContainer.visibility = View.GONE
holder.sectionContainer.visibility = View.VISIBLE
holder.rowContainer.visibility = View.GONE
}
fun addRiders(riders : ArrayList<Rider>){
riderList.addAll(riders)
notifyDataSetChanged()
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var headerContainer = view.findViewById<LinearLayout>(R.id.header)
var sectionContainer = view.findViewById<LinearLayout>(R.id.section)
var rowContainer = view.findViewById<LinearLayout>(R.id.row)
var name = view.findViewById<TextView>(R.id.rider_name)
}
}
In your adapter, specify the following in onCreateViewHolder():
val view = LayoutInflater.from(parent.context).inflate(R.layout.rider_item_layout, parent, false)
Specifying the root argument will let the inflated layout be sized properly. This is most likely your problem.
However, match_parent sometimes has strange behavior when used in ConstraintLayout, so use 0dp instead and remember to constrain the effected sides. It would be nice for ConstraintLayout to either handle this case, report it as an error or, at least, support a Lint check.
An aside: The constraints on the TextViews that are held within a LinearLayout have no effect. Constraints only apply to direct children of ConstraintLayout.