web-dev-qa-db-ja.com

ScrollView内のListViewはAndroidでスクロールしていません

ListView内でのScrollViewのスクロールに問題があります。上部にいくつかのEditTextがあるアクティビティがあり、次にそれぞれ1つのListViewを持つ2つのタブを持つタブホストがあります。 EditTextビューがフォーカスされると、ソフトキーボードが表示され、ScrollViewがあるので、コンテンツはスクロール可能です。しかし、問題は、リストビュー(タブに1つ)にさらにアイテムがある場合に発生し、アイテムがさらにある場合でも、リストビューをスクロールできません。

以下は、レイアウトXMLです。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:background="?backgroundImage"
    Android:orientation="vertical">

  <LinearLayout
        Android:orientation="horizontal"
        Android:layout_width="fill_parent"
        Android:layout_height="50dip"
        Android:layout_alignParentBottom="true"
        Android:layout_margin="10dip"
        Android:id="@+id/buttons">
    <Button
            Android:text="Save"
            Android:layout_margin="2dip"
            Android:textSize="20dip"
            Android:id="@+id/btnSaveorUpdate"
            Android:layout_height="wrap_content"
            Android:layout_width="145dip"></Button>
    <Button
            Android:text="Cancel"
            Android:layout_margin="2dip"
            Android:textSize="20dip"
            Android:id="@+id/btnCancelorDelete"
            Android:layout_height="wrap_content"
            Android:layout_width="145dip"></Button>
  </LinearLayout>
  <ScrollView
        Android:layout_above="@id/buttons"
        Android:layout_width="fill_parent"
        Android:layout_height="fill_parent"
        Android:fillViewport="true"
        Android:layout_margin="10dip">
    <LinearLayout
            Android:orientation="vertical"
            Android:layout_width="fill_parent"
            Android:layout_height="fill_parent"
            Android:layout_margin="10dip">
      <TextView
                Android:text="Bill details"
                Android:textColor="?textColorDark"
                Android:layout_alignParentTop="true"
                Android:id="@+id/txtEnterDetails"
                Android:textSize="25dip"
                Android:textStyle="bold"
                Android:layout_height="wrap_content"
                Android:layout_width="fill_parent"
                Android:layout_marginBottom="2dip"></TextView>
      <LinearLayout
                Android:focusable="true"
                Android:focusableInTouchMode="true"
                Android:layout_width="0dip"
                Android:layout_height="0dip" />
      <EditText
                Android:layout_width="fill_parent"
                Android:hint="Enter data"
                Android:inputType="numberDecimal"
                Android:id="@+id/txtSample"
                Android:textSize="@dimen/editText"
                Android:layout_height="@dimen/editTextHeight"
                Android:text=""></EditText>
      <EditText
                Android:layout_width="fill_parent"
                Android:id="@+id/txtDescription"
                Android:hint="Enter description"
                Android:textSize="@dimen/editText"
                Android:layout_height="@dimen/editTextHeight"
                Android:inputType="text"
                Android:text=""></EditText>
      <EditText
                Android:layout_width="fill_parent"
                Android:id="@+id/txtComment"
                Android:hint="Enter comment (if any)"
                Android:textSize="@dimen/editText"
                Android:layout_height="@dimen/editTextHeight"
                Android:inputType="text"
                Android:text=""></EditText>
      <LinearLayout
                Android:orientation="horizontal"
                Android:layout_width="fill_parent"
                Android:layout_height="wrap_content">
        <TextView
                    Android:id="@+id/txtDate"
                    Android:layout_width="wrap_content"
                    Android:text=""
                    Android:textSize="20dip"
                    Android:textColor="?textColorDark"
                    Android:layout_marginLeft="10dip"
                    Android:layout_height="@dimen/editTextHeight"
                    Android:layout_gravity="center_vertical" />
        <Button
                    Android:id="@+id/btnPickDate"
                    Android:layout_width="wrap_content"
                    Android:layout_height="@dimen/editTextHeight"
                    Android:text="Select date"
                    Android:layout_margin="2dip"
                    Android:textSize="15dip"
                    Android:layout_gravity="center_vertical" />
      </LinearLayout>
      <TabHost
                Android:id="@+id/tabhost"
                Android:layout_width="fill_parent"
                Android:layout_height="fill_parent">
        <LinearLayout
                    Android:id="@+id/linearLayout1"
                    Android:layout_width="fill_parent"
                    Android:layout_height="fill_parent"
                    Android:orientation="vertical">
          <TabWidget
                        Android:layout_width="fill_parent"
                        Android:layout_height="wrap_content"
                        Android:id="@Android:id/tabs"></TabWidget>
          <FrameLayout
                        Android:layout_width="fill_parent"
                        Android:layout_height="fill_parent"
                        Android:id="@Android:id/tabcontent">
            <ScrollView
                            Android:layout_above="@id/buttons"
                            Android:layout_width="fill_parent"
                            Android:layout_height="fill_parent"
                            Android:fillViewport="true"
                            Android:id="@+id/tab1">
              <LinearLayout
                                Android:layout_width="fill_parent"
                                Android:layout_height="wrap_content"
                                Android:orientation="vertical">
                <TableLayout
                                    Android:layout_height="wrap_content"
                                    Android:layout_width="fill_parent">
                  <TableRow
                                        Android:id="@+id/tableRow1"
                                        Android:layout_marginLeft="2dip"
                                        Android:layout_marginRight="5dip"
                                        Android:layout_width="fill_parent"
                                        Android:layout_height="wrap_content">
                    <LinearLayout
                                            Android:layout_width="fill_parent"
                                            Android:layout_height="wrap_content"
                                            Android:orientation="horizontal">
                      <ImageView
                                                Android:src="@drawable/ic_menu_invite"
                                                Android:layout_width="40dip"
                                                Android:layout_height="40dip"
                                                Android:layout_gravity="center_vertical"></ImageView>
                      <TextView
                                                Android:text="Add friend"
                                                Android:layout_height="wrap_content"
                                                Android:layout_width="fill_parent"
                                                Android:layout_centerVertical="true"
                                                Android:textColor="?textColorDark"
                                                Android:textSize="@dimen/editText"
                                                Android:layout_gravity="center_vertical" />
                    </LinearLayout>
                  </TableRow>
                  <TableRow
                                        Android:layout_width="fill_parent"
                                        Android:layout_height="wrap_content"
                                        Android:layout_marginLeft="5dip"
                                        Android:layout_marginRight="5dip">
                    <TextView
                                            Android:id="@+id/txtData1"
                                            Android:layout_width="170dip"
                                            Android:layout_height="wrap_content"
                                            Android:text="Data"
                                            Android:textSize="14dip"
                                            Android:textStyle="bold"
                                            Android:textColor="#000000">
                    </TextView>
                    <TextView
                                            Android:id="@+id/txtData2"
                                            Android:layout_width="wrap_content"
                                            Android:layout_height="wrap_content"
                                            Android:text="Sample"
                                            Android:textSize="13dip"
                                            Android:textColor="#000000"></TextView>

                  </TableRow>
                </TableLayout>
                <ListView
                                    Android:cacheColorHint="#00000000"
                                    Android:id="@+id/ListView01"
                                    Android:layout_height="wrap_content"
                                    Android:layout_width="fill_parent" />
              </LinearLayout>
            </ScrollView>
            <LinearLayout
                            Android:layout_width="wrap_content"
                            Android:layout_height="wrap_content"
                            Android:gravity="center_horizontal"
                            Android:id="@+id/tab2"
                            Android:orientation="vertical">
              <TableLayout
                                Android:layout_height="wrap_content"
                                Android:layout_width="fill_parent">
                <TableRow
                                    Android:id="@+id/tableRow2"
                                    Android:layout_marginLeft="2dip"
                                    Android:layout_marginRight="5dip"
                                    Android:layout_width="fill_parent"
                                    Android:layout_height="wrap_content">
                  <LinearLayout
                                        Android:layout_width="fill_parent"
                                        Android:layout_height="wrap_content"
                                        Android:orientation="horizontal">
                    <ImageView
                                            Android:src="@drawable/ic_menu_invite"
                                            Android:layout_width="40dip"
                                            Android:layout_height="40dip"
                                            Android:layout_gravity="center_vertical"></ImageView>
                    <TextView
                                            Android:text="Sample"
                                            Android:layout_height="wrap_content"
                                            Android:layout_width="fill_parent"
                                            Android:layout_centerVertical="true"
                                            Android:textColor="?textColorDark"
                                            Android:textSize="@dimen/editText"
                                            Android:layout_gravity="center_vertical" />
                  </LinearLayout>
                </TableRow>
                <TableRow
                                    Android:layout_width="fill_parent"
                                    Android:layout_height="wrap_content"
                                    Android:layout_marginLeft="5dip"
                                    Android:layout_marginRight="5dip">
                  <TextView
                                        Android:id="@+id/txtUser1"
                                        Android:layout_width="170dip"
                                        Android:layout_height="wrap_content"
                                        Android:text="User"
                                        Android:textSize="14dip"
                                        Android:textStyle="bold"
                                        Android:textColor="#000000">
                  </TextView>
                  <TextView
                                        Android:id="@+id/txtUserData"
                                        Android:layout_width="wrap_content"
                                        Android:layout_height="wrap_content"
                                        Android:text="UserData"
                                        Android:textSize="13dip"
                                        Android:textColor="#000000"></TextView>
                </TableRow>
              </TableLayout>

              <ListView
                                Android:cacheColorHint="#00000000"
                                Android:id="@+id/ListView02"
                                Android:layout_height="wrap_content"
                                Android:layout_width="fill_parent" />

            </LinearLayout>


          </FrameLayout>
        </LinearLayout>
      </TabHost>
    </LinearLayout>
  </ScrollView>
