web-dev-qa-db-ja.com

スクロールビュー内のAndroidリストビュー

私はそれに含まれる要素の数でscrollViewを持っているAndroidのレイアウトを持っています。 scrollViewの一番下に私はlistViewを持っています、そしてそれはそれからアダプターによって移入されます。

私が経験している問題は、listViewが既にスクロール可能な機能を持っているので、AndroidがscrollViewからscrollViewを除外していることです。私はlistViewを内容がある限り、そしてマスタースクロールビューがスクロール可能であるようにしたいです。

どうすればこの動作を実現できますか?

これが私の主なレイアウトです。

<ScrollView
    Android:id="@+id/scrollView1"
    Android:layout_width="match_parent"
    Android:layout_height="0dp"
    Android:layout_weight="2"
    Android:fillViewport="true"
    Android:gravity="top" >

    <LinearLayout
        Android:id="@+id/foodItemActvity_linearLayout_fragments"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical" >
    </LinearLayout>

</ScrollView>

それから私はプログラム的に自分のコンポーネントをid:foodItemActvity_linearLayout_fragmentsでlinearlayourに追加します。以下はその線形レイアウトにロードされるビューの1つです。これは私が巻物に問題を抱えているものです。

<?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:orientation="vertical" >

    <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" />

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

私のアダプタはこのリストビューを埋めます。

これは私がマスターscrollViewをクリックしたときのAndroid階層ビューアからの画像です。

Android List View inside a scroll view

ご覧のとおり、レビューのlistViewを除いています。

私はページをスクロールダウンして8つのレビューを見ることができるはずですが、代わりにそれは私にそれらの3つを示しているだけです、そして私はレビューがある小さな部分をスクロールすることができます。グローバルページスクロールが欲しい

249
Zapnologica

ScrollView内でスクロールするChildView最短かつ最も簡単なソリューションListView、RecyclerViewなどのようなものコードで特別なことをする必要はありません。

ScrollViewAndroid.support.v4.widget.NestedScrollView現在のxmlでマジックが発生します。

以下はサンプルxmlコードです。

<?xml version="1.0" encoding="utf-8"?>
<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.support.v7.widget.LinearLayoutCompat
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical"
        Android:padding="16dp"
        Android:paddingBottom="20dp">

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:text="Recycler View inside a Scroll View"
            Android:textColor="@color/black"
            Android:textSize="@dimen/_20sp"
            Android:textStyle="bold" />

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginTop="8dp"
            Android:text="Below is a Recycler View as an example."
            Android:textSize="16sp" />

        <Android.support.v7.widget.RecyclerView
            Android:id="@+id/recycler_view"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@id/et_damaged_qty" />

        <TextView
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_marginTop="8dp"
            Android:text="This text automatically goes below the Recycler View."
            Android:textSize="16sp" />
    </Android.support.v7.widget.LinearLayoutCompat>
</Android.support.v4.widget.NestedScrollView>

これで、このネストされたスクロールを回避するために行ったすべてのいハックを取り除くことができます。

次はプレイします。地獄Yeeeeeeeeeeeeeeeeeah!

enter image description here

547
arshu

答えは簡単です、そしてここでまだ答えられていないことに驚きました。

リスト自体にHeader Viewまたは/およびFooter Viewを使用します。 ScrollViewListViewまたはスクロールできるものを混在させないでください。それはヘッダとフッタで使われることを意図しています:)

基本的には、ListViewの上にあるすべてのコンテンツをレイアウトとして別の.xmlファイルに入れてからコードで展開し、それをヘッダービューとしてリストに追加します。

すなわち.

View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
listView.addHeaderView(header);
listView.addFooterView(footer);
218
ericosg

私はそれが長すぎることを知っていますが、私もこの問題を抱えていました。だから私はそれが他の人たちにも役立つかもしれないと思います。

ScrollViewのレイアウトxmlにAndroid:fillViewport = "true"を追加します。だから全体的に私のScrollViewはこのようになるでしょう。

<ScrollView
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:id="@+id/scrollView6" 
    Android:fillViewport="true">

そしてそれは私にとって魔法のように働きます。私のScrollViewの中にあるListViewは、そのサイズに再び拡大します。

これがScrollViewとListViewのための完全なサンプルコードです。

