I want to recyclerView on Bottom sheet can scroll but i cant . I try use OntouchListenner in RecyclerView but which can not scroll. Pls help me....
Main layout
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="#efefef"
tools:context=".MainActivity">
<include layout="#layout/conten_main" />
<include layout="#layout/layout_bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/air" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want to know how to Scroll in RecyclerView
Try this custom view
package com.avatus.util
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.LinearLayout
class DisallowInterceptView : LinearLayout {
constructor(context: Context?) : super(context) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
requestDisallowInterceptTouchEvent(true)
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
requestDisallowInterceptTouchEvent(true)
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
parent.requestDisallowInterceptTouchEvent(true)
return super.dispatchTouchEvent(ev)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_MOVE -> requestDisallowInterceptTouchEvent(true)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> requestDisallowInterceptTouchEvent(
false
)
}
return super.onTouchEvent(event)
}
}
<com.avatus.util.DisallowInterceptView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:padding="#dimen/_16sdp"
android:background="#17141A"
android:layout_height="match_parent">
// Your view here
</androidx.constraintlayout.widget.ConstraintLayout>
</com.avatus.util.DisallowInterceptView>
I had the same requirement and solved it like below
Make BottomSheetDialog object attribute set cancelable false like below
viewIndentItemBottomSheetDialog.setCancelable(false)
Related
I have a simple project with 3 fragments and i'm trying to add animations to it transitions.
It navigates when i click the buttons but now i'm trying to add animations to those transitions in the nav graph but for some reason the destination of the actions are not being registered.
This is the code i have already:
Main Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupActionBarWithNavController(findNavController(R.id.fragment))
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
Fragment A:
class FragmentA : Fragment() {
private var _binding: FragmentABinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentABinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val navController : NavController = Navigation.findNavController(view)
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
binding.button2.setOnClickListener {
navController.navigate(R.id.fragmentC)
}
}
}
Main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment A xml:
<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"
android:background="#android:color/holo_purple"
tools:context="com.example.FragmentA">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentB"
android:textColor="#color/black" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentC"
android:textColor="#color/black" />
</LinearLayout>
</FrameLayout>
And the other 2 fragments just have 2 buttons and nothing of logic aplicated yet.
So when i try to create an action for the navigation from Fragment A (Home fragment) to fragment B the desing window normally shows the id of the action and its destinantion but i don't know why it is not registering the destination when i create the action. It looks like this now:
I tried to add the destination manually but it doesn't works either because it just delete it and let it blank again.
Nav Graph xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/fragmentA">
<fragment
android:id="#+id/fragmentA"
android:name="com.example.FragmentA"
android:label="fragment_a"
tools:layout="#layout/fragment_a" >
<action
android:id="#+id/action_fragmentA_to_fragmentB2"
app:destination="#id/fragmentB" />
</fragment>
<fragment
android:id="#+id/fragmentB"
android:name="com.example.FragmentB"
android:label="fragment_b"
tools:layout="#layout/fragment_b" />
<fragment
android:id="#+id/fragmentC"
android:name="com.example.FragmentC"
android:label="fragment_c"
tools:layout="#layout/fragment_c" />
</navigation>
Animation xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%" android:toXDelta="0%" android:duration ="800"/>
</set>
I added this custom animation to enterAnim field but its not taking it.
I don't really know whats going on and i didn't find any answer to this issue.
I found my error and it was on the Fragmnet A code. It was about the click listener.
I had it like:
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
but i need to add the action instead:
binding.button1.setOnClickListener {
findNavController().navigate(R.id.action_fragmentA_to_fragmentB)
}
The destination field is bugged i think, now everything works as it should.
I have this drawer which opens normally but the scrolling in it is very slow, and I don't know why knowing that the image it has on top of it is not HD image, and available in the 4 sizes, I've done various examples in Java and it was fine, I wonder if the issue is that I'm using Kotlin in the adapter.
I would appreciate any help.
The activity's layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background"
android:orientation="vertical">
<include
android:id="#+id/top"
layout="#layout/top_bar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize" />
<TextView
android:layout_below="#+id/top"
android:id="#+id/tvTradesTicker"
android:layout_width="match_parent"
android:layout_height="#dimen/trades_ticker_height"
android:background="#color/colorPrimaryLight"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:focusable="true"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="#dimen/small_margin"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="#color/darkGray"
android:textSize="#dimen/font"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvGrid"
android:layout_below="#+id/tvTradesTicker"
android:layout_above="#+id/llFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/footer"
android:id="#+id/llFooter"
android:layout_width="match_parent"
android:layout_height="#dimen/footer_height"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:fitsSystemWindows="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:paddingTop="#dimen/big_margin_padding"
android:orientation="vertical">
<include
layout="#layout/item_drawer_header"
android:id="#+id/header"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="#dimen/drawer_header_height" />
<ListView
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white"
android:divider="#android:color/transparent"
android:paddingTop="#dimen/small_margin" />
</LinearLayout>
</android.support.design.widget.NavigationView>
The Drawer's Adapter:
class DrawerListAdapter : BaseAdapter {
private var drawerItems: MutableList<DrawerItem> = mutableListOf()
private var context: Context? = null
constructor(context: Context, notesList: MutableList<DrawerItem>) : super() {
this.drawerItems = notesList
this.context = context
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
val view: View?
val vh: ViewHolder
val inflater = LayoutInflater.from(context)
val type = getItemViewType(position)
if (convertView == null) {
view = if (type == 0){
inflater.inflate(R.layout.item_drawer_section, parent, false)
}else{
inflater.inflate(R.layout.item_drawer_child, parent, false)
}
vh = ViewHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ViewHolder
}
val drawerItem = drawerItems[position]
vh.name.text = drawerItem.name
if (type == 0){
vh.rel.setBackgroundColor(ContextCompat.getColor(context!!, R.color.blue))
vh.name.setBackgroundColor(ContextCompat.getColor(context!!, R.color.colorAccent))
vh.name.setTextColor(ContextCompat.getColor(context!!, R.color.white))
}else if (type == 1){
vh.rel.setBackgroundColor(ContextCompat.getColor(context!!, R.color.white))
vh.name.setBackgroundColor(ContextCompat.getColor(context!!, R.color.white))
vh.name.setTextColor(ContextCompat.getColor(context!!, R.color.colorPrimary))
vh.separator.visibility = View.VISIBLE
}else{
vh.rel.setBackgroundColor(ContextCompat.getColor(context!!, R.color.white))
vh.name.setBackgroundColor(ContextCompat.getColor(context!!, R.color.white))
vh.name.setTextColor(ContextCompat.getColor(context!!, R.color.colorPrimary))
vh.separator.visibility = View.GONE
}
Actions.overrideFonts(context!!, vh.rel)
return view
}
override fun getItem(position: Int): DrawerItem {
return drawerItems[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return drawerItems.size
}
override fun getItemViewType(position: Int): Int {
if (drawerItems[position].isHeader)
return 0
else if (!drawerItems[position].isLast)
return 1
else
return 2
}
override fun getViewTypeCount(): Int {
return 3
}
}
private class ViewHolder(view: View) {
val rel: RelativeLayout = view.findViewById(R.id.rel) as RelativeLayout
val name: TextView = view.findViewById(R.id.name) as TextView
val separator = view.findViewById<View>(R.id.separator)
}
What should I change, the activity layout, or the adapter or what i realy have no idea.
EDIT
I set the visibility of the marquee text view in the layout to gone, and everything worked just fine, does anybody know why? because I don't want to replace this text view with a horizontal recycler view
SOLVED.
Changed the approach completely, I stopped the marquee and applied cross fade animation to the text view, with a handler that changes the object being set in the text view at each iteration, and now it looks perfect and way more elegnat
I'm hoping you would help me to get to the problem I'm facing right now.
I created a custom search view using a TextInputEditText, it's really simple actually and works fine on all the Android version with the exception of 5.1(Lollipop), the EditText never hide the hint when write or even show the cursor, also the text is not visible.
I t I have tried everything I know, but I can't figure what could it be.
Thank you so much for your time.
This is my custom class view :
public class CustomSearchView extends RelativeLayout {
#BindView(R.id.csvIcon) ImageView csvIcon;
public #BindView(R.id.csvEditText) TextInputEditText editText;
public CustomSearchView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
}
public CustomSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_search_view, this, true);
ButterKnife.bind(this);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.CustomSearchView,
0, 0);
String hintText = typedArray.getString(R.styleable.CustomSearchView_hintText);
try {
int max = typedArray.getInteger(R.styleable.CustomSearchView_csv_length, 17);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
editText.setInputType(typedArray.getInt(R.styleable.CustomSearchView_android_inputType, InputType.TYPE_CLASS_TEXT));
editText.setHint(hintText);
csvIcon.setImageDrawable(typedArray.getDrawable(R.styleable.CustomSearchView_csv_icon));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
This are the attribute specification :
<declare-styleable name="CustomSearchView">
<attr name="csv_icon" format="reference"/>
<attr name="hintText" format="string"/>
<attr name="csv_length" format="integer"/>
<attr name="android:inputType"/>
<attr name="android:digits"/>
</declare-styleable>
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/charcoal_grey_two">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/csvIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:src="#drawable/icon_search"/>
<android.support.design.widget.TextInputEditText
android:id="#+id/csvEditText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:layout_marginLeft="10dp"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete"
android:textSize="20dp"
android:textColorHint="#color/slate_grey"
android:textColor="#color/slate_grey"
android:background="#android:color/transparent"/>
</LinearLayout>
</RelativeLayout>
And this is my implementation in a fragment :
<?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:clickable="true"
android:background="#color/gunmetal">
<mx.com.segurosbancomer.gpsajustadores.view.custom.TopBarLayout
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tb_color="#color/gunmetal"
app:tb_text="#string/bf_title"
app:tb_text_color="#color/white"
app:tb_btnleft_visibility="0"
app:tb_btnright_visibility="8"
app:tb_btnleft="#drawable/icon_prev_white"
app:tb_btnright="#drawable/icon_dot_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloPoliza"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchPoliza"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_poliza" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchInciso"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="30"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_inciso" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#color/charcoal_grey_two"/>
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomTextView
android:id="#+id/tituloSerie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/bf_search_serie"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_margin="10dp"
app:ctv_customFont="stagsansbook.otf" />
<mx.com.segurosbancomer.gpsajustadores.view.custom.CustomSearchView
android:id="#+id/searchSerie"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:cursorVisible="true"
android:focusableInTouchMode="true"
android:inputType="textAutoComplete"
android:digits="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890"
app:csv_length="17"
app:csv_icon="#drawable/icon_search"
app:hintText="#string/bf_serie"/>
</LinearLayout>
</LinearLayout>
Finally I find the solution. If some else have a similar problem just like me, the issue was resolved by removing this line from the AndroidManifest.xml
android:hardwareAccelerated="false"
This line allow to the fragment and all the components to work perfectly.
You should use TextInputEditText with TextInputLayout wrapped. Just use EditText instead.
See this: https://stackoverflow.com/a/37834022/5558150
In my activity i had to create a custom web view so, i put it and i want to get a FAB in the same activity but i having no success on that. Here is my class code:
package com.mydomain.app;
import android.app.Activity;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;
public class CustomWebView extends WebView {
FloatingActionButton fab;
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
Activity activity = (Activity) context;
fab = (FloatingActionButton) activity.findViewById(R.id.fab);
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(t >= oldt)
fab.hide();
if(t < oldt)
fab.show();
}
}
When the onScrollChanged is fired the app breaks. How can i get this FAB to show or hide it?
Edit 1:
Here is the log:
Edit 2:
Here is the activity that i'm using this class:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/loading_panel"
android:layout_centerInParent="true"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:clickable="true">
<ProgressBar
style="#android:style/Widget.Material.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/corners"
android:id="#+id/progressBar2"
android:indeterminate="true"
android:layout_centerInParent="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="#color/colorAccent" />
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/webViewContainer"
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.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:backgroundTint="#color/colorAccent"
app:fabSize="normal"
android:src="#drawable/ic_apps"
app:layout_anchor="#+id/webView"
android:layout_margin="16dp"
app:layout_anchorGravity="bottom|right|end"
android:minHeight="179dp" />
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.usefashion.useapp.CustomWebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:elevation="0dp"
android:clickable="true">
</com.usefashion.useapp.CustomWebView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
This might be silly question but I didn't understand Design lib well. I am following this reference to create below layout. The Blue area should work as parallax when I scroll the GridView.
But when I scroll grid View nothing happens in AppBarLayout.
But This works with NestedScrollView and RecyclerView
Below is My Layout file-
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:background="#500403"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#122453"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<ImageView
android:id="#+id/backdrop1"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:fitsSystemWindows="true"
android:layout_gravity="center"
android:src="#drawable/bar_offline"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#982223"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<GridView
android:id="#+id/grid"
android:numColumns="4"
android:background="#367723"
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_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/bar_offline"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Any help would be appreciated.
With ListView/GridView, it works only on Lollipop by following code-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
I think for Now CoordinatorLayout works only with RecyclerView and NestedScrollView
EDIT :
use -
ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
A simple solution was added to the support lib:
ViewCompat.setNestedScrollingEnabled(listView,true);
I've tested it with Android Support v4 Library 23.1 and it works well.
Currently the ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21.
To obtain this behavior you have to set:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setNestedScrollingEnabled(true);
}
It is not enough to implement the NestedScrollingChild.
The AbsListView isn't deployed with support libraries , then it depends by the SO running in the device.
You have to override some methods in the AbsListView. For example you can check the onInterceptTouchEvent method.
Inside this code you can see:
case MotionEvent.ACTION_DOWN: {
//......
startNestedScroll(SCROLL_AXIS_VERTICAL);
//....
}
case MotionEvent.ACTION_MOVE: {
//.....
if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
//....
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
//..
stopNestedScroll();
break;
}
This code is only in the implementation of AbsListView v21+.
If you check the AbsListView with API 20 or lower, you will not find any nested scroll reference.
You have to put gridview inside NestedScrollview as usual then you have to add gridview height dynamically. then everything would work good.!!!
<android.support.v4.widget.NestedScrollView
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:layout_gravity="fill_vertical"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/camp_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar"
android:layout_margin="10dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:visibility="visible" >
</GridView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This is working for me.
https://gist.github.com/sakurabird/6868765
I use GridView inside NestedScrollView
For convenience i've created a subclass with the new ViewCompat solution:
public class CoordinatedListView extends ListView {
public CoordinatedListView(Context context) {
super(context);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
ViewCompat.setNestedScrollingEnabled(this, true);
}
}
Enjoy :)