</RelativeLayout>

誰が問題がここにあるのか教えてもらえますか? ScrollView内部のListViewに関する別の投稿がありますが、私の場合は役に立ちませんでした。

149
Ashwani K

ListViewクラスは独自のスクロールを実装しているため、ScrollViewの内部にListViewを配置しないでください。これらはすべて親ScrollViewによって処理されるため、ジェスチャを受信しません。レイアウトを何らかの形で簡素化することを強くお勧めします。たとえば、スクロールするビューをヘッダーまたはフッターとしてListViewに追加できます。

UPDATE

APIレベル21(Lollipop)から、ネストされたスクロールコンテナはAndroid SDKによって公式にサポートされています。この機能を提供するViewクラスとViewGroupクラスのメソッドがたくさんあります。 Lollipopでネストされたスクロールを機能させるには、Android:nestedScrollingEnabled="true"をXML宣言に追加するか、setNestedScrollingEnabled(true)を明示的に呼び出して、子スクロールビューで有効にする必要があります。

Lollipop以前のデバイスでネストされたスクロールを機能させる場合は、おそらくサポートライブラリの対応するユーティリティクラスを使用する必要があります。まず、ScrollViewNestedScrollView に置き換える必要があります。後者は NestedScrollingParentNestedScrollingChild の両方を実装しているため、親または子のスクロールコンテナとして使用できます。

