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

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!

Related

View below recyclerview is not clickable

Hi I am working on showing a view (publisher ad) below recycler view. Recycler view has some empty views to show publisher ad when it comes to related index. Currently I can click on any recycler view item and I am able to see my ad, but I cannot click on ad. How to achieve this? Here is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/parallax_ad_unit_id"/>
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
</widgets.AppSwipeRefreshLayout>
</FrameLayout>
As you are using FrameLayout the PublisherAdView will be overlapped by AppRecyclerView or AppSwipeRefreshLayout, so when you click on item, that click event will be listened by `AppRecyclerView1 every time.
Try after changing the position of your RecyclerView and AdView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
</widgets.AppSwipeRefreshLayout>
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/parallax_ad_unit_id"/>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="false"
android:scrollbars="vertical"
ads:layout_constraintBottom_toBottomOf="parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintTop_toBottomOf="#id/ad_view" />
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="0dp"
ads:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</widgets.AppSwipeRefreshLayout>
Use a RelativeLayout for activity_main.xml which allows to place views
on top of others (in Z axis).
ex: SwipeRefreshLayout is the top most view here.
<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"
android:focusable="true"
tools:context=".MainActivity">
<View
android:id="#+id/ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_posts"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_posts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
Create two item layouts for two types of recycler view types
ex:
item_normal.xml, item_transparent.xml (where layout background is transparent that allow to see the layout below the surface area)
Setting android:clickable=false in item_transparent does not stop triggering the click event on transparent item, so use communication flow using interfaces, to bring the other view(ad) to the front when clicked on transparent item.
MainActivity.kt
class MainActivity : AppCompatActivity(), RvAdpater.OnItemClick {
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
private lateinit var adView: View
private lateinit var rvPosts: RecyclerView
override fun onClick() {
bringAdFront()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupRecyclerView()
setupSwipeRefreshLayout()
}
private fun setupRecyclerView() {
rvPosts = findViewById(R.id.rv_posts)
rvPosts.layoutManager = LinearLayoutManager(this)
val rvAdpater = RvAdpater()
rvAdpater.setListener(this)
rvPosts.adapter = rvAdpater
}
private fun setupSwipeRefreshLayout() {
swipeRefreshLayout = findViewById(R.id.swipe_refresh_posts)
swipeRefreshLayout.setOnRefreshListener {
Log.d(TAG, "Refreshing...")
}
}
private fun bringAdFront() {
adView = findViewById<View>(R.id.ad)
adView.bringToFront()
}
// to go back to the normal recycler view(which is inside swipe refresh layout)
// when back button is pressed
override fun onBackPressed() {
val parent = swipeRefreshLayout.parent as ViewGroup
parent.removeAllViews()
parent.addView(adView, 0)
// at last add swipe refresh layout which is the container of the recyclerview
parent.addView(swipeRefreshLayout, 1)
}
}
RvAdapter.kt
const val TAG = "RvAdpater"
class RvAdpater : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var listener:OnItemClick
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val viewNormal = LayoutInflater.from(parent.context).inflate(R.layout.item_normal, parent, false)
val viewTransparent = LayoutInflater.from(parent.context).inflate(R.layout.item_transparent, parent, false)
return when(viewType){
0 -> NormalViewHolder(viewNormal)
2 -> TransparentViewHolder(viewTransparent)
else -> NormalViewHolder(viewNormal)
}
}
override fun getItemCount(): Int = 10
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(getItemViewType(position)){
0 -> {
val normalHolder = holder as NormalViewHolder
normalHolder.tv.text = "Post"
normalHolder.itemView.setOnClickListener {
Log.d(TAG, "Clicked on Normal item")
}
}
2 -> {
val transparentHolder = holder as TransparentViewHolder
transparentHolder.itemView.setOnClickListener {
listener.onClick()
}
}
}
}
fun setListener(onItem:OnItemClick){
listener = onItem
}
interface OnItemClick{
fun onClick()
}
override fun getItemViewType(position: Int): Int = position % 2 * 2
class NormalViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val tv:TextView = itemView.findViewById(R.id.tv_post)
}
class TransparentViewHolder(itemView: View): RecyclerView.ViewHolder(itemView)
}
Checkout branch: with-swipe-layout

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>

RecyclerView only shows 1 item when I use LinearLayout instead of CardView as the parent XML layout

Here's my XML Fragment:
fragment_leaderboards.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/leaderboards_list"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
and my custom view for the LayoutInflater of the ViewHolder:
leaderboard_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/leaderboard_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Title"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imgShare"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#android:drawable/ic_menu_share"
android:tint="#eeeeee"/>
</LinearLayout>
The above XML will only show 1 item of my RecyclerView. It will also show only 1 item of RecyclerView if I put the CardView inside the LinearLayout: <LinearLayout><Cardview>...</Cardview></LinearLayout>
However, if I wrap the LinearLayoutin a CardView, it will show all items (which is 2 at the moment): <Cardview><LinearLayout>...</LinearLayout></Cardview>.
Why is this?
Here's my Kotlin code if you're curious:
fragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_leaderboards, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewManager: RecyclerView.LayoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
val viewAdapter: RecyclerView.Adapter<*> = LeaderboardsAdapter(arrayListOf(Player("James", 23, "male"), Player("Jane", 22, "female")))
val recyclerView: RecyclerView = leaderboards_list.apply {
adapter = viewAdapter
layoutManager = viewManager
setHasFixedSize(true)
}
}
LeaderboardsAdapter.kt
class LeaderboardsAdapter(private val players: ArrayList<Player>) : RecyclerView.Adapter<LeaderboardsAdapter.ViewHolder>(){
class ViewHolder(val v: View) : RecyclerView.ViewHolder(v)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeaderboardsAdapter.ViewHolder {
Log.d("Viewgroup", "$parent.toString() $parent.")
val view = LayoutInflater.from(parent.context).inflate(R.layout.leaderboard_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: LeaderboardsAdapter.ViewHolder, position: Int) {
holder.v.leaderboard_name.text = players[position].name
}
override fun getItemCount(): Int = players.size
}
My LinearLayout had android:layout_height="match_parent" so the 1 item was taking up the whole screen - that was the problem.
Exactly, either you can set android:layout_height="wrap_content" or fix height as per your view requirement. See this is very common among android developers. Good Luck for next time.

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
}

How can I reduce spacing between items in RecyclerView?

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>

Categories

Resources