Prevent Bottom Sheet Covering Bottom App Bar - bottom-sheet

I have a BottomAppBar in my activity and a BottomNavigationFragment that extends BottomSheetDialogFragment.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(bottom_app_bar)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) {
android.R.id.home -> {
val bottomNavDrawerFragment = BottomNavigationFragment()
bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
}
}
return true
}
}
activity_main.xml
<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=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorPrimary"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/baseline_menu_white_24"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/baseline_add_white_24"
app:layout_anchor="#id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is when bottom sheet collapses, it covers BottomAppBar.
I want to it open behind the BottomAppBar.

Related

Onclick doen't work in the fragments of viewpager2

I have a viewpager2 with a tablayout with two tabs. In the fragment of each tab a have a button, but the onclick listener never is called when I click on it. I don't know where is the problem...
Fragment where I instance the viewpager2:
...
#BindView(R.id.tab_viewpager2_bookings)
lateinit var tabLayout: TabLayout
#BindView(R.id.viewpager2_booking)
lateinit var viewPager: ViewPager2
...
val tabs = arrayOf(getString(R.string.bookings_on_going), getString(R.string.bookings_upcoming))
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter: FragmentStateAdapter = BookingsPagerAdapter(requireActivity(), tabs, ticketsOnGoing, ticketUpcoming, _user!!, bearerToken)
viewPager.adapter = adapter
if (ticketsOnGoing.isEmpty() && ticketUpcoming.isNotEmpty()) {
viewPager.currentItem = 1
}
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = tabs[position]
}.attach()
The XML with the viewpager2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_viewpager2_bookings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:visibility="visible"
app:tabIndicatorColor="#color/tabs_indicator_boardingpass"
app:tabTextColor="#color/tabs_indicator_boardingpass"></com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewpager2_booking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tab_viewpager2_bookings" />
...
The adapter:
class BookingsPagerAdapter(fa: FragmentActivity, private val tabs:Array<String>, private val
ticketsOnGoing: List<TicketModel>, private val ticketUpcoming: List<TicketModel>,
private val user: UserModel, private val bearerToken: String) : FragmentStateAdapter(fa) {
override fun getItemCount(): Int {
return tabs.size
}
override fun createFragment(position: Int): Fragment {
when(position){
0 -> return BookingsTabFragment(ticketsOnGoing, position, user, bearerToken)
1 -> return BookingsTabFragment(ticketUpcoming, position, user, bearerToken)
}
return BookingsTabFragment(ticketsOnGoing,0, user, bearerToken)
}
}
The tab fragment:
class BookingsTabFragment(private val bookingsList: List<TicketModel>, val position: Int, val user: UserModel, private val bearerToken: String) : BaseFragment() {
lateinit var buttonTest : Button
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v: View = inflater.inflate(R.layout.fragment_tab_bookings, container, false)
if (bookingList.isEmpty()) {
llMessageBookingsFragment.visibility = View.GONE
rv_bookings_list.visibility = View.VISIBLE
buttonTest = v.findViewById<Button>(R.id.button_test)
buttonTest.setOnClickListener(View.OnClickListener {
Toast.makeText(context, "Prueba", Toast.LENGTH_SHORT).show()
//IT NEVER ENTER HERE!!
})
} else {
llMessageBookingsFragment.visibility = View.VISIBLE
rv_bookings_list.visibility = View.GONE
...
The XML of tab fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="#+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="#layout/row_bookings"
tools:visibility="visible"/>
Thanks!
I found the problem. In the tab fragment, I use RelativeLayout. The order of the components is very important for this issue, so I put the recycler view before the layout with the button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="#layout/row_bookings"
tools:visibility="visible"/>
<LinearLayout
android:id="#+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="#+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...

Android App stops with a Keeps Stopping message

I am following the instructions at CodeLabs to add the countMe function
But when I run it I get a "My First App keeps stopping" message.
import android.view.View
import android.widget.TextView
import android.widget.Toast
import android.widget.Toast.*
class MainActivity() : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun countMe (view: View) {
// Get the text view
val showCountTextView = findViewById<TextView>(R.id.textView)
// Get the value of the text view.
val countString = showCountTextView.text.toString()
// Convert value to a number and increment it
var count: Int = Integer.parseInt(countString)
count++
// Display the new value in the text view.
showCountTextView.text = count.toString();
}
}
the layout is
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/FirstHello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="#+id/textView"/>
<Button
android:text="#string/toast"
android:onClick="toastMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toast_button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"/>
<Button
android:text="Count"
android:onClick="countMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/count_button" android:layout_marginTop="152dp"
app:layout_constraintTop_toTopOf="#+id/toast_button" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="48dp" android:layout_marginStart="48dp"/>
<Button
android:text="Random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/random_button" android:layout_marginTop="160dp"
app:layout_constraintTop_toBottomOf="#+id/textView" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="72dp" android:layout_marginRight="72dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
When I run the app and click the button the application stops.
It worked when I changed the text to contain a number

RecyclerView OnclickListener in CardView

I have a list with recyclerView in which the CardView is visualized through an adapter and within the CardView I have 8 textView.
Up to that point everything is perfect, the list is displayed well.But when implementing the onClick to the cardView I have the following problem,
that the cardViews are only clickable in the background below the TextView elements, it is as if they cover the clickleable zone
ArrayList<Maquina> maquinasList;
maquinasList=new ArrayList<>();
listaMaquinas= (RecyclerView) findViewById(R.id.lista_de_maquinas);
listaMaquinas.setLayoutManager(new LinearLayoutManager(this));
maquinasAdapter adapter= new maquinasAdapter(maquinasList);
adapter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Maquina user=maquinasList.get(listaMaquinas.getChildAdapterPosition(v));
Intent intent=new Intent(ListaMaquinas.this,RegistrarMaquina.class);
Bundle bundle=new Bundle();
bundle.putSerializable("maquina",user);
intent.putExtras(bundle);
startActivity(intent);
}
});
listaMaquinas.setAdapter(adapter);
public class maquinaAdapter extends RecyclerView.Adapter<maquinaAdapter.MaquinaViewHolder> {
private ArrayList<Maquina> listaMaquinas;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class MaquinaViewHolder extends RecyclerView.ViewHolder{
public TextView tipo;
public TextView marca;
public TextView modelo;
public TextView serie;
public MaquinaViewHolder(View itemView,final OnItemClickListener listener){
super(itemView);
tipo= itemView.findViewById(R.id.tipo);
marca= itemView.findViewById(R.id.marca);
modelo= itemView.findViewById(R.id.modelo);
serie= itemView.findViewById(R.id.serie);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener!=null){
int position=getAdapterPosition();
if (position!=RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
public maquinaAdapter(ArrayList<Maquina> listaMaquina){
listaMaquinas = listaMaquina;
}
#Override
public MaquinaViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_maquinas,parent,false);
return new MaquinaViewHolder(v,mListener);
}
#Override
public void onBindViewHolder(MaquinaViewHolder holder,int position){
Maquina currentItem = listaMaquinas.get(position);
holder.tipo.setText(currentItem.getTipo());
holder.marca.setText(currentItem.getMarca());
holder.modelo.setText(currentItem.getModelo());
holder.serie.setText(currentItem.getSerie());
}
#Override
public int getItemCount(){
return listaMaquinas.size();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/cardview"
android:backgroundTintMode="multiply"
android:elevation="20dp"
card_view:cardBackgroundColor="#color/colorAccent"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="24dp"
card_view:cardMaxElevation="20dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="false">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="93dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/btnbordes"
android:text="Tipo"
android:textAlignment="center"
android:textColor="#3dffb8"
android:textSize="14sp" />
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/btnbordes"
android:text="Marca"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
<TextView
android:id="#+id/t3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/btnbordes"
android:text="Modelo"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
<TextView
android:id="#+id/t4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:background="#drawable/btnbordes"
android:text="Serie"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="227dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tipo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/text"
android:inputType="textMultiLine"
android:isScrollContainer="true"
android:maxLines="4"
android:textAlignment="center"
android:textColor="#3dffb8"
android:textSize="14sp" />
<TextView
android:id="#+id/marca"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/text"
android:inputType="textMultiLine"
android:isScrollContainer="true"
android:maxLines="4"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
<TextView
android:id="#+id/modelo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/text"
android:inputType="textMultiLine"
android:isScrollContainer="true"
android:maxLines="4"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
<TextView
android:id="#+id/serie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:background="#drawable/text"
android:inputType="textMultiLine"
android:isScrollContainer="true"
android:maxLines="4"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is the code of the cardViews that are displayed in the list. I have already reviewed the adapter and the other classes and I think the problem is in the CardView Configuration.
Thank you very much in advance.
this is sample adapter of RecyclerView.Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private ListItemClickListener mOnClickListener;
public interface ListItemClickListener{
void onListItemClick(View view, int postion);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView title;
public CardView cardView;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.tvTitle);
cardView=(CardView)view.findViewById(R.id.card_view);
title.setOnClickListener(this);
cardView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mOnClickListener.onListItemClick(v, getAdapterPosition());
}
}
public void setClickListener(ListItemClickListener itemClickListener) {
this.mOnClickListener = itemClickListener;
}
Now implement Adapter.ListItemClickListener on your activity class and set the setClickListener of your adapter
Adapter.setClickListener(this);
//the intent where to go inside the implemented method

How to get the button in FrameLayout for NavigationDrawer

I m new to NavigationDrawer, the code below is working.
But how to get the button inside the homeLayout.axml which is inflated as indicated in HomeFragment.cs after the below code.
ft.Add(Resource.Id.HomeFrameLayout, new HomeFragment());
ft.Commit();
1) I need to add eventHandler to this btn : btnProducts in homeLayout.axml
- where to add below code
what I need to add to setup code to get the btnProducts and add event for this button??
SetContentView (Resource.Layout.????);
Btn = FindViewById<Button>(Resource.Id.BtnGM);
Btn.Click += Btn_Click1;
--- UI:
Main Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:fitsSystemWindows="true">
<android.support.v4.widget.DrawerLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/drawer_layout">
<LinearLayout
android:id="#+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/app_bar" />
<FrameLayout
android:id="#+id/HomeFrameLayout"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navmenu"
app:headerLayout="#layout/headerdrawerlayout" /> </android.support.v4.widget.DrawerLayout>
</LinearLayout>
-- Code for main UI
public class NaviDrawerActivity : AppCompatActivity
{
private SupportToolbar toolbar;
DrawerLayout drawerLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.NaviDrawer);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
//--- Init toolbar
toolbar = FindViewById<SupportToolbar>(Resource.Id.app_bar);
SetSupportActionBar(toolbar);
SupportActionBar.SetTitle(Resource.String.app_name);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
//--- Attach item selected handler to navigation view
var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
//-- Create ActionBarDrawerToggle button and add it to the toolbar
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
drawerLayout.SetDrawerListener(drawerToggle);
drawerToggle.SyncState();
//--load default home screen
var ft = FragmentManager.BeginTransaction();
ft.AddToBackStack(null);
ft.Add(Resource.Id.HomeFrameLayout, new HomeFragment());
ft.Commit();
}
//---define custom title text
protected override void OnResume()
{
SupportActionBar.SetTitle(Resource.String.app_name);
base.OnResume();
}
-------------HomeFragment.cs
class HomeFragment: Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.homeLayout, container, false);
return view;
}
}
----------homeLayout.axml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff46a2fd"
android:text="Home"
android:textSize="22dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="center"
android:layout_gravity="center"
android:layout_centerInParent="true" />
<Button
android:id="#+id/btnProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_marginTop="3dp"
android:background="#307FC1"
android:text="merchant"
android:layout_alignParentBottom="true"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
in your HomeFragment.cs in the OnCreateView method add the following (between the declaration of view and the return statment):
var btnProducts = view.FindViewById<Button>(Resource.Id.btnProducts);
btnProducts.Click += btnProducts_Click;
Then add the method for your click event (somewhere in your HomeFragment class):
public void btnProducts_Click(object sender, EventArgs e)
{
// Action you want to take upon button click
}