ただし、ListViewはネストされたスクロールをサポートしていないため、サブクラス化してNestedScrollingChildを実装する必要があります。幸いなことに、サポートライブラリは NestedScrollingChildHelper クラスを提供するため、このクラスのインスタンスを作成し、ビュークラスの対応するメソッドからそのメソッドを呼び出すだけです。

161
Michael

うまく機能し、ListViewを問題なくスクロールできるソリューションを見つけました。

ListView lv = (ListView)findViewById(R.id.myListView);  // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

これにより、TouchEventScrollViewsが無効になり、ListViewがインターセプトされます。シンプルであり、常に機能します。

298
Moisés Olmedo

解決策も1つあります。私は常にこの方法を使用します。これを試して

<ScrollView
    Android:id="@+id/createdrill_scrollView"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent" >


        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginLeft="20dp"
            Android:layout_marginRight="20dp"
            Android:layout_marginTop="15dp" >

            <net.thepaksoft.fdtrainer.NestedListView
                Android:id="@+id/crewList"
                Android:layout_width="0dip"
                Android:layout_height="wrap_content"
                Android:layout_marginBottom="2dp"
                Android:layout_weight="1"
                Android:background="@drawable/round_shape"
                Android:cacheColorHint="#00000000" >
            </net.thepaksoft.fdtrainer.NestedListView>
        </LinearLayout>

        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginLeft="20dp"
            Android:layout_marginRight="20dp"
            Android:layout_marginTop="15dp" >

            <net.thepaksoft.fdtrainer.NestedListView
                Android:id="@+id/benchmarksList"
                Android:layout_width="0dip"
                Android:layout_height="wrap_content"
                Android:layout_marginBottom="2dp"
                Android:layout_weight="1"
                Android:background="@drawable/round_shape"
                Android:cacheColorHint="#00000000" >
            </net.thepaksoft.fdtrainer.NestedListView>
        </LinearLayout>

</ScrollView>

NestedListView.Javaクラス:

public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}
140

<ScrollView ></ScrollView>Custom ScrollViewのようにこの<com.tmd.utils.VerticalScrollview > </com.tmd.utils.VerticalScrollview >に置き換えるだけです

package com.tmd.utils;

import Android.content.Context;
import Android.util.AttributeSet;
import Android.util.Log;
import Android.view.MotionEvent;
import Android.widget.ScrollView;

