How can I reduce spacing between items in RecyclerView? - android

I have a simple question , I have a RecyclerView with 10 items, an Horizontal Scroll with paging Activated and my items appears in the middle like this :
What do I want now it's to have this. As you can see the next item is visible on the screen :
Here is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ImageView
android:id="#+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="#drawable/square_elem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
My MainActivity :
class MainActivity : AppCompatActivity() {
{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerInit()
}
fun recyclerInit()
{
val list : ArrayList<String> = ArrayList()
for (i in 1..10) {
list.add("$i")
}
recyclerview.layoutManager = LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false)
recyclerview.setHasFixedSize(true)
recyclerview.adapter = RecyclerAdapter(list)
val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(recyclerview)
}
}
And This is my Adapter :
class RecyclerAdapter(val list: ArrayList<String>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate((R.layout.box_elem), parent, false)
return ViewHolder(view)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemViewType(position: Int): Int {
return position
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.image.setImageResource(R.drawable.original_box)
holder.itemView.setOnClickListener(View.OnClickListener {
removeElemAt(position);
})
}
fun removeElemAt(position: Int) {
list.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, list.size)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
var image: ImageView = itemView.findViewById(R.id.box_elem)
}
}

Try this code
RecyclerView xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
YourItem xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl"
android:layout_width="YourOption"
android:layout_height="YourOption">
<ImageView
android:id="#+id/img"
android:layout_width="YourOption"
android:layout_height="YourOption"
android:gravity="YourOption"
android:scaleType="YourOption"
/>
</RelativeLayout>

change your layout with this code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:src="#drawable/square_elem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Related

How to set adapter for recyclerview with Databinding

I want to use databinding for recyclerview. I created a model with BindignAdapter annotation. But after that I don't know what should I do and I didn't find any solution.
I want to set some data for recyclerview adapter and set the adapter as recyclerview adapter and show data in the recyclerview.
This is my MainActivity XML file(This line has an error:android:recyclerUser="#{dog.recyclerViewBinder()}"):
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="dog"
type="com.hooman.databinding.DogModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:recyclerUser="#{dog.recyclerViewBinder()}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
This is my model class:
class DogModel(var dogName:String, var dogImage:String?, var dogBreed:String,var context: Context){
#BindingAdapter("android:loadImage")
fun loadImage(imageView: ImageView,dogImage:String?){
Picasso.get().load(dogImage).into(imageView)
}
#BindingAdapter("android:recyclerUser")
fun recyclerViewBinder(recyclerView: RecyclerView,dogList: MutableList<DogModel>){
var dogAdapter = DogAdapter(dogList)
recyclerView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
recyclerView.adapter = dogAdapter
}
}
This is recyclerview's adapter:
class DogAdapter(var dogList:List<DogModel>): RecyclerView.Adapter<DogAdapter.DogViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DogViewHolder {
val view = DataBindingUtil.inflate<RvDogItemBinding>(LayoutInflater.from(parent.context),R.layout.rv_dog_item,parent,false)
return DogViewHolder(view)
}
override fun onBindViewHolder(holder: DogViewHolder, position: Int) {
holder.view.dog = dogList[position]
holder.view.executePendingBindings()
}
override fun getItemCount(): Int {
return dogList.size
}
inner class DogViewHolder(var view: RvDogItemBinding) : RecyclerView.ViewHolder(view.root)
}
This is rv_dog_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="dog"
type="com.hooman.databinding.DogModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:cardCornerRadius="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/img_dog"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:loadImage="#{dog.dogImage}"/>
<TextView
android:id="#+id/txt_dogName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/img_dog"
app:layout_constraintEnd_toStartOf="#id/img_dog"
android:textSize="22sp"
android:padding="10dp"
android:textColor="#color/black"
android:text="#{dog.dogName}"/>
<TextView
android:id="#+id/txt_dogBreed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/txt_dogName"
app:layout_constraintStart_toStartOf="#id/txt_dogName"
app:layout_constraintEnd_toEndOf="#id/txt_dogName"
android:layout_marginTop="32dp"
android:text="#{dog.dogBreed}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
And this is mainActivity class:
class MainActivity : AppCompatActivity() {
lateinit var binding:ActivityMainBinding
lateinit var adapter:DogAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
var model = DogModel("Foo Dog",null,"Husky",this)
binding.dog = model
}
}
So what I should I do next and what did I make a mistake?

MyAdapter with setOnClickListener in Activity does work but in Fragment does not work