Horizontal RecyclerViews inside vertical RecyclerView scrolling jerks

I am using a layout in which I used multiple RecyclerViews (Horizontal) as a item view of RecyclerView. The problem is that the vertical scrolling is not as smooth as I am expecting.There are some jerks in while scrolling vertically(Parent RecyclerView).
How to remove these vertical scrolling jerks ? I used to set adapters to horizontal RecyclerViews in OnBindViewHolder() method of Parent RecyclerView.
I have solved the problem.
Scrolling performance is much better in this case.
Do not set adapters to horizontal RecyclerViews in OnBindViewHolder() method of Parent RecyclerView.
Instead of it set it at very first time when the view is created via onCreateViewHolder() of RecyclerView with empty or null dataList.
Just replace the new secondary data list with previous null list at onBindViewHolder() and call notifydataSetChanged() to HorizontalAdapetr.
This is much better than setAdapter() in onBindViewHolder().
You can try this way
main activity
public void initialize(List<List<ResponseObject>> responseObjectList) {
RecyclerView upperRecyclerView = (RecyclerView) this.findViewById(R.id.main_layout);
upperRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
VerticalAdapter adapter = new VerticalAdapter(this, responseObjectLists);
upperRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Vertical recycled view adapter
public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.Holder> {
final private SearchActivity activity;
List<List<ResponseObject>> list;
public VerticalAdapter(SearchActivity activity, List<List<ResponseObject>> lists) {
this.list = lists;
this.activity = activity;
}
public Holder onCreateViewHolder(ViewGroup parent,int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vertical_layout, null);
return new Holder(itemLayoutView);
}
public void onBindViewHolder(Holder viewHolder, int position) {
List<ResponseObject> objectList = list.get(position);
viewHolder.packageTitle.setText(objectList.get(0).getTag());
ImageAdapter imageAdapter = new ImageAdapter(activity, objectList);
viewHolder.horizontalRecyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
viewHolder.horizontalRecyclerView.setAdapter(imageAdapter);
viewHolder.horizontalRecyclerView.setNestedScrollingEnabled(false);
imageAdapter.notifyDataSetChanged();
}
public final static class Holder extends RecyclerView.ViewHolder {
protected TextView packageTitle;
protected RecyclerView horizontalRecyclerView;
public Holder(View view) {
super(view);
this.packageTitle = (TextView) view.findViewById(R.id.recycleViewTitle);
this.horizontalRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
this.horizontalRecyclerView.setLayoutManager(new LinearLayoutManager(this.horizontalRecyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
this.horizontalRecyclerView.setNestedScrollingEnabled(false);
horizontalRecyclerView.setAdapter(null);
}
}
public int getItemCount() {
return ListUtil.isEmpty(list) ? 0 : list.size();
}
}
Horizontal recycleview adapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private List<ResponseObject> mainPageResponseList;
private SearchActivity activity;
public ImageAdapter(SearchActivity activity, List mainPageResponseList) {
this.mainPageResponseList = mainPageResponseList;
this.activity = activity;
}
public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.horizontal_layout_view, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, int position) {
ResponseObject object = mainPageResponseList.get(position);
Util.setImageUsingGlide(object.getImage(), viewHolder.imageView);
viewHolder.packageName.setText(object.getDestination());
viewHolder.packagePrice.setText(object.getPrice() + "");
viewHolder.imageView.setOnClickListener(null);
}
public final static class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView packageName;
protected TextView packagePrice;
public ViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.packageImage);
this.packageName = (TextView) view.findViewById(R.id.packageName);
this.packagePrice = (TextView) view.findViewById(R.id.packagePrice);
}
}
public int getItemCount() {
return ListUtil.isEmpty(mainPageResponseList) ? 0 : mainPageResponseList.size();
}
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivHolidayMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<ImageView
android:id="#+id/ivHotelImage"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignTop="#id/ivHolidayMainImage"
android:background="#drawable/gradient_from_up_hotel_image" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="250dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
<TextView
android:id="#+id/mainPackageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular Pick"
android:textColor="#color/white"
android:textSize="12dp" />
<TextView
android:id="#+id/mainPackageName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainPackageTitle"
android:text="Andaman Islands"
android:textColor="#color/white"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/mainPackagePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25000"
android:textColor="#color/white"
android:textSize="18dp" />
<TextView
android:id="#+id/journeyType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainPackagePrice"
android:text="Popular Pick"
android:textColor="#color/white"
android:textSize="12dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="-100dp"
android:background="#drawable/gradient_from_down_hotel_image" />
</app.viaindia.views.ViaLinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/ic_sms_black"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end" />
vertical_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="#+id/recycleViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Beaches"
android:textColor="#color/black_light"
android:textSize="20dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_gravity="center"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
holizontal_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:viaCustom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/packageImage"
android:layout_width="wrap_content"
android:layout_height="230dp"
android:scaleType="fitXY"
android:src="#drawable/cheese_1" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp">
<TextView
android:id="#+id/tvNightAndDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3 Night/4 days"
android:textColor="#color/white" />
</RelativeLayout>
</RelativeLayout>
Try to notifydatasetChanged() in onbindViewHolder() not setAdapter(). SetAdapter() is more time consuming than notifying datset change.

Resources