public class VerticalScrollview extends ScrollView{

    public VerticalScrollview(Context context) {
        super(context);
    }

     public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
         return true;
    }
}
47
Atul Bhardwaj
list.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @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;
    }
});
11
Salman Ashraf

私は同じ問題を抱えており、グーグル検索中にあなたの質問を見つけました。はいマークされた答えも私のために働いたが、いくつかの問題があった。
とにかく、別の ソリューション を見つけました。これは、手品をすることなく完全に機能します。

11
Priyank

以下は、ScrollviewでListviewを使用する正確な方法です。タッチイベントを処理する必要があるのはすべてです。

      lstvNextTracks.setOnTouchListener(new OnTouchListener()
      {
          @Override
          public boolean onTouch(View v, MotionEvent event)
          {
         Log.e("Lisview *************", "focused");
         SCView.requestDisallowInterceptTouchEvent(true);
         return false;
          }
      });




    SCView.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            int arr[] = new int[] { 1, 2 };
            lstvNextTracks.getLocationOnScreen(arr);

            /* Get bounds of child Listview*/
            int lstvTop = arr[0];
            int lstvBottom = arr[1] + lstvNextTracks.getHeight();
            int lstvLeft = arr[1];
            int lstvRight = arr[0] + lstvNextTracks.getWidth();

            float x = event.getRawX();
            float y = event.getRawY();

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                /*check if child ListView bounds are touched*/
                if (x > lstvTop && x < lstvBottom && y > lstvLeft && y < lstvRight)
                {
                    SCView.clearFocus();
                    /*This statement tells the ScrollView to do not handle this touch event, so the child Listview will handle this touch event and will scroll */
                    SCView.requestDisallowInterceptTouchEvent(true);
                    /*The child Listview isFocusable attribute must be set to true otherwise it will not work*/
                    lstvNextTracks.requestFocus();
                    return true;
                } else
                    return false;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE)
            {

                if (x > lstvTop && x < lstvBottom && y > lstvLeft && y < lstvRight)
                {
                    SCView.clearFocus();
                    SCView.requestDisallowInterceptTouchEvent(true);
                    lstvNextTracks.requestFocus();
                    return true;
                } else
                    return false;
            } else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                SCView.clearFocus();
                SCView.requestDisallowInterceptTouchEvent(true);
                lstvNextTracks.requestFocus();
                return false;
            } else
            {
                return false;
            }
        }
    });
6
Muhammad Bilal

次の方法を使用してお楽しみください!

private void setListViewScrollable(final ListView list) {
    list.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            listViewTouchAction = event.getAction();
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                list.scrollBy(0, 1);
            }
            return false;
        }
    });

    list.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                list.scrollBy(0, -1);
            }
        }
    });
}

listViewTouchActionはグローバルな整数値です。行を交換できる場合

list.scrollBy(0, 1);

他の何かと私たちと共有してください。

楽しい!

6
Bobs

2つの異なるスクロールビューを一緒にするのは悪い習慣です。 ListView自体には独自のスクロール機能があり、行項目のアダプタ設定に従って高さが自動調整されます(レイアウトxmlでListViewに特定の高さを設定していないことに注意してください)。ただし、あなたの場合、ListViewをクラスに置き換えることができます。これにより、ListViewがScrollView内にあることに留意して、ListViewの高さを調整します。

これはあなたを助けます:

public class ExpandableHeightListview extends ListView
{

    boolean expanded = false;

    public ExpandableHeightListview(Context context)
    {
        super(context);
    }

    public ExpandableHeightListview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

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

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT Android!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

このクラスでListViewを拡張し、ListViewタグをリソースxmlからこのようにスモッシングに変更するだけです。

<yourpackagename.ExpandableHeightListview
            Android:id="@+id/listView"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:background="@color/White"
            Android:scrollbars="none"
            Android:orientation="vertical"
            Android:fadingEdge="none">
            </cyourpackagename.ExpandableHeightListview>

これにより、親のスクロールがListViewではなくScrollViewによって管理されるため、ListViewのスクロールの問題が解決されます。

4
Ravi Kabra

同様の問題を抱えており、ListViewで拡張してカスタムクラスを作成することで解決しました。

ScrollableListView.Java

import Android.content.Context;
import Android.util.AttributeSet;
import Android.widget.ListView;

public class ScrollableListView extends ListView {