my Adapter with setOnClickListener in Activity does work but in my Fragment does not work.
I have two Adapter with setOnClickListener,both do work in Activity on RecyclerView but None of them don't work in my Fragment's RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myToolbar.title = "My Test"
setSupportActionBar(myToolbar)
var myToggle = object :
ActionBarDrawerToggle(this, myDrawerLayout, myToolbar,
R.string.open, R.string.close) {
override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
invalidateOptionsMenu()
}
override fun onDrawerClosed(drawerView: View) {
super.onDrawerClosed(drawerView)
invalidateOptionsMenu()
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
super.onDrawerSlide(drawerView, slideOffset)
if (slideOffset < 0.6) {
myToolbar.alpha = 1 - slideOffset
}
}
}
myDrawerLayout.addDrawerListener(myToggle)
myDrawerLayout.post {
myToggle.syncState()
}
var myLinearLayoutManager = LinearLayoutManager(this)
myLinearLayoutManager.orientation = LinearLayoutManager.VERTICAL
var myAdapter = MyAdapter(this, MyDataClass.DataProducer.MyList)
myRecyclerView.adapter = myAdapter
myRecyclerView.layoutManager = myLinearLayoutManager
/////////////////////////////////////////////////////////////////////
supportFragmentManager
.beginTransaction()
.replace(R.id.myFrameLayoutMain, MainFragment())
.commit()
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:context=".MainActivity"
android:background="#0068FF"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myToolbar"
android:background="#EF30"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myFrameLayoutMain"
android:background="#61C224"
android:layout_below="#id/myToolbar">
</FrameLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myDrawerLayout"
tools:openDrawer="start"
android:fitsSystemWindows="false"
android:layout_below="#id/myToolbar">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myNavigationView"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FE7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text in NavigationView"
android:textSize="25sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EC67">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myRecyclerView"
android:layout_gravity="center"
android:background="#000000"/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container:
ViewGroup?, savedInstanceState: Bundle?): View? {
var view: View = inflater.inflate(R.layout.fragment_main, container,
false)
var myRe = view.findViewById<RecyclerView>(R.id.myRecyclerViewMain)
var myLinearLayoutManager = LinearLayoutManager(activity)
myLinearLayoutManager.orientation = LinearLayoutManager.VERTICAL
var myAdapter = MyAdapterMain(activity,
MyDataClass.DataProducer.MyList)
myRe.adapter = myAdapter
myRe.layoutManager = myLinearLayoutManager
return view
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myRecyclerViewMain"
android:layout_gravity="center"
android:background="#9C27B0"
android:layout_marginTop="0dp"/>
</FrameLayout>
class MyAdapterMain(var context: Context?, var list: List<MyDataClass>) :
RecyclerView.Adapter<MyAdapterMain.MyViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val view: View =
LayoutInflater.from(context).inflate(R.layout.list_pattern_main, p0, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
p0.myTextHolder.text = MyDataClass.DataProducer.MyList[p1].data
p0.myCircleImgView.setImageResource(R.mipmap.arezo9696)
var font = Typeface.createFromAsset(context?.assets, "BYekan.ttf")
p0.myTextHolder.typeface = font
}
inner class MyViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var myTextHolder = itemView.txtViewPattern!!
var myCircleImgView = itemView.myCircleImageView!!
init {
Log.d("BeForinit MyAdapterMain", "Mahmoud Shokri Test")
myCircleImgView.setOnClickListener {
Toast.makeText(context, "Klicked
Position\n\t\t\t\t$adapterPosition", Toast.LENGTH_SHORT).show()
Log.d("Afterinit MyAdapterMain", "Mahmoud Shokri Test")
}
}
}
}
#Suppress("DEPRECATION")
class MyAdapter(var context: Context?, var list: List<MyDataClass>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val view =
LayoutInflater.from(context).inflate(R.layout.list_pattern_nav, p0, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
p0.myTextHolder.text = MyDataClass.DataProducer.MyList[p1].data
p0.myImgHolder.setImageResource(R.drawable.abc_ic_star_black_36dp)
var font = Typeface.createFromAsset(context?.assets, "BYekan.ttf")
p0.myTextHolder.typeface = font
}
inner class MyViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var myTextHolder = itemView.txtViewPattern!!
var myImgHolder = itemView.imgViewPattern!!
var myRipple = itemView.myRippleView!!
init {
Log.d("BeForinit MyAdapterMain", "Mahmoud Shokri Test")
myRipple.setOnRippleCompleteListener {
Toast.makeText(context, "Klicked
Position\n\t\t\t\t$adapterPosition", Toast.LENGTH_SHORT).show()
Log.d("Afterinit MyAdapterMain", "Mahmoud Shokri Test")
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="0dp"
android:layout_marginTop="1dp"
android:gravity="center"
app:cardCornerRadius="10dp"
android:background="#EF7">
<FrameLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/myCircleImageView"
android:src="#mipmap/ic_launcher_round"
android:layout_width="96dp"
android:layout_height="96dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_gravity="end|center_vertical"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtViewPattern"
android:text="#string/my_test_is_this"
android:textSize="30sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="5dp"/>
</FrameLayout>
</android.support.v7.widget.CardView>
For Fragment, init with setOnClickListener(in AdapterMain) is not working.
please change this
var myRe = view.findViewById<RecyclerView>(R.id.myRecyclerViewMain)
to this
var myRe = view.findViewById(R.id.myRecyclerViewMain) as RecyclerView

TextView unexpected padding

The RecyclerView shows simple items. Each item has one TextView inside FrameLayout. TextView has no any paddings, but in some cases they appears unexpectedly. The picture below shows that the padding appears immediately after opening the keyboard. The padding appears only if the text contains newline characters. How to fix it ?
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var adapter : MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
val button : Button = findViewById(R.id.button)
adapter = MyAdapter();
recyclerView.adapter = adapter;
button.setOnClickListener {
adapter.notifyDataSetChanged()
}
}
}
class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView;
init {
textView = view.findViewById(R.id.txt)
}
}
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
private val data = listOf("Text without \\n ", "multiline\ntext")
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
MyViewHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false))
override fun getItemCount() = data.size;
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = data[position]
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#android:color/holo_blue_light"
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
activity_main.xml
<?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:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="notifyDataSetChanged"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:id="#+id/button"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="#+id/button"
android:id="#+id/recyclerView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
app:layout_constraintStart_toEndOf="#+id/button" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