<ScrollView
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:id="@+id/scrollView6" Android:fillViewport="true">
    <LinearLayout
        Android:orientation="vertical"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">
        ....
        <ListView
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:id="@+id/lv_transList" Android:layout_gravity="top"
            Android:layout_marginTop="5dp"/>
        ....
    </LinearLayout>
</ScrollView>
34
acyn

スクロールできないカスタムListViewを作成します

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

あなたのレイアウトリソースファイルに

     <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:fadingEdgeLength="0dp"
            Android:fillViewport="true"
            Android:overScrollMode="never"
            Android:scrollbars="none" >

            <RelativeLayout
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content" >

                <!-- com.Example Changed with your Package name -->

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

                <RelativeLayout
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:layout_below="@+id/lv_nonscroll_list" >

                    <!-- Your another layout in scroll view -->

                </RelativeLayout>
            </RelativeLayout>

        </ScrollView>

Javaファイルの場合以下のように、ListViewの代わりにcustomListviewのオブジェクトを作成します。 NonScrollListView non_scroll_list =(NonScrollListView)findViewById(R.id.lv_nonscroll_list);

16
    public static void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0); // 计算子项View 的宽高
        totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}

あなたはscrollviewのlistviewのためにこのコードを使うことができます

8
Su Zhenpeng

ScrollViewにAndroid:fillViewport="true"を追加することで解決できます。

<ScrollView
      Android:layout_width="match_parent"
      Android:layout_height="match_parent"
      Android:background="@color/white"
      Android:fillViewport="true"
      Android:scrollbars="vertical">

<ListView
      Android:id="@+id/statusList"
      Android:layout_width="fill_parent"
      Android:layout_height="wrap_content"
      Android:animationCache="false"
      Android:divider="@null"
      Android:scrollingCache="false"
      Android:smoothScrollbar="true" />

</ScrollView>


そのプロパティを使用する前は、リストビューの子は1人しか表示されていませんでした。それを使用した後、リストのすべての行または子が表示されます。

8
MV53

ListViewだけをコードに実装した場合、このコードは問題を解決します。

ListViewの子としてRelativeLayoutを使用している場合、このコードよりもNullPointerExceptionが返されます。 listItem.measure(0、0); 、RelativeLayoutのため。ソリューションはRelativelayoutをLinearLayout内に配置しても問題ありません。

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
6
krunal shah

誰かが同じ問題に直面する可能性がある場合に備えて、ここに残しておきます。私はScrollViewの中にListViewを置かなければなりませんでした。ヘッダー付きのListViewは、さまざまな理由からオプションではありませんでした。 ListViewの代わりにLinearLayoutを使用することもできませんでした。それで私は容認された解決策に従いました、しかしリストの項目が複数の行で複雑なレイアウトを持っていて、そしてそれぞれのlistview項目が可変の高さであったのでそれはうまくいきませんでした。高さが正しく測定されていません。解決策は、ListViewアダプタのgetView()メソッド内で各項目を測定することでした。

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view == null) {
        . . .
        view.setTag(holder);
    } else holder = (ViewHolder)view.getTag();
    . . .

    // measure ListView item (to solve 'ListView inside ScrollView' problem)
    view.measure(View.MeasureSpec.makeMeasureSpec(
                    View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    return view;
}
5
Alexander Zhak

あなたは簡単にScrollViewにListViewを置くことができます! ListViewの高さを変更する必要があります プログラム的に 、このように:

    ViewGroup.LayoutParams listViewParams = (ViewGroup.LayoutParams)listView.getLayoutParams();
    listViewParams.height = 400;
    listView.requestLayout();

これは完璧に機能します。

4
Taras Okunev

たくさんの研究開発を経て完成しました:

fragment_one.xmlは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:id="@+id/scrollViewParent"
    Android:orientation="vertical" >

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

        <RelativeLayout
            Android:layout_width="match_parent"
            Android:layout_height="400dip" >

            <ListView
                Android:id="@+id/listView"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent" />

            <View
                Android:id="@+id/customView"
                Android:layout_width="fill_parent"
                Android:layout_height="fill_parent"
                Android:background="@Android:color/transparent" />
        </RelativeLayout>

        <!-- Your other elements are here -->

    </LinearLayout>

</ScrollView>

FragmentOne.JavaのJavaクラスは以下のようになります。

private ListView listView;
private View customView

onCreateView

listView = (ListView) rootView.findViewById(R.id.listView);
scrollViewParent = (ScrollView)rootView.findViewById(R.id.scrollViewParent);
customView = (View)rootView.findViewById(R.id.customView);

customView.setOnTouchListener(new View.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.
                        scrollViewParent.requestDisallowInterceptTouchEvent(true);
                        // Disable touch on transparent view
                        return false;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        scrollViewParent.requestDisallowInterceptTouchEvent(false);
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        scrollViewParent.requestDisallowInterceptTouchEvent(true);
                        return false;

                    default:
                        return true;
                }
            }
        });