    public ScrollableListView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollableListView (Context context) {
        super(context);
    }

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

使用法:

<com.my.package.ScrollableListView
    Android:id="@+id/listview"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"/>
3
Waqar UlHaq

上記のほぼすべての方法を試し、テストしましたが、RecyclerViewから完全に逃げた後、ListViewをRecyclerViewに置き換えて完全に機能しました。 ExtendedHeightListViewおよびすべてのサードパーティライブラリは必要ありません。単純でシンプルなRecyclerViewです。

それで、recyclerViewの前に私のレイアウトファイルをHeres:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:id="@+id/scrollView"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:elevation="5dp">

<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:id="@+id/relativeLayoutre"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:layout_margin="2dp"
    tools:context="com.example.Android.udamovappv3.activities.DetailedActivity">

    <Android.support.v7.widget.Toolbar
        Android:id="@+id/my_toolbar_detail"
        Android:layout_width="match_parent"
        Android:layout_height="56dp"
        Android:layout_gravity="top"
        Android:background="?attr/colorPrimary"
        Android:elevation="4dp"
        Android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <TextView
        Android:id="@+id/title_name"
        Android:layout_width="fill_parent"
        Android:layout_height="128dp"
        Android:layout_alignParentStart="true"
        Android:layout_marginTop="59dp"
        Android:background="#079ED9"
        Android:gravity="left|center"
        Android:padding="25dp"
        Android:text="Name"
        Android:textColor="#ffffff"
        Android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/my_toolbar_detail"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="0dp" />


    <ImageView
        Android:id="@+id/iv_poster"
        Android:layout_width="131dp"
        Android:layout_height="163dp"
        Android:layout_alignStart="@+id/my_toolbar_detail"
        Android:layout_below="@+id/title_name"
        Android:layout_marginBottom="15dp"
        Android:layout_marginRight="8dp"
        Android:layout_marginTop="15dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <Button
        Android:id="@+id/bt_mark_as_fav"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignBottom="@+id/iv_poster"
        Android:layout_alignParentEnd="true"
        Android:layout_marginBottom="11dp"
        Android:layout_marginEnd="50dp"
        Android:background="#079ED9"
        Android:text="Mark As \n Favorite"
        Android:textSize="10dp" />

    <TextView
        Android:id="@+id/tv_runTime"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignStart="@+id/tv_rating"
        Android:layout_below="@+id/tv_releaseDate"
        Android:layout_marginTop="11dp"
        Android:text="TextView"
        Android:textSize="20sp" />

    <TextView
        Android:id="@+id/tv_releaseDate"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignStart="@+id/tv_runTime"
        Android:layout_alignTop="@+id/iv_poster"
        Android:text="TextView"
        Android:textSize="25dp" />

    <TextView
        Android:id="@+id/tv_rating"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignStart="@+id/bt_mark_as_fav"
        Android:layout_below="@+id/tv_runTime"
        Android:layout_marginTop="11dp"
        Android:text="TextView" />

    <TextView
        Android:id="@+id/tv_overview"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_below="@+id/iv_poster"
        Android:layout_centerHorizontal="true"
        Android:layout_marginBottom="5dp"
        Android:foregroundGravity="center"
        Android:text="adasdasdfadfsasdfasdfasdfasdfnb agfjuanfalsbdfjbdfklbdnfkjasbnf;kasbdnf;kbdfas;kdjabnf;lbdnfo;aidsnfl';asdfj'plasdfj'pdaskjf'asfj'p[asdfk"
        Android:textColor="#000000"
         />


    <RelativeLayout
        Android:id="@+id/foodItemActvity_linearLayout_fragments"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical"
        Android:layout_below="@+id/tv_overview">
        <TextView
            Android:id="@+id/fragment_dds_review_textView_label"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:text="Reviews:"

            Android:textAppearance="?android:attr/textAppearanceMedium" />
        <com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
            Android:id="@+id/expandable_listview"
            Android:layout_width="fill_parent"
            Android:layout_height="match_parent"
            Android:layout_alignParentBottom="true"
            Android:layout_alignParentStart="true"
            Android:layout_below="@+id/fragment_dds_review_textView_label"
            Android:padding="8dp">

