Skip to content Skip to sidebar Skip to footer

How To Make Linearlayout Scrollable With Two Listviews Scrollable Which Wrap Their Content

I know this has been asked many times but I still have yet to find a solution I really like. Can someone please suggest the most elegant way to do what I'm trying to do: I have two

Solution 1:

As far as I know, you are trying to make the ListView as height as its content requires.

You can create a custom listview which extends ListView and override its onMeasure method like this:

publicclassUnscrollableListViewextendsListView {

    publicUnscrollableListView(Context context) {
        super(context);
    }

    publicUnscrollableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicUnscrollableListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        intmaxHeightSpec= MeasureSpec.makeMeasureSpec( 
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
        super.onMeasure(widthMeasureSpec, maxHeightSpec); 
    }

}

This will make your listview to wrap their content.

As you need to scroll down, try to add a ScrollView in your layout to wrap the LinearLayout. Finally, your layout is something like:

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><com.example.UnscrollableListViewandroid:id="@+id/listview1"android:layout_width="match_parent"android:layout_height="wrap_content" /><com.example.UnscrollableListViewandroid:id="@+id/listview2"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout></ScrollView>

Is it what you want?

But I have to say that these codes make listview to perform just like a vertical linearlayout with a lot of similar children. It could not take advantage of the recycling views to improve layout performance because no view is going to be recycled.

You can have a look on this article: Performance Tips for Android’s ListView

Solution 2:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1" ></ListView><ListViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1" ></ListView></LinearLayout>

Solution 3:

do this way,

listview_main.xml

    ?xml version="1.0" encoding="utf-8"?>
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ScrollViewandroid:id="@+id/scrollview"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/list_view1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="5dp"android:background="@android:color/black" /><LinearLayoutandroid:id="@+id/list_view2"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ></LinearLayout></LinearLayout></ScrollView></LinearLayout>

Adapter1.java(Adapter2 is same as Adapter1)

publicclassAdapter1extendsBaseAdapter {

    private Activity activity;
    private LayoutInflater mInflater;
    private ArrayList<String> arraylist1 = newArrayList<String>();

    publicAdapter1(Activity act, ArrayList<String> array) {

        this.activity = act;
        this.arraylist1 = array;
        mInflater = LayoutInflater.from(activity);
    }

    @OverridepublicintgetCount() {
        // TODO Auto-generated method stubreturn arraylist1.size();
    }

    @Overridepublic Object getItem(int position) {
        return arraylist1.get(position);
    }

    @OverridepubliclonggetItemId(int position) {
        // TODO Auto-generated method stubreturn arraylist1.get(position).hashCode();
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderholder=null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item1, null);

            holder = newViewHolder();

            holder.lablel = (TextView) convertView.findViewById(R.id.label);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.lablel.setText(arraylist1.get(position).toString());

        return convertView;

    }

    privateclassViewHolder {
        TextViewlablel=null;
    }

}

MainActivity.java

publicclassMainActivityextendsActivity {
    private ArrayList<String> arraylist1 = newArrayList<String>();
    private ArrayList<String> arraylist2 = newArrayList<String>();

    privateAdapter1adapter1=null;
    privateAdapter2adapter2=null;

    private LinearLayout listview2;
    private LinearLayout listview1;
    private ScrollView scrollView;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);

        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");
        arraylist1.add("listview_item1");

        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");
        arraylist2.add("listview_item2");

        listview1 = (LinearLayout) findViewById(R.id.list_view1);
        listview2 = (LinearLayout) findViewById(R.id.list_view2);
        scrollView = (ScrollView) findViewById(R.id.scrollview);

        adapter1 = newAdapter1(MainActivity.this, arraylist1);
        adapter2 = newAdapter2(MainActivity.this, arraylist2);

        listview1.setOnTouchListener(newOnTouchListener() {

            @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                scrollView.requestDisallowInterceptTouchEvent(true);
                returnfalse;
            }
        });

        listview1.removeAllViews();
        if (adapter1 != null && adapter1.getCount() > 0) {
            for (inti=0; i < adapter1.getCount(); i++) {

                finalViewconvertView= adapter1.getView(i, null, null);
                if (listview1 != null)
                    listview1.addView(convertView);
            }
        }

        listview2.removeAllViews();
        if (adapter2 != null && adapter2.getCount() > 0) {
            for (inti=0; i < adapter2.getCount(); i++) {

                finalViewconvertView= adapter2.getView(i, null, null);
                if (listview2 != null)
                    listview2.addView(convertView);
            }
        }

    }

}

Post a Comment for "How To Make Linearlayout Scrollable With Two Listviews Scrollable Which Wrap Their Content"