4
Hiren Patel

Parent ScrollViewでは何もしないでください。子ListViewに対してのみこれを行います。すべてうまくいくでしょう。

mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mScrollView.requestDisallowInterceptTouchEvent(true);
               int action = event.getActionMasked();
                switch (action) {
                    case MotionEvent.ACTION_UP:
                        mScrollView.requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
4
Sibin

すべてを線形レイアウトにすることができます。つまり、線形レイアウトを作成すると、2つの子、scrollviewともう1つの線形レイアウトが作成されます。それらにレイアウトの重みを与えると、ここに行きます:

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical" >

<ScrollView
    Android:layout_width="fill_parent"
    Android:layout_height="0dip" Android:layout_weight="0.8">

    <LinearLayout
        Android:id="@+id/seTaskActivityRoot"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:background="@color/white"
        Android:orientation="vertical" >

        <TextView
            Android:id="@+id/textView1"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:text="@string/taskName" />


        <Spinner
            Android:id="@+id/seTaskPrioritiesSP"
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content"
            Android:layout_weight="1" />

        <TextView
            Android:id="@+id/textView4"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:text="@string/taskTargetInNumeric" />

        <Spinner
            Android:id="@+id/seTaskUnitsSP"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_weight="1" />

        <TextView
            Android:id="@+id/textView6"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:text="@string/newTaskCurrentStatus" />

        <EditText
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:ems="10"
            Android:hint="@string/addTaskCurrentStatus"
            Android:inputType="numberDecimal" />


    </LinearLayout>
</ScrollView>

<LinearLayout
    Android:layout_width="match_parent"
    Android:layout_height="0dip"
    Android:orientation="vertical" Android:layout_weight="0.2">

    <TextView
        Android:id="@+id/textView8"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:text="TextView" />

    <ListView
        Android:id="@+id/logList"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
3
Jeyhun Karimov

他の人がすでに述べたように、ScrollViewの中でListViewを使わないでください。

これを回避するには、LinearLayoutを使用しますが、それでもきれいにしておくために、ListViewを使用する場合と同じように、LinearLayoutにアダプタを追加します。

このクラスは、アダプタをサポートするLinearLayoutの代わりとして使用できます。

import Android.content.Context;
import Android.database.DataSetObserver;
import Android.util.AttributeSet;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.BaseAdapter;
import Android.widget.LinearLayout;

public class AdaptableLinearLayout extends LinearLayout {

private BaseAdapter mAdapter;

private int mItemCount = 0;

private boolean mDisableChildrenWhenDisabled = false;

private int mWidthMeasureSpec;
private int mHeightMeasureSpec;


public AdaptableLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public BaseAdapter getAdapter() {
    return mAdapter;
}

public void setAdapter(BaseAdapter adapter) {
    mAdapter = adapter;
    adapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            updateLayout();
            super.onChanged();
        }

        @Override
        public void onInvalidated() {
            updateLayout();
            super.onInvalidated();
        }
    });
    updateLayout();
}

private void updateLayout() {
    mItemCount = mAdapter.getCount();
    requestLayout();
    invalidate();
}