        </com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView>
    </RelativeLayout>
</RelativeLayout>
</ScrollView>

このISは、上記の多くのソリューションのいずれかでリストビューを置き換えた後。したがって、問題は、Androidの2つのscrollviewバグ(おそらくバグではない)のために、リストビューが適切に動作していないことでした。

最終的なレイアウトを形成するために、リサイクラービューに置き換えました。

これは私のリサイクルビューアダプタです。

public class TrailerAdapter extends RecyclerView.Adapter<TrailerAdapter.TrailerAdapterViewHolder> {

private ArrayList<String> Youtube_URLS;

private Context Context;

public TrailerAdapter(Context context, ArrayList<String> Youtube_URLS){
    this.Context = context;
    this.Youtube_URLS = Youtube_URLS;
}
@Override
public TrailerAdapter.TrailerAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trailer_layout, parent, false);
    return new TrailerAdapterViewHolder(view);
}

@Override
public void onBindViewHolder(TrailerAdapter.TrailerAdapterViewHolder holder, int position) {
    Picasso.with(Context).load(R.drawable.ic_play_arrow_black_24dp).into(holder.iv_playbutton);
    holder.item_id.setText(Youtube_URLS.get(position));
}

@Override
public int getItemCount() {
    if(Youtube_URLS.size()==0){
        return 0;
    }else{
        return Youtube_URLS.size();
    }
}

public class TrailerAdapterViewHolder extends RecyclerView.ViewHolder {
    ImageView iv_playbutton;
    TextView item_id;

    public TrailerAdapterViewHolder(View itemView) {
        super(itemView);
        iv_playbutton = (ImageView)itemView.findViewById(R.id.play_button);
        item_id = (TextView)itemView.findViewById(R.id.tv_trailer_sequence);
    }
}
}

そして、これは私のRecyclerViewカスタムレイアウトです:

<RelativeLayout
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="56dp"
    Android:padding="6dip" >

    <ImageView
        Android:id="@+id/play_button"
        Android:layout_width="70dp"
        Android:layout_height="wrap_content"
        Android:layout_marginRight="6dip"
        Android:layout_marginStart="12dp"
        Android:src="@drawable/ic_play_arrow_black_24dp"
        Android:layout_centerVertical="true"
        Android:layout_alignParentStart="true" />

    <TextView
        Android:id="@+id/tv_trailer_sequence"
        Android:layout_width="wrap_content"
        Android:layout_height="26dip"
        Android:layout_centerVertical="true"
        Android:layout_toEndOf="@+id/play_button"
        Android:ellipsize="Marquee"
        Android:gravity="center"
        Android:maxLines="1"
        Android:text="Description"
        Android:textSize="12sp" />

</RelativeLayout>

そして、VOILA、私はscollview内でListView(Now RecyclerView)の望ましい効果を得ました。 これがUIの最終画像です

最後に、RecyclerViewを置き換えることは、アプリ全体の安定性を向上させ、RecyclerViewをよりよく理解するのに役立ちました。私が解決策を提案するのであれば、リストビューをRecyclerViewに置き換えてください。

1
Pushkaraj Joshi

私はこの質問がずっと前に尋ねられていることを知っていますが、今のところ立ち往生している人は、この行をListViewに追加することでこれを解決できます

Android:nestedScrollingEnabled="true"

例えば ​​-

                    <ListView
                    Android:id="@+id/listView"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:nestedScrollingEnabled="true" />
1
Karan

@Shailesh Rohitが提供する上記のソリューションは完全に正常に動作します。いくつかのトリックを実行する必要があります。

  1. ヘルパークラスを同じクラス(メインクラス)内に配置する場合は、ヘルパークラスを静的として、getListViewSize()を静的ではないようにします。

  2. 最も重要なことは、「Helper.getListViewSize(listView);」と書くことです。 「listView.setAdapter(myAdapter);」など、初めてアダプターを設定した後のステートメントまた、「myAdapter.notifyDataSetChanged();」を使用しているときも同様です。

  3. 使用法を以下に示します。

    listView =(ListView)findViewById(R.id.listView); myAdapter = new ArrayAdapter(this、Android.R.layout.simple_list_item_1、listValues); listView .setAdapter(myAdapter); Helper.getListViewSizelistView(listView);

    myAdapter.notifyDataSetChanged(); Helper.getListViewSizelistView(listView);

1
Tara

最良の解決策は、NestedScrollVewをRecyclerViewとともに使用することです。または、Listviewを使用する場合は、これにヘッダービューとフッタービューを追加できます。例えば:

View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.outofoffice_footer_view, null, false);
infoListView.addFooterView(footerView);
1
Srishti Roy

Demo_ListView_In_ScrollView

