android listview显示所有可用项目,不带静态标题滚动

android listview显示所有可用项目,不带静态标题滚动,android,listview,Android,Listview,我在尝试使用某个布局时遇到了一些困难:我想要一个列表。列表不必是可滚动的,但应完全显示。但是如果总内容高于屏幕,页面本身应该能够滚动(包含列表) <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" >

我在尝试使用某个布局时遇到了一些困难:我想要一个列表。列表不必是可滚动的,但应完全显示。但是如果总内容高于屏幕,页面本身应该能够滚动(包含列表)

<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     >

     <LinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/linear_layout"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:background="#ff181818"
         >
           <Textview android:id="@+id/my_text" text="header contents goes here" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
           <Textview android:id="@+id/headertext" text="header contents goes here" android:layout_width="fill_parent" android:layout_height="wrap_content"/>

          <ListView
               android:id="@+id/my_list1"
               android:layout_height="wrap_content"
               android:layout_width="fill_parent"
          /> 
     </LinearLayout> 

</ScrollView>


它只使用屏幕的一小部分(每个列表大约2行),而不是填充可用的高度,并且列表本身可以滚动。如何更改布局以始终显示整个列表,但使屏幕滚动

我使用的解决方案是用LinearLayout替换ListView。您可以在LinearLayout中创建所有项目,它们都将显示出来。因此,实际上没有必要使用ListView

LinearLayout list = (LinearLayout)findViewById(R.id.list_recycled_parts);
for (int i=0; i<products.size(); i++) {
  Product product = products.get(i);
  View vi = inflater.inflate(R.layout.product_item, null);
  list.addView(vi);
}
LinearLayout list=(LinearLayout)findViewById(R.id.list\u回收零件);

对于线性布局中的(int i=0;iSet
android:layout\u height=“fill\u parent”
,请查看以下内容:

使用android:layout\u height和android:layout\u weight为我解决了这个问题:

<ListView
    android:layout_height="0dp"
    android:layout_weight="1"
    />

我只是使用ListView的设置参数来完成的

public static void setListViewHeightBasedOnChildren(ListView listView) {

    //this comes from value from xml tag of each item
    final int HEIGHT_LARGE=75;
    final int HEIGHT_LARGE=50;
    final int HEIGHT_LARGE=35;
    ViewGroup.LayoutParams params = listView.getLayoutParams();

    int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

    switch(screenSize) {

    case Configuration.SCREENLAYOUT_SIZE_LARGE:
         params.height =(int) (HEIGHT_LARGE*size);
         break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
         params.height =(int) (HEIGHT_NORMAL*size);
         break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
          params.height =(int) (HEIGHT_SMALL*size);
          break;
    }
    listView.setLayoutParams(params);  
}

我的布局中有一个
列表视图
,我想在这里使用一个不能处理
列表视图
的库,因为它将它包装成
滚动视图
。对我来说,最好的解决方案是基于Fedor的答案

由于我已经为
列表视图
获得了一个
阵列适配器
,我想重新使用它:

LinearLayout listViewReplacement = (LinearLayout) findViewById(R.id.listViewReplacement);
NamesRowItemAdapter adapter = new NamesRowItemAdapter(this, namesInList);
for (int i = 0; i < adapter.getCount(); i++) {
    View view = adapter.getView(i, null, listViewReplacement);
    listViewReplacement.addView(view);
}
LinearLayout listViewReplacement=(LinearLayout)findViewById(R.id.listViewReplacement);
NamesRowItemAdapter=新的NamesRowItemAdapter(此,namesInList);
对于(inti=0;i

对我来说,这很好,因为我只需要显示从1到5个元素的动态数据。我只需要添加自己的分隔符。

正如@Alex在接受的答案中指出的那样,LinearLayout很难替代。我遇到了一个问题,LinearLayout不是一个选项,那就是我遇到这个问题的时候。我将把代码放在这里作为参考希望它能帮助其他人

public class UIUtils {

    /**
     * Sets ListView height dynamically based on the height of the items.
     *
     * @param listView to be resized
     * @return true if the listView is successfully resized, false otherwise
     */
    public static boolean setListViewHeightBasedOnItems(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                View item = listAdapter.getView(itemPos, null, listView);
                item.measure(0, 0);
                totalItemsHeight += item.getMeasuredHeight();
            }

            // Get total height of all item dividers.
            int totalDividersHeight = listView.getDividerHeight() *
                    (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            listView.setLayoutParams(params);
            listView.requestLayout();

            return true;

        } else {
            return false;
        }

    }
}

您可以创建自己的customlistview。(它可以扩展ListView/ExpandableListView/GridView)并用它覆盖onMeasure方法。这样您就不需要调用函数或任何东西。只需在xml中使用它

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

如果所有项目的高度相同

        int totalItemsHeight = baseDictionaries.size() * item.getMeasuredHeight();
        int totalDividersHeight = listView.getDividerHeight() * (baseDictionaries.size() - 1);
        int totalPadding = listView.getPaddingBottom() + listView.getPaddingTop();

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) listTranslationWords.getLayoutParams();
        lp.height = totalItemsHeight + totalDividersHeight + totalPadding;
        listTranslationWords.setLayoutParams(lp);

