Scroll not working with Multiple GridView in a ScrollView - android

I am trying to display multiple GridView in a LinearLayout and ScrollView but if the content of the first scroll view is more then the first GridView fills the screen and the scroll is not working. here is my current layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/owngroup_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/own_groups"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/GridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</GridView>
<TextView
android:id="#+id/ingroup_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/in_groups"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/inGroups"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</GridView>
</LinearLayout>
</ScrollView>
</FrameLayout>

Change android:layout_width="match_parent" of the GridView and test a value.
Smae action with the LinearLayout.
Example : android:layout_width="400dp"

parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});`
here chield scroll view can be replace with your gridview

Related

How to disable Scrollview

I have ScrollView and inside that a TabLayout. How can I disable the ScrollView from scrolling without effecting on TabLayout scroll?
EDIT:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollview"
android:background="#color/backgroundView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="none"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/slider_height"
android:background="#drawable/border_light"
>
app:pv_progressStyle="#style/LightInColorCircularProgressView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/main_slider"
android:layout_width="match_parent"
android:layout_height="#dimen/slider_height"
/>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/AndroidImageSlider_Corner_Oval"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
</RelativeLayout>
.... other stuff
<com.rey.material.widget.TabPageIndicator
style="#style/TabPageIndicator"
android:id="#+id/main_tpi"
android:layout_height="48dp"
android:layout_width="match_parent"
android:paddingLeft="0dp"
android:clipToPadding="true"
app:tpi_mode="scroll"
app:tpi_indicatorColor="#color/colorPrimary"
android:background="#color/overlayView"/>
<adapters.CustomViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_vp"
/>
</ScrollView>
I used this and it worked for me :
// Get the ScrollView
final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);
// Disable Scrolling by setting up an OnTouchListener to do nothing
myScroll.setOnTouchListener( new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
// Enable Scrolling by removing the OnTouchListner
tvDisplayScroll.setOnTouchListener(null);
happy programming......!!!

Get the height of dynamically added LinearLayouts in Android

I am working in an android application and I am adding a Linear Layout dynamically in my Scollview. Now I want to get the height of each Linear Layout added dynamically . Below is my code and Xml.
LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List<String> mSempLog = new ArrayList<String>();
mSempLog.add("TEST1");
mSempLog.add("TEST2");
mSempLog.add("TEST3");
mSempLog.add("TEST4");
mSempLog.add("TEST5");
mSempLog.add("TEST6");
mSempLog.add("TEST7");
mSempLog.add("TEST8");
for (int i = 0; i < mSempLog.size(); i++) {
LinearLayout service_row = (LinearLayout) inflater.inflate(
R.layout.tesffff, null);
TextView dd = ((TextView) service_row.findViewById(R.id.test1));
dd.setText(mSempLog.get(i));
dd.setTag(i);
((TextView) service_row.findViewById(R.id.test2)).setText(mSempLog
.get(i));
layout.addView(service_row, i);
}
ScrollView parentScrollView = (ScrollView) findViewById(R.id.parentscroll);
ScrollView childScrollView = (ScrollView) findViewById(R.id.subscroll);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.subscroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentscroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="#+id/subscroll"
android:layout_width="match_parent"
android:layout_height="200dp" >
<LinearLayout
android:id="#+id/subscrollLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
<TextView
android:id="#+id/test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfsdfsdf" />
</LinearLayout>
</ScrollView>
Row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="2" >
<TextView
android:id="#+id/test1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:clickable="true"
android:onClick="onTest1"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/test2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:onClick="onTest2"
android:textColor="#android:color/black" />
</LinearLayout>
This worked for me
MyListViewObj.post(new Runnable(){
public void run(){
int height = MyListViewObj.getHeight();
}
});
Actually you can get width by few ways.
case 1: yourView.getHeight() (only when the view was already drawn)
case 2: yourView.getLayoutParams().height (depends on LayoutParams)
case 3: http://www.limbaniandroid.com/2014/10/viewtreeobserver-how-to-get-layout.html
case 4: If you have custom layout then you can get height in onMeasure (http://developer.android.com/reference/android/view/View.html#onMeasure(int,
int))

How to add a ontouchlistener or clicklistener on LinearLayout?

I added a ontouchlistener on linearlayout which include a TextView and a ImageView.When I touched the TextView part it works,and the ImageView doesn't work.I want all the linearlayout can get the touch event.What should I do? My XML is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#drawable/graytitle_bj1_black"
android:gravity="center|bottom"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/nav_boutique"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="2dip"
android:clickable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="blocksDescendants">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/description"
android:src="#drawable/btn_jingpin_icon"
android:background="#FFF"
android:focusable="false"
android:clickable="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:text="#string/bottom_jingpin"/>
<View
android:id="#+id/nav_boutique_line"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#F00"/>
</LinearLayout>
<View
android:layout_width="1dip"
android:layout_height="fill_parent"/>
<LinearLayout
android:id="#+id/nav_fenlei"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="2dip"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/description"
android:src="#drawable/icon_fenlei"
android:background="#00000000"
android:focusable="false"
android:clickable="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:text="#string/bottom_fenlei"/>
<View
android:id="#+id/nav_fenlei_line"
android:layout_width="fill_parent"
android:layout_height="2dip"/>
</LinearLayout>
<View
android:layout_width="1dip"
android:layout_height="fill_parent"/>
<LinearLayout
android:id="#+id/nav_dingyue"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="2dip"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/description"
android:src="#drawable/btn_zizhudingyue_icon"
android:background="#00000000"
android:clickable="true"
android:focusable="false"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:text="#string/bottom_dingyue"/>
<View
android:id="#+id/nav_dingyue_line"
android:layout_width="fill_parent"
android:layout_height="2dip"/>
</LinearLayout>
<View
android:layout_width="1dip"
android:layout_height="fill_parent"/>
<LinearLayout
android:id="#+id/nav_sousuo"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="2dip"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/description"
android:src="#drawable/icon_sousuo"
android:background="#00000000"
android:focusable="false"
android:clickable="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:text="#string/bottom_sousuo"/>
<View
android:id="#+id/nav_sousuo_line"
android:layout_width="fill_parent"
android:layout_height="2dip"/>
</LinearLayout>
</LinearLayout>
And this is my code:
dingyueLL.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
setFlagTrue(Navigation.Dingyue.getPosition());
setBtnLine();
return false;
}
});
Create custom linear layout with onintercepttouchevent.
public class MyLayout extends LinearLayout {
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// do whatever you want with the event
// and return true so that children don't receive it
return true;
}
}
check this thread.
http://developer.android.com/training/gestures/viewgroup.html
adding touch listener for liner layout filled with buttons
What i would try is to explicitly add a touch listener to the image, and see what happend. in this listener, dont call the supper, and return false, so the event will propagate to the next listener, hopefully the layout
dingyueLL.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
setFlagTrue(Navigation.Dingyue.getPosition());
setBtnLine();
return false;
}
});
THEIMAGE.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
also, put a breakpoint in the ontouch function of the listener of the image, to see if it is getting there

How to stop scrolling when the Custom view has touchlisteners in android?

I am dynamically adding an Custom Views to the relative layouts.
In my custom View,I have an Child view which can be dragged left and right.Its all working fine with only one view in the Relative layout.But when there are multiple views ACTION_UP events are not called for the dragging child views. so, I want to stop the scrolling when user touches the Dragging childView.
Main xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<com.example.dynamicscroll.ObservableScrollView
android:id="#+id/mainScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="wrap_content" android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</HorizontalScrollView>
</com.example.dynamicscroll.ObservableScrollView>
</RelativeLayout>
inflatedfeedmodebloops.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ababab"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/transparent"
>
<com.example.dynamicscroll.ConstrainedDragAndDropView
android:id="#+id/dndView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/like_small"
android:id="#+id/dummy"
android:visibility="gone"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/images">
<!-- left circle -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:id="#+id/half_left">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/like_small"
android:id="#+id/like_image"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/like_image"
android:text="36"
android:textSize="25dp"
android:textColor="#DF013A"
android:id="#+id/likes_count"
/>
</RelativeLayout>
<!-- end of left circle -->
<!-- <ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/img_1"
android:background="#drawable/pinkcircle"
android:id="#+id/full_left"
android:layout_centerVertical="true"
android:visibility="gone"
/>
-->
<!-- right circle -->
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/backbtn"
android:layout_centerInParent="true"/>
<!-- end of right circle -->
<!-- cartoon image -->
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp" ----->I want to drag this image,so I want to stop
scrolling when user touches this view.
android:layout_centerHorizontal="true"
android:background="#drawable/img_1"
android:layout_marginTop="20dp"
android:id="#+id/cartoon_image">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/like_small"
android:layout_centerInParent="true"
android:id="#+id/ivheart"
android:visibility="gone"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/play_btn"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:id="#+id/play_btn"
>
</RelativeLayout>
</RelativeLayout>
<!-- end of cartoon image -->
</RelativeLayout>
</com.example.dynamicscroll.ConstrainedDragAndDropView>
</RelativeLayout>
</LinearLayout>
For adding Views to Relative Layout:
for(int i=0;i<al_new.size();i++)
{
Log.e("inflating the bloops ","....................");
final View view= getLayoutInflater().inflate(R.layout.inflatedfeedmodebloops, null);
iv= (ImageView)view.findViewById(R.id.main_img);
ivLike=(ImageView)view.findViewById(R.id.like_image);
txtlikesCount=(TextView)view.findViewById(R.id.likes_count);
loader.DisplayImage(al_new.get(i).replace(" ", "%20"), iv);
ConstrainedDragAndDropView dndView = (ConstrainedDragAndDropView)view.findViewById(R.id.dndView);
dndView.setDragHandle(view.findViewById(R.id.cartoon_image),view.findViewById(R.id.half_left),heart);
dndView.setAllowVerticalDrag(false,ScrollingFeedmode.this);
rl.addView(view);
if (view.findViewById(R.id.cartoon_image)!= null && myscrollview!= null) {
view.findViewById(R.id.cartoon_image).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mainScrollView.getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
}
}
So,can anyone suggest me how can I stop scrolling when user touches the childview.
Use the following code to stopping parent view scrolling while touching childview
if (childView != null && parentView != null) {
childView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.parent_view).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
}

android masking a layout with other layout

I am using a progress bar in a matched parent layout in my application. Problem is button in the background is still clickable while the progress bar runs. They should not be clickable, so how do I make them non-clickable. The layout is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black_with_fifty_percent_alpha"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_tv_progress_bar_bultenler"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
Try set this to this layout:
OnTouchListener listener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;//consume the touch event
}
};
yourLayout.setOnTouchListener(listener);

Categories

Resources