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.
Related
Hello Team!!
I'm new on android but i have knoweledge in html and javascript. So i have a JSON that have some options(img,or not img) and i create a recycler view to list this JSON and all ok, later i try that hide some imageView of card because the JSON show that this doesn't have picture. It works fine
But when i do scroll down to bottom limit all imageViews hide, i don't know how solve it and i search in web but i can't find some solution for this issue.
Code in adapter
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onBindViewHolder(postViewHolder postViewHolder, int position) {
addPostTimeline addPost = addPostTimelines.get(position);
try {
CircleImageView img=postViewHolder.imgFoto;
JSONObject fileson=addPost.getDatosTimeine().getJSONObject("file");
// postViewHolder.imgTimeLine = #+id/image_timeline
if(!fileson.getBoolean("err")){ // si tiene archivos adjuntos
switch (fileson.getString("type")){
case "Pic":
imageTimeline imgTm=new imageTimeline(fileson.getString("src"), postViewHolder.imgTimeLine);
imgTm.execute();
break;
default:
postViewHolder.imgTimeLine.setVisibility(View.GONE);
break;
}
}else{
postViewHolder.contentFiles.setVisibility(View.GONE);
}
putImgProfile putImg=new putImgProfile(addPost.getDatosTimeine().getString("pDir"), img);
putImg.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
Structure of card with recycler
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cvManana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
card_view:cardCornerRadius="4dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout android:background="#drawable/textlines"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:orientation="vertical"
android:id="#+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgPerfil"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="8dp"
android:scaleType="centerCrop"
android:src="#mipmap/ic_load_img" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_fechaPost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hace 44 minutos sep. 22º 16"
android:textColor="#999" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="12dp"
android:text="SonickSeven"
android:textColor="#24650e"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:id="#+id/contentTextStory">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_postTimeLine"
android:textColor="#666"
android:textSize="15dp"
android:layout_gravity="left"
android:text="historias"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contentFiles">
<!-- image to hide -->
<ImageView
android:id="#+id/image_timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitStart"
card_view:srcCompat="#drawable/campo_manana" />
<!-- end imagen to hide -->
</LinearLayout>
<LinearLayout
android:paddingTop="4dp"
android:paddingBottom="7dp"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="right"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_reply"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_reply_gray"/>
<TextView
android:id="#+id/countReply"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_share"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_share_gray"/>
<TextView
android:id="#+id/countShare"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:layout_gravity="center_vertical"
android:id="#+id/bt_like"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_like_gray"/>
<TextView
android:id="#+id/countLike"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_dislike"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_nolike_gray"/>
<TextView
android:id="#+id/countNoLike"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
i saw that when i go to bottom of scroll and up the switch trigger again, but it doesn't could because the recycler view run all array JSON. Sorry i can understand how work android-java
Try this in onBindViewHolder method:
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onBindViewHolder(postViewHolder postViewHolder, int position) {
addPostTimeline addPost = addPostTimelines.get(position);
try {
CircleImageView img=postViewHolder.imgFoto;
JSONObject fileson=addPost.getDatosTimeine().getJSONObject("file");
if(!fileson.getBoolean("err") && fileson.getString("type").equals("pic")){
//image must be visible and no error
postViewHolder.imgTimeLine.setVisibility(View.VISIBLE);
postViewHolder.contentFiles.setVisibility(View.VISIBLE);
imageTimeline imgTm=new imageTimeline(fileson.getString("src"), postViewHolder.imgTimeLine);
imgTm.execute();
}else if(!fileson.getBoolean("err") && !fileson.getString("type").equals("pic")){
//image shouldn't be visible and no error
postViewHolder.imgTimeLine.setVisibility(View.GONE);
postViewHolder.contentFiles.setVisibility(View.VISIBLE);
}else{
//error
postViewHolder.imgTimeLine.setVisibility(View.GONE);
postViewHolder.contentFiles.setVisibility(View.GONE);
}
putImgProfile putImg=new putImgProfile(addPost.getDatosTimeine().getString("pDir"), img);
putImg.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
You need to reset the visible where you don’t need to set the visible.
postViewHolder.imgTimeLine.setVisibility(View.VISIBLE);// reset
if(!fileson.getBoolean("err")){ // si tiene archivos adjuntos
switch (fileson.getString("type")){
case "Pic":
imageTimeline imgTm=new imageTimeline(fileson.getString("src"),
postViewHolder.imgTimeLine);
imgTm.execute();
break;
default:
postViewHolder.imgTimeLine.setVisibility(View.GONE);
break;
}
}else{
postViewHolder.contentFiles.setVisibility(View.GONE);
}
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")
}
}
})
}
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)
}
}
}
}
I am an amateur in android studio and kotlin. I am implementing a dashboard where I have frame layout as the bodylayout and is replaced by different fragments when each item on the bottomnavigationview is clicked. However, for one particular item I need to use two view, imageview and a scrollview. So, when the progress bar in the scrollview contains no progress, I have to show the Imageview hiding the scrollview and also disabling the scroll at that time. But When there is some progress in the progressbar in the scrollview, scrollview with it's content should be shown and not the imageview.
I have seen some answers int he same context but it didn't work for me. So, I would like to know how to do it? Am I doing it wrong.
The code snippet is below.
class ProgressFragment : Fragment() {
val TAG = "ProgressFragment"
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_progress, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
progress_progressbar2?.setProgress(5)
progress_progressbar2?.max=15
if (progress_progressbar2?.progress!!.equals(0)){
started_image?.bringToFront()
progress_scrollview?.invalidate()
}
else{
progress_scrollview?.bringToFront()
started_image?.invalidate()
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
}
}
I am calling this fragment in the mainactivity and replacing the framelayout with this fragment in the mainactivity.
The activity_main.xml layout is given below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
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:background="#mipmap/bg"
tools:context=".MainActivity">
<ImageView
android:id="#+id/navigation_header_container"
android:layout_width="match_parent"
android:layout_height="65dp"
android:scaleY="1.5"
android:scaleX="2"
android:src="#drawable/header_green"
/>
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="75dp"
android:layout_above="#id/bottom_nav"
android:layout_alignParentStart="true"
android:layout_below="#+id/navigation_header_container"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:itemBackground="#color/colorWhite"
app:itemTextColor="#color/nav_item_colors"
app:menu="#menu/bottom_navigation">
</android.support.design.widget.BottomNavigationView>
<TextView
android:id="#+id/header_text"
android:layout_width="156dp"
android:layout_height="39dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:gravity="center"
android:textSize="20dp"
android:textColor="#200"
android:textStyle="bold"
android:text="TextView" />
<!--app:itemIconTint="#color/nav_item_colors"-->
<!--app:itemTextColor="#color/nav_item_colors"-->
</RelativeLayout>
The progress layout is given below.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProgressFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:id="#+id/progress_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/progress_parentrelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/progress_child1relayout"
android:layout_width="190dp"
android:layout_height="280dp">
<TextView
android:id="#+id/quadrant1_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="Min"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/progress_progressbar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="140dp"
android:progressDrawable="#drawable/customprogressbar" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/progress_child2relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_below="#+id/progress_child1relayout">
<TextView
android:id="#+id/quadrant2_textview1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40sp"
android:layout_marginStart="40sp"
android:layout_marginTop="70dp"
android:text="Challenge Status"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="#+id/progress_progressbar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="160dp"
android:progressDrawable="#drawable/customprogressbar" />
<TextView
android:id="#+id/quadrant2_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="42dp"
android:layout_marginTop="130dp"
android:text="Completed"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/quadrant2_textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="190dp"
android:text="open"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/quadrant2_textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="230dp"
android:text="0"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/progress_child3relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_toRightOf="#+id/progress_child1relayout">
<TextView
android:id="#+id/quadrant3_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="TextView"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/progress_child4relayout"
android:layout_width="190dp"
android:layout_height="280dp"
android:layout_below="#id/progress_child3relayout"
android:layout_toRightOf="#+id/progress_child2relayout">
<TextView
android:id="#+id/quadrant4_textview1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60sp"
android:layout_marginTop="70dp"
android:text="Pods Mastered"
android:textColor="#200"
android:textSize="18sp"
android:textStyle="bold" />
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="#+id/circularprogress"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="130dp"
android:progress="20" />
<!--<ProgressBar-->
<!--android:id="#+id/progress_progressbar3"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginTop="120dp"-->
<!--android:layout_marginLeft="50sp"-->
<!--android:indeterminateDrawable="#drawable/ringprogressbar"-->
<!--android:max="100"-->
<!--android:progress="20"-->
<!--style="?android:attr/progressBarStyleLarge" />-->
</RelativeLayout>
<TextView
android:id="#+id/whatdoesthis_mean"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="580dp"
android:clickable="true"
android:gravity="center"
android:text="What does this mean?"
android:textColor="#200"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/child_button"
android:layout_width="344dp"
android:layout_height="79dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="620dp"
android:src="#drawable/pinkcolor"
android:text="child name" />
<TextView
android:id="#+id/child_name_text"
android:layout_width="184dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="11dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="Child Name"
android:textColor="#190fdf"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
<ImageView
android:id="#+id/started_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/letsgetstarted"
android:background="#ebcac3c7"
/>
</FrameLayout>
Any help is appreciated.
Fragments don't create the view in OnCreate unlike the Activity. The same can be understood through the life-cycle of a fragment. Moreover, this is one of the major differences between an activity life-cycle and a fragment life-cycle. So, instead of assigning the values in the OnCreate, it should be done in OnViewCreated.
The code snippet could be modified to:
class ProgressFragment : Fragment() {
val TAG = "ProgressFragment"
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_progress, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
progress_progressbar2?.setProgress(12)
progress_progressbar2?.max=15
val currentProgress = progress_progressbar2?.progress ?: 0
if (currentProgress == 0){
started_image?.visibility = View.VISIBLE
progress_scrollview?.visibility = View.GONE
} else {
started_image?.visibility = View.GONE
progress_scrollview?.visibility = View.VISIBLE
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
}
}
The condition and static/dynamic progress assignment can be done in the OnResume method also.
Thank you Vishnu.
val currentProgress = progress_progressbar2?.progress ?: 0
if (currentProgress == 0){
started_image?.visibility = VISIBLE
progress_scrollview?.visibilty = GONE
} else {
started_image?.visibility = GONE
progress_scrollview?.visibilty = VISIBLE
navigation_header_container?.setImageResource(R.drawable.header_pink)
}
Instead of bringToFront you manage it using the visibility of the views.
Android Documentation : https://developer.android.com/reference/android/view/View.html#setVisibility(int)
Try this out
enter image description here
In Activty one i have RecyclerView with to View :
HEADER_VIEW
ITEM_VIEW
Header_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFF7F7F7">
<TextView
android:id="#+id/tv_group_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#F50057"
android:textSize="20sp" />
<TextView
android:id="#+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:text="More..." />
</RelativeLayout>
Item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/imageView"
android:background="#f5fff2">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="name"
android:textAlignment="center"
android:textColor="#FF20C200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/new_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/old_price"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/old_price"
android:text="480.00 DA"
android:textColor="#FF65C2FD"
android:textStyle="bold" />
<TextView
android:id="#+id/old_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv_name"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:text="250.00 DA"
android:textAlignment="center"
android:textColor="#F50057" />
<ImageButton
android:id="#+id/overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/new_price"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:srcCompat="#drawable/ic_more_vert_black_24dp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I created the Adapter and it Work Fine .
My problem is :
I need when I click on more TextView In HEADER_VIEW an New Activity Start
with Specific Itemes Of Stection.
My try :
In Adapter Exactly in onBindViewHolder
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//Context context = mContextWeakReference.get();
if (mContext == null) {
return;
}
if (SECTION_VIEW == getItemViewType(position)) {
final SectionViewHolder sectionViewHolder =(SectionViewHolder)holder;
GroupTitleModel sectionItem = ((GroupTitleModel) mUsersAndSectionList.get(position));
sectionViewHolder.title.setText(sectionItem.title);
sectionViewHolder.more.setText(sectionItem.moreSection);
sectionViewHolder.more.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
// I TRY TO DO IT HERE BUT NO THINGS
}
} );
return;
}
My first thought is that you are bringing yourself into trouble by doing it this way.
You separate the Header view and its Content view, so the problem appear when the Header has no idea which content to continue to next step. If you don't want to refactor than it's ok, there will be a workaround. But my suggestion is that you combine the header and the content into 1 ViewHolder, keep the title, more text, and items inside 1 Model class too. They need to go with each other so they know how to handle with interaction