/**
 * set size for the current View
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mWidthMeasureSpec = widthMeasureSpec;
    mHeightMeasureSpec = heightMeasureSpec;

    removeAllViewsInLayout();
    for (int i = 0; i < mItemCount; i++) {
        makeAndAddView(i);
    }
}

private View makeAndAddView(int position) {
    View child;

    // Nothing found in the recycler -- ask the adapter for a view
    child = mAdapter.getView(position, null, this);

    // Position the view
    setUpChild(child, position);

    return child;

}

private void setUpChild(View child, int position) {

    ViewGroup.LayoutParams lp = child.getLayoutParams();
    if (lp == null) {
        lp = generateDefaultLayoutParams();
    }
    addViewInLayout(child, position, lp);

    // Get measure specs
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec, getPaddingTop() + getPaddingBottom(), lp.height);
    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, getPaddingLeft() + getPaddingRight(), lp.width);

    // Measure child
    child.measure(childWidthSpec, childHeightSpec);

    int childLeft;
    int childRight;

    // Position vertically based on gravity setting
    int childTop = getPaddingTop() + ((getMeasuredHeight() - getPaddingBottom() - getPaddingTop() - child.getMeasuredHeight()) / 2);
    int childBottom = childTop + child.getMeasuredHeight();

    int width = child.getMeasuredWidth();
    childLeft = 0;
    childRight = childLeft + width;

    child.layout(childLeft, childTop, childRight, childBottom);

    if (mDisableChildrenWhenDisabled) {
        child.setEnabled(isEnabled());
    }
}
}
3
AAverin

ListViewは独自の垂直スクロールを処理するので、ScrollViewをListViewと一緒に使用しないでください。最も重要なことは、これを行うと、ListViewがScrollViewで提供される無限のコンテナを埋めるためにListViewに項目のリスト全体を表示するように強制するため、ListViewで大きなリストを扱うための重要な最適化がすべて無効になることです。

http://developer.Android.com/reference/Android/widget/ScrollView.html

3
Muhammad Asim

更新

<ScrollView
Android:id="@+id/scrollView1"
Android:layout_width="match_parent"
Android:layout_height="0dp"
Android:layout_weight="2"
Android:fillViewport="true"
Android:gravity="top" >

<LinearLayout
    Android:id="@+id/foodItemActvity_linearLayout_fragments"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:orientation="vertical" >
</LinearLayout>

<ScrollView
Android:id="@+id/scrollView1"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_weight="2"
Android:fillViewport="true"
Android:gravity="top" >

<LinearLayout
    Android:id="@+id/foodItemActvity_linearLayout_fragments"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:orientation="vertical" >
</LinearLayout>

ここで重要なのは、高さを0dpに設定しようとしていることです(固定)

2
Rajnish Mishra

私はオリジナルポスターが提起した問題 - リストビューをスクロールビュー内でスクロールさせる方法 - と同様の問題を抱えていましたが、この回答は私の問題を解決しました。 ScrollViewに含まれるListViewのスクロールを無効にします

OPが行っていたように、既存のレイアウトやそのようなものに新しいフラグメントを呼び出さなかったので、私のコードは次のようになります。

<ScrollView
    Android:id="@+id/scrollView1"
    Android:layout_width="match_parent"
    Android:layout_height="0dp"
    Android:layout_weight="2"
    Android:fillViewport="true"
    Android:gravity="top" >

 <LinearLayout
        Android:id="@+id/foodItemActvity_linearLayout_fragments"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical" >


    <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" />

   <ListView
       Android:id="@+id/my_listView"
       Android:layout_width="match_parent"
       Android:layout_height="wrap_content">
   </ListView>

</LinearLayout>

</ScrollView>

基本的に私がしていることは私がそれを呼び出す前にリストビューの長さをチェックすることですそしてそれを呼び出すとき私はそれをその長さにします。あなたのJavaクラスでこの関数を使ってください。

public static void justifyListViewHeightBasedOnChildren (ListView listView) {

    ListAdapter adapter = listView.getAdapter();

    if (adapter == null) {
        return;
    }
    ViewGroup vg = listView;
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams par = listView.getLayoutParams();
    par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(par);
    listView.requestLayout();
}

そしてこのように関数を呼び出します。

justifyListViewHeightBasedOnChildren(listView);

その結果、スクロールバーのないリストビューが表示されます。表示されているリストビューの全長は、スクロールビューのスクロールバーでスクロールします。

2
CHarris

scrollview - > viewpager - > FragmentPagerAdapter - > fragment - >動的リストビューの解決策を見つけましたが、作者ではありません。いくつかのバグがありますが、少なくともそれは動作します

public class CustomPager extends ViewPager {

    private View mCurrentView;

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        this.post(new Runnable() {
            @Override
            public void run() {
                requestLayout();
            }
        });
    }

    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}


public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;
    private int mCurrentPosition = -1;


    public MyPagerAdapter(FragmentManager fm) {
        super(fm);//or u can set them separately, but dont forget to call notifyDataSetChanged()
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FirstFragment());
        fragments.add(new SecondFragment());
        fragments.add(new ThirdFragment());
        fragments.add(new FourthFragment());
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (position != mCurrentPosition) {
            Fragment fragment = (Fragment) object;
            CustomPager pager = (CustomPager) container;
            if (fragment != null && fragment.getView() != null) {
                mCurrentPosition = position;
                pager.measureCurrentView(fragment.getView());
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

フラグメントのレイアウトは何でも

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools" Android:layout_width="match_parent"
Android:orientation="vertical"
    Android:layout_height="match_parent" tools:context="nevet.me.wcviewpagersample.FirstFragment">


    <ListView
        Android:id="@+id/lv1"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:background="#991199"/>
</LinearLayout>

それからどこかで

lv = (ListView) view.findViewById(R.id.lv1);
        lv.setAdapter(arrayAdapter);
        setListViewHeightBasedOnChildren(lv);
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
                        LinearLayout.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
2
bedman

私の要件は、ScrollView内に同じサイズのアイテムのListViewを含めることです。ここにリストされている他の解決策をいくつか試しましたが、どれもListViewのサイズを正しく設定できないようでした(スペースが小さすぎるか大きすぎる)。これは私のために働いたものです:

    public static void expandListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    listView.measure(0, 0);
    params.height = listView.getMeasuredHeight() * listAdapter.getCount() + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

これが誰かに役立つことを願っています。

2
Malcolm Bryant

Xmlの場合:

<com.example.util.NestedListView
                    Android:layout_marginTop="10dp"
                    Android:id="@+id/listview"
                    Android:layout_width="fill_parent"
                    Android:layout_height="fill_parent"
                    Android:divider="@null"

                    Android:layout_below="@+id/rl_delivery_type" >
                </com.example.util.NestedListView>

Javaの場合:

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.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;
    }
}
1
Makvin

わかりました、これが私の答えです。 ListViewの高さを固定する方法は十分に閉じられていますが、完全ではありません。ほとんどのアイテムが同じ高さである場合、それはうまく機能します。しかし、そうでない場合は、大きな問題があります。私は何度も試してみましたが、listItem.getMeasureHeightとlistItem.getMeasuerWidthの値をログに記録すると、幅の値が大きく異なることがわかりました。これは、同じListView内のすべての項目で行われるべきではないため同じ幅を持っています。そしてそこにバグがあります:

ある方向(0、0)で実際にビューをアンバインドにしていたものもあります。 listViewのgetWidthを使おうとした人もいますが、0を返すので意味がありません。

私がAndroidがどのようにビューをレンダリングするのかをさらに詳しく読むと、ビューがレンダリングされた後にこれらの関数が実行されない限り、これらすべての試みが私が探した答えに達することはできない。

今回は高さを固定したいListViewのgetViewTreeObserverを使用し、次にaddOnGlobalLayoutListenerを使用します。このメソッドの中で、私は新しいOnGlobalLayoutListenerを宣言します。今回は、getWidthがListViewの実際の幅を返します。

private void getLayoutWidth(final ListView lv, final int pad){
        //final ArrayList<Integer> width = new ArrayList<Integer>();

        ViewTreeObserver vto = lv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                lv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //width.add(layout.getMeasuredWidth());
                int width = lv.getMeasuredWidth();
                ListUtils.setDynamicHeight(lv, width, pad);
            }
        });
    }

public static class ListUtils {
        //private static final int UNBOUNDED = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        public static void setDynamicHeight(ListView mListView, int width, int pad) {
            ListAdapter mListAdapter = mListView.getAdapter();
            mListView.getParent();
            if (mListAdapter == null) {
                // when adapter is null
                return;
            }
            int height = 0;


            int desiredWidth = View.MeasureSpec.makeMeasureSpec(width - 2*pad, View.MeasureSpec.EXACTLY);
            for (int i = 0; i < mListAdapter.getCount(); i++) {
                View listItem = mListAdapter.getView(i, null, mListView);

                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                //listItem.measure(UNBOUNDED, UNBOUNDED);
                height += listItem.getMeasuredHeight() + 2*pad;
                Log.v("ViewHeight :", mListAdapter.getClass().toString() + " " + listItem.getMeasuredHeight() + "--" + listItem.getMeasuredWidth());
            }
            ViewGroup.LayoutParams params = mListView.getLayoutParams();
            params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
            mListView.setLayoutParams(params);
            mListView.requestLayout();
        }
    }

値padは、ListViewレイアウトで設定した余白です。

1

このListViewを使う

   package net.londatiga.Android.widget;

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

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

そしてxmlで

            <com.pakagename.ExpandableHeightListView
                Android:id="@+id/expandableHeightListView"
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content" >
            </com.Example.ExpandableHeightListView>

とMainActivityに

  ExpandableHeightListView listView = new ExpandableHeightListView(this);
    listview=(ExpandableHeightListView)findViewById(R.id.expandableHeightListView);
   listView.setAdapter(adapter); //set your adaper
   listView.setExpanded(true);

詳しくは、スクロールビュー内にgridviewを保持する方法を知るための 参照 記事

1
Manohar Reddy

何らかの理由でaddHeaderViewaddFooterViewを使いたくない場合は、複数のリストがある場合は、ListAdapterを再利用して単純なLinearLayoutを作成することをお勧めします。スクロール機能はありません。

ListFragmentから派生したフラグメント全体を既に持っていて、その代わりにスクロールせずに単純なLinearLayoutを持つ同様のフラグメントに変換したい場合(例えばScrollViewに入れるなど)、次のようなアダプタフラグメントを実装できます。

// converts listFragment to linearLayout (no scrolling)
// please call init() after fragment is inflated to set listFragment to convert
public class ListAsArrayFragment extends Fragment {
    public ListAsArrayFragment() {}

    private ListFragment mListFragment;
    private LinearLayout mRootView;


    // please call me!
    public void init(Activity activity, ListFragment listFragment){
        mListFragment = listFragment;
        mListFragment.onAttach(activity);
        mListFragment.getListAdapter().registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                refreshView();
            }
        });
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // create an empty vertical LinearLayout as the root view of this fragment
        mRootView = new LinearLayout(getActivity());
        mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mRootView.setOrientation(LinearLayout.VERTICAL);
        return mRootView;
    }

    // reusing views for performance
    // todo: support for more than one view type
    ArrayList<View> mViewsToReuse = new ArrayList<>();
    ArrayList<View> mCurrentViews = new ArrayList<>();

    // re-add views to linearLayout
    void refreshView(){

        // remove old views from linearLayout and move them to mViewsToReuse
        mRootView.removeAllViews();
        mViewsToReuse.addAll(mCurrentViews);
        mCurrentViews.clear();

        // create new views
        for(int i=0; i<mListFragment.getListAdapter().getCount(); ++i){
            View viewToReuse = null;
            if(!mViewsToReuse.isEmpty()){
                viewToReuse = mViewsToReuse.get(mViewsToReuse.size()-1);
                mViewsToReuse.remove(mViewsToReuse.size()-1);
            }
            final View view = mListFragment.getListAdapter().getView(i, viewToReuse, mRootView);
            ViewGroup.LayoutParams oldParams = view.getLayoutParams();
            view.setLayoutParams(new LinearLayout.LayoutParams(oldParams.width, oldParams.height));
            final int finalI = i;

            // pass click events to listFragment
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListFragment.onListItemClick(null, view, finalI, finalI);
                }
            });
            mRootView.addView(view);
            mCurrentViews.add(view);
        }
    }

ニーズに応じてonCreateonPauseonResumeなどを元のフラグメントに転送するか、コンポジションの代わりに継承を試すこともできます(ただし、元のフラグメントが実際にレイアウト階層に関連付けられないように特定のメソッドをオーバーライドします)。しかし、ListAdapterを抽出するだけでよいため、可能な限り元のフラグメントを分離したいと思いました。元のフラグメントのsetListAdapteronAttachで呼び出すと、おそらくそれで十分です。

スクロールしないでListAsArrayFragmentをインクルードするためにOriginalListFragmentを使う方法は次のとおりです。親アクティビティのonCreate内:

ListAsArrayFragment fragment = (ListAsArrayFragment) getFragmentManager().findFragmentById(R.id.someFragmentId);
OriginalListFragment originalFragment = new OriginalListFragment();
fragment.init(this, originalFragment);

// now access originalFragment.getListAdapter() to modify list entries
// and remember to call notifyDatasetChanged()
1
atablash

scrollview - > viewpager - > FragmentPagerAdapter - > fragment - >動的リストビューの解決策を見つけましたが、作者ではありません。

public class CustomPager extends ViewPager {

    private View mCurrentView;

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        this.post(new Runnable() {
            @Override
            public void run() {
                requestLayout();
            }
        });
    }

    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}


public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;
    private int mCurrentPosition = -1;


    public MyPagerAdapter(FragmentManager fm) {
        super(fm);//or u can set them separately, but dont forget to call notifyDataSetChanged()
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FirstFragment());
        fragments.add(new SecondFragment());
        fragments.add(new ThirdFragment());
        fragments.add(new FourthFragment());
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (position != mCurrentPosition) {
            Fragment fragment = (Fragment) object;
            CustomPager pager = (CustomPager) container;
            if (fragment != null && fragment.getView() != null) {
                mCurrentPosition = position;
                pager.measureCurrentView(fragment.getView());
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

フラグメントのレイアウトは何でも

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools" Android:layout_width="match_parent"
Android:orientation="vertical"
    Android:layout_height="match_parent" tools:context="nevet.me.wcviewpagersample.FirstFragment">


    <ListView
        Android:id="@+id/lv1"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:background="#991199"/>
</LinearLayout>

それからどこかで

 lv = (ListView) view.findViewById(R.id.lv1);
        lv.setAdapter(arrayAdapter);
        setListViewHeightBasedOnChildren(lv);
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
                        LinearLayout.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
1
qwerty

ListViewScrollViewの中に入れないでください。そのトピックに関する詳細情報は Google にあります。あなたのケースでは、LinearLayoutの代わりにListViewを使って、プログラム的に要素を追加してください。

1
SimonSays

アダプタをリストビューに割り当てた後にこの関数を呼び出すだけです。

public static void setListViewHeightBasedOnChildren
            (ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0) view.setLayoutParams(new
                    ViewGroup.LayoutParams(desiredWidth,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));

        listView.setLayoutParams(params);
        listView.requestLayout();
    } 
0

これは私のために働きました( link1link2 ):

  • スクロールできないカスタムListViewを作成します。

    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 = View.MeasureSpec.makeMeasureSpec(
                        Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            }
        }
    
  • レイアウトファイル内

    <ScrollView 
     xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:fillViewport="true">
    
        <RelativeLayout
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content" >
    
            <!-- com.Example Changed with your Package name -->
    
            <com.thedeveloperworldisyours.view.NonScrollListView
                Android:id="@+id/lv_nonscroll_list"
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content" >
            </com.thedeveloperworldisyours.view.NonScrollListView>
    
            <RelativeLayout
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
                Android:layout_below="@+id/lv_nonscroll_list" >
    
                <!-- Your another layout in scroll view -->
    
            </RelativeLayout>
        </RelativeLayout>
    
    </ScrollView>
    
  • 以下のように、ListViewの代わりにcustomListviewのオブジェクトを作成します。

     NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_nonscroll_list);
    
  • リストビューには既にスクロールプロパティがあるため、リストビュー内でスクロールビューを使用することはできません。
  • Scroll-viewの中でlist-viewを使うには、私にはうまくいった以下のステップに従うことができます。

    1)リストビューのデフォルトのスクロールプロパティを無効にする NonScrollListView Javaファイルを作成します。そしてコードは以下の通りです

    package your-package-structure;
    
    import Android.content.Context;
    import Android.util.AttributeSet;
    import Android.view.ViewGroup;
    import Android.widget.ListView;
    
    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)NestedScrollViewを持つxmlファイルを作成し、その中にあなたのアイテムをリストするためのNonScrollListViewを使用します。これにより、画面全体がすべてのビューでスクロールされます。

            <LinearLayout
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
                Android:layout_weight="1"
                Android:orientation="vertical">
                <ViewFlipper
    
                    Android:id="@+id/v_flipper"
                    Android:layout_width="match_parent"
                    Android:layout_height="130dp">
                </ViewFlipper>
                <TextView
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
    
                    Android:text="SHOP"
                    Android:textSize="15dp"
                    Android:textStyle="bold"
                    Android:gravity="center"
                    Android:padding="5dp"
                    Android:layout_marginTop="15dp"
                    Android:layout_marginBottom="5dp"
                    Android:layout_marginLeft="8dp"
                    Android:layout_marginRight="8dp"/>
                <View
                    Android:layout_width="match_parent"
                    Android:layout_height="1dp"
    
                    Android:layout_marginBottom="8dp"
                    Android:layout_marginLeft="8dp"
                    Android:layout_marginRight="8dp"
                    Android:background="#ddd"/>
            </LinearLayout>
            <LinearLayout
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
                Android:orientation="vertical"
                Android:layout_weight="1"
                >
                <com.abc.xyz.NonScrollListView
                    Android:id="@+id/listview"
    
                    Android:divider="@null"
                    Android:layout_width="match_parent"
                    Android:layout_marginBottom="10dp"
                    Android:layout_height="match_parent"
                    Android:padding="8dp">
                </com.abc.xyz.NonScrollListView>
            </LinearLayout>
    
           <LinearLayout
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
    
                Android:gravity="bottom">
                <include layout="@layout/footer" />
            </LinearLayout>
    
        </LinearLayout>
    

    3)今度はJavaクラスすなわちhome.JavaではNonScrollListViewの代わりにListviewを定義します。

    package comabc.xyz.landscapeapp;
    import Android.content.Intent;
    import Android.support.annotation.NonNull;
    import Android.support.annotation.Nullable;
    import Android.support.v4.app.Fragment;
    import Android.os.Bundle;
    import Android.support.v4.app.FragmentTransaction;
    import Android.util.Log;
    import Android.view.LayoutInflater;
    import Android.view.View;
    import Android.view.ViewGroup;
    import Android.widget.AdapterView;
    import Android.widget.Button;
    import Android.widget.ImageView;
    
    import Android.widget.ListView;
    import Android.widget.Toast;
    import Android.widget.Toolbar;
    import Android.widget.ViewFlipper;
    

    public class homeはFragmentを拡張しています{int pos = 0; ViewFlipper v_flipper;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_home, container, false);
        return view;
    }
    
    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        NonScrollListView listView = (NonScrollListView) view.findViewById(R.id.listview);
        customAdapter customAdapter = new customAdapter(getActivity());
        listView.setAdapter(customAdapter);
        listView.setFocusable(false);
    
        customAdapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("listview click", "onItemClick: ");
               /* FragmentTransaction fr = getFragmentManager().beginTransaction().replace(R.id.fragment_container, new productdisplay());
    
                fr.putExtra("Position", position);
                fr.addToBackStack("tag");
                fr.commit();*/
                Intent intent = new Intent(getActivity(), productdisplay.class);
                intent.putExtra("Position", position);
                startActivity(intent);
            }
        });
    
    
        //image slider
        int images[] = {R.drawable.slide1, R.drawable.slide2, R.drawable.slide3};
        v_flipper = view.findViewById(R.id.v_flipper);
        for (int image : images) {
            flipperImages(image);
    
        }
    }
    
    private void flipperImages(int image) {
        ImageView imageView = new ImageView(getActivity());
        imageView.setBackgroundResource(image);
    
        v_flipper.addView(imageView);
        v_flipper.setFlipInterval(4000);
        v_flipper.setAutoStart(true);
    
        v_flipper.setInAnimation(getActivity(), Android.R.anim.slide_in_left);
        v_flipper.setOutAnimation(getActivity(), Android.R.anim.slide_out_right);
    }
    }
    

    注:ここではFragmentsを使用しました。

0
user8235005

親のスクロールビュー内のlistviewのheight属性にrequired heightの値を設定するだけです。他の親の子アイテムと一緒にスクロールします。

0
Shibu Tamang