Android Non Scrollable listview example

Android ListView has a default property of scroll. If you are not familiar with how to implement a listview , please refer Android Listview Example tutorial first. Suppose if we want to give listview with some other views inside a scrollview , a problem will come like the scrolling gestures wont work properly. For resolving such kind of issues in those requirements we can use a Custom listview which is a Non Scrollable listview. In this tutorial we will learn how to implement a non scrollable listview in android.

First step is to create a Custom Listview class which extends Android Listview, Create a class in your package and use the below code in it.

NonScrollListView.java
public class NonScrollListView extends ListView {

public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}

Now simply use the below code in layout file as a replacement of your normal listview.

<com.yourpackage.NonScrollListView
android:id="@+id/lv_topics"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.yourpackage.NonScrollListView>

Note: While referring this listview in your class file you need to use NonScrollListView instead of Listview like mentioned below

NonScrollListView listview =  (NonScrollListView) findViewById(R.id.listview);

Now you can use this listview wherever required.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *