I am using a RecyclerView with LinearLayoutManager to fetch Data that I get from my API using Retrofit.
The problem is whenever I try to scroll down it goes to the first position.
Some answers on Github & Here, suggested decreasing the image' resolution and avoid using focusable objects but none of these solutions worked for me.
This is my RecyclerView declaration
<android.support.v7.widget.RecyclerView
app:layout_constraintTop_toBottomOf="#id/btn_rech"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp"
android:descendantFocusability="beforeDescendants"
android:id="#+id/recyler_view_voiture_occasion">
</android.support.v7.widget.RecyclerView>
This is the CardView item that I use to bind data
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="340dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="MissingConstraints"
app:cardCornerRadius="10sp"
android:elevation="15dp"
android:background="#color/colorGrey"
app:cardBackgroundColor="#color/colorLightGrey"
android:layout_margin="7dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#color/colorWhite"
android:scaleType="centerCrop"
android:id="#+id/annonce_image"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/general"
android:layout_below="#+id/annonce_image"
android:layout_alignStart="#id/annonce_image"
android:layout_marginStart="10dp"
android:textAppearance="#android:style/TextAppearance.Material.Widget.Toolbar.Title"
android:id="#+id/annonce_info"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Material.Widget.Toolbar.Subtitle"
android:padding="2dp"
android:layout_marginStart="10dp"
android:id="#+id/annonce_price_info"
android:layout_below="#+id/annonce_info"
android:text="this is a sample text"
/>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#ffffff"
android:outlineProvider="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:weightSum="90"
android:background="#drawable/item_edit_versement"
android:layout_below="#id/annonce_price_info"
android:layout_marginHorizontal="10dp"
android:padding="5dp"
android:id="#+id/infos_holder"
>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/annonce_km"
android:layout_weight="30"
android:textAlignment="center"
android:text="#string/prix"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/annonce_annee"
android:textAlignment="center"
android:layout_weight="30"
android:text="#string/prix"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_weight="30"
android:id="#+id/annonce_fuel"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="#string/Fuel"
android:textSize="14sp"
android:fontFamily="sans-serif-medium"
/>
</android.support.v7.widget.LinearLayoutCompat>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:weightSum="100"
android:gravity="center"
android:layout_below="#id/infos_holder"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_edit_versement"
android:layout_marginHorizontal="10dp"
android:layout_weight="40"
android:textStyle="bold"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:text="Faire un offre"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/item_edit_versement"
android:backgroundTint="#424242"
android:drawableStart="#drawable/phone"
android:paddingStart="15dp"
android:text="Call owner"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="15sp"
android:layout_marginHorizontal="10dp"
android:layout_weight="35"
/>
</android.support.v7.widget.LinearLayoutCompat>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my part of Code where I get the data from DB and adapt it into the RV
fun prepareRecyclerView(v : View, id : String){
val layout = LinearLayoutManager(v.context)
layout.orientation = LinearLayoutManager.VERTICAL
val adapter = v.findViewById<RecyclerView>(R.id.recyler_view_voiture_occasion)
adapter.layoutManager = layout
val annonceAdapter = OccasionCarsAdapter(v.context,annonceList )
adapter.adapter = annonceAdapter
//initLineaire(v , R.id.recyler_view_voiture_occasion, LinearLayoutManager.VERTICAL ,annonceAdapter as RecyclerView.Adapter<RecyclerView.ViewHolder>)
}
fun getAnnonceList(id: String, maView: View, filters: VehiculeRechFilters?){
val service = prepareService()
val requestCall = service.GetOccasionAnnouncement(id,
filters!!.minPrix, filters.maxPrix, filters.minAnnee,
filters.minAnnee, filters.maxKm, filters.codeVersion)
requestCall.enqueue(object : Callback<List<VehiculeOccasion>> {
override fun onFailure(call: Call<List<VehiculeOccasion>>, t: Throwable) {
Log.e("Call response" , "Can't get the data" , t.cause)
}
override fun onResponse(call: Call<List<VehiculeOccasion>>, response: Response<List<VehiculeOccasion>>) {
if(response.isSuccessful){
Toast.makeText(this#CustomOccasionFragment.context,response.body().toString(), Toast.LENGTH_LONG).show()
for (e in response.body()!!){
annonceList.add(e)
}
prepareRecyclerView(maView!! ,idUser)
}
else {
Log.i("response assert" , "couldn't get the data correctly")
}
}
})
}
Related
I am hoping that someone could please help me. I am creating an application using Kotlin. Mostly so far, so good, but I am having issues with one area.
The issue is with that 'distance.amountPicker.value' does not seem to be communicating with the layout. All of the other areas are working just fine i.e hikeName.text.toString(), etc, but I cannot get amountPicker to work.
I am not sure if the issue is with the import 'import kotlinx.android.synthetic.main.activity_hike.view.', as these seems to be a different import than the rest, which use 'import kotlinx.android.synthetic.main.activity_hike.'
If anyone could please guide me in the right direction, I'd really appreciate it.
Many thanks
Here is my code:
View:
package org.wit.hikingtrails.views.hike
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_hike.*
import kotlinx.android.synthetic.main.activity_hike.view.*
import org.wit.hikingtrails.R
import org.wit.hikingtrails.models.HikeModel
import org.wit.hikingtrails.views.BaseView
import readImageFromPath
class HikeView : BaseView()
{
lateinit var presenter: HikePresenter
var hike = HikeModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_hike)
progressBar.max = 1000
amountPicker.minValue = 1
amountPicker.maxValue = 1000
init(toolbarAdd)
presenter = initPresenter (HikePresenter(this)) as HikePresenter
btnAdd.setOnClickListener{
presenter.doAddOrSave(
hikeName.text.toString(),
description.text.toString(),
if(difficultyLevel.checkedRadioButtonId == R.id.Intermediate)
"Intermediate" else "Hard",
**distance.amountPicker.value**
) }
btnDelete.setOnClickListener { presenter.doAddOrSave(hikeName.text.toString(), description.text.toString(), difficulty.text.toString(), distance.text.length) }
chooseImage.setOnClickListener { presenter.doSelectImage() }
hikeLocation.setOnClickListener { presenter.doSetLocation() }
btnDelete.setOnClickListener { presenter.doDelete() }
}
override fun showHike(hike: HikeModel) {
hikeName.setText(hike.name)
description.setText(hike.description)
distance.setText("Distance - "+hike.distance+"km")
difficulty.setText("Difficulty Level - "+hike.difficultyLevel)
hikeImage.setImageBitmap(readImageFromPath(this, hike.image.toString()))
if (hike.image != null) {
chooseImage.setText(R.string.change_hike_image)
}
btnAdd.setText(R.string.save_hike)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_hike, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item?.itemId) {
if (hikeName.text.toString().isEmpty()) {
R.id.item_cancel -> {
presenter.doCancel()
}
}
return super.onOptionsItemSelected(item)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data != null) {
presenter.doActivityResult(requestCode, resultCode, data)
}
}
override fun onBackPressed() {
presenter.doCancel()
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.wit.hikingtrails.views.hike.HikeView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:fitsSystemWindows="true"
app:elevation="0dip"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#color/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appBarLayout"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/hikeName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="8dp"
android:autofillHints=""
android:hint="#string/hint_hikeName"
android:inputType="text"
android:maxLength="25"
android:maxLines="1"
android:padding="8dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
<EditText
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="#string/hint_hikeDescription"
android:inputType="text"
android:maxLength="25"
android:maxLines="1"
android:padding="8dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
<Button
android:id="#+id/chooseImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="#string/button_addImage"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<ImageView
android:id="#+id/hikeImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher" />
<Button
android:id="#+id/hikeLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_location"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/hikeSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/donateTitle"
android:layout_alignEnd="#+id/donateTitle"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/hikeSubtitle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/difficultyLevel"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:layout_above="#+id/progressBar"
android:layout_alignParentStart="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/Hard"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/hard" />
<RadioButton
android:id="#+id/Intermediate"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/intermediate" />
</RadioGroup>
<NumberPicker
android:id="#+id/amountPicker"
android:layout_width="wrap_content"
android:layout_height="121dp"
android:layout_alignEnd="#+id/donateSubtitle"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="349dp"
android:layout_height="14dp"
android:layout_above="#+id/donateButton"
android:layout_alignEnd="#+id/donateSubtitle"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:indeterminate="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="#+id/distance"
android:layout_width="267dp"
android:layout_height="38dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/distance" />
<TextView
android:id="#+id/difficulty"
android:layout_width="265dp"
android:layout_height="38dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/difficulty" />
<Button
android:id="#+id/btnAdd"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/colorAccent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_addHike"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#color/red"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:stateListAnimator="#null"
android:text="#string/button_deleteHike"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
There are multiple selectable TextView in the Dialog.
I'm looking for a way to get text values by selecting multiple views in a LinearLayout over and over.
As the first method, I thought of how to set the click event after setting the variables of each view. But this makes the code too long and messy.
As a second method, I thought of using the RecyclerView to place the view and set the click event.
It sounds like a good method, but it seems inefficient because the number of items is so small that it makes no sense to use RecyclerView.
How can I set up a click event that gets the values of the views in the LinearLayout?
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_body_part"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/back"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text=" BACK "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/chest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" CHEST "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/leg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" LEG "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/shoulder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" SHOULDER "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/bieceps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" BICEPS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/triceps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" TRICEPS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<TextView
android:id="#+id/abs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" ABS "
android:textSize="20dp"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="15dp" />
<ImageView
android:id="#+id/selection_state7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_dumbbell"
android:backgroundTint="#android:color/transparent"
android:layout_gravity="center_vertical|end"
android:layout_marginRight="20dp"
android:visibility="invisible"/>
</FrameLayout>
<Button
android:id="#+id/check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="START"
android:textSize="27dp"
android:backgroundTint="#color/purple_200"/>
</LinearLayout>
This is the code I tried.
class BodyPartDialogFragment : DialogFragment(), View.OnClickListener{
private lateinit var ll: LinearLayout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_body_part_dialog, container, false)
ll = view.findViewById(R.id.ll_body_part)
ll.setOnClickListener {
onClick(it)
}
return view
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.back -> Toast.makeText(context, "back", Toast.LENGTH_LONG)
R.id.chest -> Toast.makeText(context, "chest", Toast.LENGTH_SHORT)
R.id.leg -> Toast.makeText(context, "leg", Toast.LENGTH_SHORT)
R.id.shoulder -> Toast.makeText(context, "shoulder", Toast.LENGTH_SHORT)
R.id.bieceps -> Toast.makeText(context, "biceps", Toast.LENGTH_SHORT)
R.id.triceps -> Toast.makeText(context, "triceps", Toast.LENGTH_SHORT)
R.id.abs -> Toast.makeText(context, "abs", Toast.LENGTH_SHORT)
}
}
}
It seems that the entire LinearLayout is selected
Please tell me what to do
Should I just use Recycler View?
You are not clicking the views, you are clicking the linear layout
ll.setOnClickListener {
onClick(it) //it here is view, which in this case is the linear layout
}
So if you want to only select the TextView and is always going to be a TextView the view you need, you can iterate through them:
fun addClickListeners(viewGroup: ViewGroup) {
for (i in 0 until viewGroup.childCount) {
val candidate = viewGroup.childAt(i) as? TextView
if (candidate != null) {
candidate.setOnClickListener {
it.text.toString() //do something
}
} else {
(viewGroup.childAt(i) as? ViewGroup)?.let {
addClickListeners(it) //recursive for inner childs
}
}
}
}
Then call it like this
addClickListeners(ll)
Regarding if you should use or not a RecyclerView that is up to you. If your evaluation is the requirement is low enough for trusting that is always gonna be a TextView then do that. If you think it can increase the scope then use a RecyclerView. If you think there is gonna be a middle point where things grow but not that much, create a custom view.
I am building an android app which has follow and unfollow feature. I have 3 pages inside ViewPager in main home. Feed consist of post of the users based on interest.
For example, user A follows user B, that posts the article in 3 different interest food, travel and tech. In this case, user A will be able to see his all the post in the 3 different places. What I want is that if user A who follows B unfollow the user B from any of the page it will just get removed from all the pages and refresh the feed.
I want to observe that follow and unfollow event based on the user action and refresh the feed.
How to build observer for this pattern. I am open to use anything expect BroadcastReceiver.
feed_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:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:cardCornerRadius="4dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="horizontal">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/profilePicture"
android:layout_width="40dp"
android:layout_height="40dp"
fresco:roundedCornerRadius="#dimen/feed_rect_card_radius" />
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:textColor="#color/darkTextColor" />
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:fontFamily="#font/noto_sans_bold"
android:textColor="#747474"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/followBtn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_marginEnd="32dp"
android:fontFamily="#font/noto_sans_bold"
android:gravity="center"
android:text="#string/follow"
android:textColor="#color/green"
android:textSize="10sp"
app:rippleColor="#color/green"
app:strokeColor="#color/green" />
<ImageView
android:id="#+id/pop_up_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:layout_marginTop="4dp"
android:contentDescription="#null"
android:padding="2dp"
android:src="#drawable/ic_more_dark" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="4dp"
android:paddingTop="8dp"
android:paddingEnd="4dp"
android:paddingBottom="8dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/image"
android:layout_width="124dp"
android:layout_height="80dp"
fresco:roundedCornerRadius="#dimen/feed_rect_card_radius" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:contentDescription="#null"
android:src="#drawable/ic_play_circle_feature" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="#dimen/item_offset">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:ellipsize="end"
android:fontFamily="#font/noto_sans_bold"
android:gravity="start|top"
android:maxLines="2"
android:textColor="#color/darkTextColor"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="#font/noto_sans"
android:gravity="start"
android:lineSpacingExtra="4dp"
android:maxLines="2"
android:padding="2dp"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/categoryLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/whatsAppShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:foreground="?attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#null"
android:src="#drawable/ic_whatsapp_fill" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
FeedAdapter
class FeedAdapter(private val mContext: Context, val fg: FragmentManager, val nodes: ArrayList<Feed.Item>, val tagId: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var showLoader: Boolean = false
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ViewTypes.FEED -> FeedItemVH(LayoutInflater.from(mContext).inflate(R.layout.feed_item, parent, false))
else -> FooterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_footer, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is FeedItemVH -> feedItemView(holder, nodes[position].fragments().feedItem())
else -> {
if (showLoader) (holder as FooterViewHolder).itemView.footerLoader.visibility = View.VISIBLE
else (holder as FooterViewHolder).itemView.footerLoader.visibility = View.GONE
}
}
}
private fun feedItemView(holder: FeedItemVH, feed: FeedItem) {
holder.followBtn.setOnClickListener {
//API call to follow/unfollow user and broadcast the result.
}
}
override fun getItemCount(): Int {
return when (nodes.size) {
0 -> 0
else -> nodes.size + 1
}
}
override fun getItemViewType(position: Int): Int {
return when {
position != 0 && position == itemCount - 1 -> ViewTypes.LOADER
else -> {
ViewTypes.FEED
}
}
}
fun showLoader(status: Boolean) {
showLoader = status
Handler().post { this.notifyDataSetChanged() }
}
interface ViewTypes {
companion object {
const val FEED = 1
const val LOADER = 2
}
}
}
Any type of help would be appreciated. Thanks!
I am using ConstraintLayout as a parent and put recyclerview in it to populate the list. Suppose I have to give item count 5 to populate the list and run the code then the list is showing perfectly but the last item of the recyclerview is showing half and not fully visible, and scroll stops at there. I found a solution for this is if am giving height match_parent to the recyclerview then it works fine but then all other view is hidden behind the recyclerview.
If I use another parent view like LinearLayout or RelativeLayout, then recyclerview with height wrap_content works fine and all the list item are fully visible.
I have tried by 2 ways that work for me but I found that wrong programming practice
I give height match_parent to recyclerview and give top_margin to the recylerview so all the other UI item show.
I give padding_bottom to the recyclerview until all the list item position visible
My XML file "activity_per_day_sale.xml" is below.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/btn_back"
android:padding="5dp"
android:onClick="OnClickPerDaySale"
android:background="?attr/selectableItemBackground"
android:src="#drawable/icon_back_white"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:layout_centerInParent="true"
android:id="#+id/home_title_text"
android:textColor="#color/black"
android:visibility="gone"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP"
android:padding="10dp"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="#+id/skip"
android:textColor="#color/black"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#color/yellow_app_logo_color"
android:fontFamily="#font/segoeuib"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/TV_signupScreenText"
android:layout_marginLeft="45dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rv_perday_sale"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="10dp"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText"/>
</android.support.constraint.ConstraintLayout>
Activity class is "PerDaySaleActivity.kt"
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.nq.NQManager.R
import com.nq.NQManager.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_per_day_sale.*
class PerDaySaleActivity:BaseActivity(){
var adapterPerdaySale : AdapterPerdaySale? = null
companion object {
fun start(context: Context) {
val starter = Intent(context, PerDaySaleActivity::class.java)
context.startActivity(starter)
}
}
override fun getID(): Int {
return R.layout.activity_per_day_sale
}
override fun iniView(savedInstanceState: Bundle?) {
initViews()
}
fun OnClickPerDaySale(v: View){
when(v){
btn_back->{
finish()
}
}
}
fun initViews() {
adapterPerdaySale = AdapterPerdaySale( this)
rv_perday_sale.layoutManager = StaggeredGridLayoutManager(1, 1)
rv_perday_sale.adapter = adapterPerdaySale
}
private fun setUpRecyclerView() {
runOnUiThread { adapterPerdaySale!!.notifyDataSetChanged() }
}
My Adapter class "AdapterPerdaySale.kt"
mport android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.nq.NQManager.R
import kotlinx.android.synthetic.main.adapter_perday_sale.view.*
class AdapterPerdaySale(context: Context):
RecyclerView.Adapter<AdapterPerdaySale.MyViewHOlder>() {
private var ctx:Context?=context
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHOlder {
return MyViewHOlder(LayoutInflater.from(ctx).inflate(R.layout.adapter_perday_sale, p0, false))
}
override fun getItemCount(): Int {
return 5
}
override fun onBindViewHolder(holder: MyViewHOlder, position: Int) {
if (position==1){
holder.tv_date.text="26 June, 2019"
}else if(position==2){
holder.tv_date.text="27 June, 2019"
}
}
inner class MyViewHOlder(view: View) : RecyclerView.ViewHolder(view) {
val tv_date=view.tv_date
}
}
My Adapter xml file "adapter_perday_sale.xml"
<?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" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:background="#color/black">
<TextView
android:id="#+id/tv_date"
android:layout_below="#+id/txt_today"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"
android:text="25 June, 2019"
android:textSize="23sp"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/tv_date">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Orders"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="50"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Revenue"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="£550"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#color/gray"
android:id="#+id/view_1"
app:layout_constraintTop_toBottomOf="#+id/linearLayout1"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
Add this app:layout_constraintBottom_toBottomOf="parent" property to your recyclerview
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="#+id/btn_back"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?attr/selectableItemBackground"
android:onClick="OnClickPerDaySale"
android:padding="5dp"
android:src="#drawable/ic_background" />
<TextView
android:id="#+id/home_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Signup"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
<TextView
android:id="#+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="SKIP"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/TV_signupScreenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#AD4E4E"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_perday_sale"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText" />
</androidx.constraintlayout.widget.ConstraintLayout>
In my fragment, I have two linear layout with the same tag "result". Inside theses linear layouts, I have another layout with the tag "toggle" and a button with the tag "toggleButton".
I want that : when I click on my button, it toggle the linear layout "toggle" of its linear layout parent "result"
But when I do this :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val root = rootLayoutResultSearch
root.findViewWithTag<Button>("toToggleButton")
val buttonToggle = view.findViewWithTag<Button>("toToggleButton")
buttonToggle.setOnClickListener{
Log.i(TAG, "click")
val toggle = view.findViewWithTag<LinearLayout>("toToggle")
if(toggle.visibility == View.GONE){
toggle.visibility = View.VISIBLE
}else{
toggle.visibility = View.GONE
}
}
}
it's working only for my first linear layout "result". When I click on my second button, it's not doing anything
Here the xml of the layouts "result" (I only post the first, the second is the exact same one)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="result"
android:contentDescription="result">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="35dp"
android:background="#drawable/whit_bg_and_shadow"
android:tag="visibleNotChangeable"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche1"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche1"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche2"
android:textFontWeight="500"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/jourCircuRecherche" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/fillJourCircuRecherche" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:background="#drawable/dark_blue_rectangle"
android:text="3"
android:textColor="#color/colorWhite" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/horaire" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:text="#string/fillTempsRecherche" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="35dp"
android:background="#drawable/gray_bg"
android:visibility="gone"
android:tag="toToggle"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche1"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche1"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/hoursSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:fontFamily="#font/rubik_medium_italic"
android:tag="heureDepart"
android:text="#string/fillHoursRecherche2"
android:textFontWeight="500"
android:textStyle="italic" />
<TextView
style="#style/citySearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight=".9"
android:fontFamily="#font/rubik_medium"
android:tag="villeDepart"
android:text="#string/fillVilleRecherche2"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/jourCircuRecherche" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto"
android:tag="villeArrivee"
android:text="#string/fillJourCircuRecherche" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:background="#drawable/dark_blue_rectangle"
android:text="3"
android:textColor="#color/colorWhite" />
</LinearLayout>
<View style="#style/HorizontalLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/horaire" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:fontFamily="#font/roboto"
android:text="#string/fillTempsRecherche" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/ToggleSearchResult"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/arrow_down_blue_circle"
android:layout_gravity="center"
android:layout_marginTop="-20dp"
android:tag="toToggleButton"
/>
</LinearLayout>
Okay, I've thrown together a quick idea of what I think you want to accomplish. It excludes all the error and consistency checking one would normally want to do.
package com.example.toggler
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val list = arrayListOf<View>()
root_View.findViewsWithText(list, "toToggleButton", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION)
list.forEach { button ->
button.setOnClickListener { v: View ->
val viewParent = v.parent
if (viewParent is LinearLayout) {
val taggedView = viewParent.findViewWithTag<View>("toggle")
taggedView.visibility = when {
taggedView.visibility == View.GONE -> View.VISIBLE
else -> View.GONE
}
}
}
}
}
}
activity_main.xml
<?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:id="#+id/root_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#F44336"
android:tag="toggle" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#9C27B0"
android:tag="toggle" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:contentDescription="toToggleButton"
android:tag="toToggleButton"
android:text="toggle"
tools:ignore="HardcodedText" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFEB3B"
android:tag="toggle" />
</LinearLayout>
</LinearLayout>
[EDIT]
Or, if you prefer to use tags:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
root_View.findViewsWithTag("toToggleButton").forEach { button ->
button.setOnClickListener { v: View ->
val viewParent = v.parent
if (viewParent is LinearLayout) {
val taggedView = viewParent.findViewWithTag<View>("toggle")
taggedView.visibility = when {
taggedView.visibility == View.GONE -> View.VISIBLE
else -> View.GONE
}
}
}
}
}
}
private fun ViewGroup.findViewsWithTag(tag: String): Sequence<View> {
return sequence {
for (index in 0 until childCount) {
val child = getChildAt(index)
when (child) {
is ViewGroup -> yieldAll(child.findViewsWithTag(tag))
is View -> if (child.tag == tag) yield(child)
}
}
}
}