我希望没有人看到这一点。你不能在同一个布局上有两个卷轴。首先你有一个卷轴视图,然后你有一个列表,我打赌你正在扼杀一些android的良好实践。

如果有人仍然有问题,那么你可以制作customList并添加
onMesure()
方法,就像我实现的那样:

public class ScrolleDisabledListView extends ListView {

private int mPosition;

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

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

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

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

如果您想在不扩展ListView类的情况下简单地解决这个问题,这是一个适合您的解决方案

 mListView.post(new Runnable() {
        @Override
        public void run() {
            int height = 0;
            for(int i = 0; i < mListView.getChildCount();i++)
                height += mListView.getChildAt(i).getHeight();
            ViewGroup.LayoutParams lParams = mListView.getLayoutParams();
            lParams.height = height;
            mListView.setLayoutParams(lParams);
        }
    });
mListView.post(新的Runnable(){
@凌驾
公开募捐{
整数高度=0;
对于(int i=0;i
我没有静态标题,但以HussoM的帖子为线索,以下是我能够完成的工作。在我的场景中,列表中项目的高度是不一致的,因为每个项目中都有可变的文本句子,我使用wrap\u content作为高度,match\u parent作为宽度

public class NonScrollableListView extends ListView {

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

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

  public NonScrollableListView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
  }

  public NonScrollableListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
  }

  /**
   * Measure the height of all the items in the list and set that to be the height of this
   * view, so it appears as full size and doesn't need to scroll.
   * @param widthMeasureSpec
   * @param heightMeasureSpec
   */
  @Override
  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      ListAdapter adapter = this.getAdapter();
      if (adapter == null) {
          // we don't have an adapter yet, so probably initializing.
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
          return;
      }

      int totalHeight = 0;

      // compute the height of all the items
      int itemCount = adapter.getCount();
      for (int index=0; index<itemCount; index++) {
          View item = adapter.getView(index, null, this);
          // set the width so it can figure out the height
          item.measure(widthMeasureSpec, 0);
          totalHeight += item.getMeasuredHeight();
      }

      // add any dividers to the height
      if (this.getDividerHeight() > 0) {
          totalHeight += this.getDividerHeight() * Math.max(0, itemCount - 1);
      }

      // make it so
      this.setMeasuredDimension(widthMeasureSpec,
              MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
  }
public类NonScrollableListView扩展了ListView{
公共非CrollableListView(上下文){
超级(上下文);
}
公共非CrollableListView(上下文、属性集属性){
超级(上下文,attrs);
}
公共非CrollableListView(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共非CrollableListView(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
}
/**
*测量列表中所有项目的高度,并将其设置为该项目的高度
*视图,因此它显示为全尺寸,不需要滚动。
*@param widthmasurespec
*@param heightmeaspect
*/
@凌驾
测量时的公共空隙(int-widthMeasureSpec,int-heightMeasureSpec){
ListAdapter=this.getAdapter();
if(适配器==null){
//我们还没有适配器,所以可能正在初始化。
超级测量(宽度测量、高度测量);
返回;
}
int totalHeight=0;
//计算所有项目的高度
int itemCount=adapter.getCount();
对于(int索引=0;索引0){
totalHeight+=this.getDividerHeight()*Math.max(0,itemCount-1);
}
//就这样吧
此.setMeasuredDimension(widthMeasureSpec,
MeasureSpec.makeMeasureSpec(总高度,精确测量));
}

}

在我的例子中,我在
ScrollView
中有
ListView
,而ScrollView在默认情况下是收缩ListView的。所以我只是在我的
ScrollView
中添加了这个,它对我很有效

android:fillViewport="true"

在另一个论坛上,我发现了类似这样的评论:“因为ListView已经有了滚动功能。因此我重写了我的自定义列表以继承LinearLayout,这在ScrollView中运行良好。我不知道这是否是唯一的方法,但它实现了我现在想要做的。”你们能帮我编写上述场景的代码吗?你们能给我们提供一些代码和xml布局吗?在这些代码和布局中,你们可以实现整个屏幕的滚动…谢谢…不,很遗憾,这不会有帮助-ListView总是试图滚动它的
android:fillViewport="true"