==============================

            package com.app.custom_seekbar;

            import Java.util.ArrayList;

            import Android.app.Activity;
            import Android.os.Bundle;
            import Android.widget.ListView;

            public class Demo_ListView_In_ScrollView  extends Activity
            {
                ListView listview;
                ArrayList<String> data=null;
                listview_adapter adapter=null;

                @Override
                protected void onCreate(Bundle savedInstanceState) 
                {
                    // TODO Auto-generated method stub
                    super.onCreate(savedInstanceState);
                    super.setContentView(R.layout.demo_listview_in_scrollview_activity);
                    init();
                    set_data();
                    set_adapter();


                }
                public void init()
                {
                    listview=(ListView)findViewById(R.id.listView1);
                    data=new ArrayList<String>();

                }
                public void set_data()
                {
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");
                    data.add("Meet");
                    data.add("prachi");
                    data.add("shailesh");
                    data.add("manoj");
                    data.add("sandip");
                    data.add("zala");
                    data.add("tushar");



                }

                public void set_adapter()
                {
                    adapter=new listview_adapter(Demo_ListView_In_ScrollView.this,data);
                    listview.setAdapter(adapter);
                    Helper.getListViewSize(listview); // set height of listview according to Arraylist item  
                }

            }

========================

listview_adapter

==========================

        package com.app.custom_seekbar;

        import Java.util.ArrayList;

        import Android.app.Activity;
        import Android.view.LayoutInflater;
        import Android.view.MotionEvent;
        import Android.view.View;
        import Android.view.ViewGroup;
        import Android.widget.ArrayAdapter;
        import Android.widget.LinearLayout;
        import Android.widget.ScrollView;
        import Android.widget.TextView;

        public class listview_adapter  extends ArrayAdapter<String>  
        {
            private final Activity context;
            ArrayList<String>data;
            class ViewHolder 
            {
                public TextView tv_name;
                public ScrollView scroll;
                public LinearLayout l1;
            }


            public listview_adapter(Activity context, ArrayList<String> all_data) {
                super(context, R.layout.item_list_xml, all_data);
                this.context = context;
                data=all_data;
            }
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View rowView = convertView;
                ViewHolder viewHolder;
                if (rowView == null)
                {
                    LayoutInflater inflater = context.getLayoutInflater();
                    rowView = inflater.inflate(R.layout.item_list_xml, null);

                    viewHolder = new ViewHolder();

                    viewHolder.tv_name=(TextView)rowView.findViewById(R.id.textView1);

                    rowView.setTag(viewHolder);
                }
                else
                viewHolder = (ViewHolder) rowView.getTag();

                viewHolder.tv_name.setText(data.get(position).toString());
                return rowView;

            }


        }

===================================

ヘルパークラス

====================================

    import Android.util.Log;
    import Android.view.View;
    import Android.view.ViewGroup;
    import Android.widget.ListAdapter;
    import Android.widget.ListView;

    public class Helper {
        public static void getListViewSize(ListView myListView) {
            ListAdapter myListAdapter = myListView.getAdapter();
            if (myListAdapter == null) {
                //do nothing return null
                return;
            }
            //set listAdapter in loop for getting final size
            int totalHeight = 0;
            for (int size = 0; size < myListAdapter.getCount(); size++) {
                View listItem = myListAdapter.getView(size, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
          //setting listview item in adapter
            ViewGroup.LayoutParams params = myListView.getLayoutParams();
            params.height = totalHeight + (myListView.getDividerHeight() *                   (myListAdapter.getCount() - 1));
            myListView.setLayoutParams(params);
            // print height of adapter on log
            Log.i("height of listItem:", String.valueOf(totalHeight));
        }
    }

========================

demo_listview_in_scrollview_activity.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="vertical" >

            <ScrollView
                Android:id="@+id/scrollView1"
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content" >

                <LinearLayout
                    Android:layout_width="match_parent"
                    Android:layout_height="match_parent"
                    Android:orientation="vertical" >

                    <ListView
                        Android:id="@+id/listView1"
                        Android:layout_width="match_parent"
                        Android:layout_height="wrap_content" >
                    </ListView>
                </LinearLayout>
            </ScrollView>

        </LinearLayout>

================

item_list_xml.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="vertical" >

            <TextView
                Android:id="@+id/textView1"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:layout_gravity="center_horizontal"
                Android:gravity="center"
                Android:text="TextView"
                Android:textSize="14sp" />

        </LinearLayout>
1
shailesh Rohit

追加:Android:nestedScrollingEnabled="true"

0
DENIS VICTOR

ListViewではなく、ScrollViewで試してください。

public class xScrollView extends ScrollView
{
    ;
    ;
    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev)
    {
        return false;
    }
}
0
Kim Sangdeuk

requestDisallowInterceptTouchEvent(ブールdisallowIntercept)

子がこの親とその祖先にonInterceptTouchEvent(MotionEvent)でタッチイベントをインターセプトすることを望まないときに呼び出されます。この親は、この呼び出しをその親に渡す必要があります。この親は、タッチの間、この要求に従う必要があります(つまり、この親がアップまたはキャンセルを受け取った後にのみフラグをクリアします)。

この答えを試してください、

    listview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

// Disallow the touch request for parent scroll on touch of child view
            scrollView.requestDisallowInterceptTouchEvent(true);

            int action = event.getActionMasked();
            switch (action) {
                case MotionEvent.ACTION_UP:
                    scrollView.requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return false;
        }
    });