How to prevent editText from overlapping recyclerView?

The problem is that when the soft keyboard appears, it overlaps the recyclerView. But I want my soft keyboard just push recycler to the top, to be above the keyboard, like with windowSoftInputMode="adjustPan".
But adjustPan is not the option because I need my tool bar.
I have tried adjustResize already, but no luck.
Main activity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<include
layout="#layout/toolbar_layout"
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/recyc"
app:layout_constraintBottom_toTopOf="#+id/message_edit_text"
app:layout_constraintTop_toBottomOf="#+id/toolbar_layout"/>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/message_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter item layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my MainActivity code, there is the only onCreate method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyc.layoutManager = LinearLayoutManager(this)
val adapter = MyAdapter(
arrayOf("sd","sd2","sd2","sd2","sd2","s2d","s2d","sd",
"sd2","s2d","s2d","sd5","s8d","67sd","s76d")
)
recyc.adapter = adapter
}
Here is my adapter class
class MyAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val view: View) :
RecyclerView.ViewHolder(view)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MyAdapter.MyViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.container, parent, false)
return MyViewHolder(textView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int)
{ holder.itemView.textView.text = myDataset[position] }
override fun getItemCount() = myDataset.size
}

LayoutInfalter push mylayout.xml to my view but without any content

I`m new here and need help.
This my code.
Activity
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
var adapter: PagerAdapter = IntroSliderAdapter(applicationContext)
viewpager.adapter = adapter
}
Adapter
class IntroSliderAdapter(var context: Context) : PagerAdapter() {
lateinit var inflater: LayoutInflater
var texts: Array<Int> = arrayOf(R.string.intro_text1,R.string.intro_text2,R.string.intro_text3)
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as ConstraintLayout
override fun getCount(): Int = texts.size
override fun instantiateItem(container: ViewGroup, position: Int): Any {
inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view: View = inflater.inflate(R.layout.intro_slider, container, false)
Log.i("INFO", "View: " + view.toString())
Log.i("INFO", "Container:" + container.toString())
container!!.addView(view)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container!!.removeView(`object` as ConstraintLayout)
}
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/profileContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".ProfileActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
slider layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:id="#+id/introLayout">
<ImageView
android:id="#+id/mainLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:contentDescription="#string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/introText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
...
</android.support.constraint.ConstraintLayout>
How I understood inflater.inflate(R.layout.intro_slider, container, false) must push intro_slider.xml content to view, but when I run my application I see just empty slides. Plz, help me understand why it's happening.
In my case problem was in complex of mistakes. But main problem was: not correct filled xml layout, for example, my logo image not been visible because app:srcCompat must be android:src and so on..
Thanks Spritzig for help!

Categories

Resources