0
Ranjith Kumar

私によると、あなたがsetOnTouchListenerを使用している間、親または子は子に触れると親のスクロールを停止し、そうでない場合は親に触れると子のスクロールを停止します

0
sharma_kunal

Xml内でScrollViewをAndroid.support.v4.widget.NestedScrollViewに置き換えます。走る

1)XML :::: Android.support.v4.widget.NestedScrollViewで使用

代わりに:::: ScrollView

2)そして、csファイルでNonScrollListViewを使用して、この方法でリストビューに使用します。

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();
    }    
}    

3)最後に、このコードを使用してcsファイルのscrollviewを識別します。

NonScrollListView listView = (NonScrollListView) view.findViewById(R.id.listview);
0
Amit yadav

このメソッドを使用すると、リストビューがスクロールビュー内でスクロール可能になります-

   ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
                            ViewNewsDetail.this, viewOfferList));
                    getListViewSize(lstNewsOffer);

void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        // do nothing return null
        return;
    }
    // set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight
            + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
}

listViewをScrollView内のRecycleViewに置き換えます。追加のソースコードなしでスムーズに実行されます。

 <ScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
    Android:paddingLeft="@dimen/activity_horizontal_margin"
    Android:paddingRight="@dimen/activity_horizontal_margin"
    Android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hung.recycleviewtest.MainActivityFragment"
    >
    <RelativeLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content">
        <Android.support.v7.widget.RecyclerView
            Android:id="@+id/recycle_view"
            Android:layout_width="match_parent"
            Android:layout_height="400dp"
            Android:background="@Android:color/darker_gray"/>
        <Android.support.v7.widget.RecyclerView
            Android:id="@+id/recycle_view_a"
            Android:layout_marginTop="40dp"
            Android:layout_below="@id/recycle_view"
            Android:layout_width="match_parent"
            Android:layout_height="400dp"
            Android:background="@Android:color/darker_gray"/>
    </RelativeLayout>

</ScrollView>
0
HungNM2

私はこのエラーが発生しました。私の解決策は次のとおりです:

1。スクロールできないカスタムリストビューを作成します

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();    
}

}

2。xmlファイルに上記のカスタムクラスを使用

  <com.Example.NonScrollListView
        Android:id="@+id/lv_nonscroll_list"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" >
    </com.Example.NonScrollListView>

あなたに最適です。

0
Hai Rom

最高のコード

<Android.support.v4.widget.NestedScrollView
Android:id="@+id/scrollView1"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_above="@+id/btmlyt"
Android:layout_below="@+id/deshead_tv">

<LinearLayout
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
   Android:orientation="vertical"
   >

<TextView
    Android:id="@+id/des_tv"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:layout_above="@+id/btmlyt"
    Android:background="@Android:color/white"
    Android:paddingLeft="3dp"
    Android:paddingRight="3dp"
    Android:scrollbars="vertical"
    Android:paddingTop="3dp"
    Android:text="description"
    Android:textColor="@Android:color/black"
    Android:textSize="18sp" />
</LinearLayout>

</Android.support.v4.widget.NestedScrollView>
0

私はそのための解決策を見つけましたscrollviewを使用する代わりに、listviewのaddHeaderviewとaddFooterviewを使用できます

これが私のスニペットです。

//  create separate layout and add it dynamically
    scrollview=getLayoutInflater().inflate(R.layout.yourlayout,null);
    lv=(ListView)listview.findViewById(R.id.listView2);
    lv.addHeaderView(scrollview);
    lv.setContentView(lv);

これで、リストビューでレイアウトもスクロールできます。楽しめ!!!

0
Jyoti JK

ScrollView内のListViewでは、NestedScrollViewを使用して、この機能を非常に簡単に処理できます。

<Android.support.v4.widget.NestedScrollView
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:orientation="vertical">

            <Android.support.v7.widget.RecyclerView
                Android:id="@+id/recycler_view"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:padding="5dip"/>

        </LinearLayout>
    </Android.support.v4.widget.NestedScrollView>
0
Puneet Kumar