【问题标题】:How to show Fixed Number of Rows in a listView如何在列表视图中显示固定数量的行
【发布时间】:2013-05-24 10:41:39
【问题描述】:

我只想在 ListView 中显示 5 行,其余的应该是 scrollable。到目前为止我已经尝试过这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/txtFilename"
        android:layout_width="375dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="2dp"
        android:

        /> 



     <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray"
        android:padding="5dp" >

        <Button 
            android:id="@+id/btnOk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK"/>

        <Button 
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CANCEL"/>
    </LinearLayout>  


</LinearLayout>

java code

public class MainActivity extends Activity {

    private ListView list1;

    private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

    "item 1", "item 2", "item3", "item 4" };
    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //parentLayout.add(childLayout);

        setContentView(R.layout.activity_main);
        list1 = (ListView) findViewById(R.id.ListView01);

        // By using setAdpater method in listview we an add string array in

        // list.

        list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));

    }

知道如何实现它

【问题讨论】:

  • Android 手机有不同的屏幕尺寸。所以在小尺寸屏幕上你怎么能显示 5 个项目。
  • 你可以检查这个:这可能是重复的:stackoverflow.com/questions/7509929/…
  • @Sandy 这些我已经检查过对我没有帮助。
  • 您可以使用两个不同的列表视图使第一个固定并在下一个列表视图中设置剩余项目
  • @vsk 假设我没有使用小屏幕移动设备,而是使用平板电脑。

标签: android android-listview


【解决方案1】:

我只是在修改你的Layout 看看吧!!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/txtFilename"
        android:layout_width="375dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
<LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="300dip">
    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="2dp"


        /> 


</LinearLayout>
     <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray"
        android:padding="5dp" >

        <Button 
            android:id="@+id/btnOk"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK"/>

        <Button 
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CANCEL"/>
    </LinearLayout>  


</LinearLayout>

【讨论】:

  • 感谢我的时间正是我想要的。
【解决方案2】:

我已经为这个 Like 编写了 Demo:

如果您想将 ListView 的高度固定为 5 个项目。那么你必须通过计算listItems 高度来务实地设置ListView 的高度。

首先你需要为列表项创建布局并给它一些固定的高度,这里我在Tag 中添加了高度,因为runtime 我将布局高度设置为0。

这是ListItem 布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:background="@android:color/black"
    android:tag="40"
    android:layout_height="40dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XXX"
        android:textColor="@android:color/darker_gray"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/text"
        android:text="ZZZ"
        android:textColor="@android:color/darker_gray"
        android:textSize="30sp" />

</RelativeLayout>

并设置ListView的高度:

list    =   (ListView) findViewById(R.id.listView1);

    adapter = new Adapter(MainActivity.this, setter);
    list.setAdapter(adapter);        

    RelativeLayout.LayoutParams lstViewParams = (LayoutParams) list.getLayoutParams();        
    View lisItemInflatedView = View.inflate(this, R.layout.item, null);        
    RelativeLayout relLayoutInLstItem = (RelativeLayout) lisItemInflatedView.findViewById(R.id.relativeLayout);       
    final float scale = this.getResources().getDisplayMetrics().density;
    int pixels = (int) ( Integer.parseInt( relLayoutInLstItem.getTag().toString() ) * scale + 0.5f);    
    int listHeigt = pixels  * 5 + 5;       
    lstViewParams.height = listHeigt;

这是截图..

希望这会有所帮助。

【讨论】:

  • 它是我的 ListItem.. 我可以在其中输入姓名和联系电话。您需要担心您的列表项中有多少视图。只需获取列表项的高度并将其相乘..
  • 我不能。我给你的已经够多了。你需要付出一些努力。
  • 如果你想在列表视图中固定列表视图项目的数量,这非常简单,你可以将列表视图放在线性布局中。
  • 如果他想显示大约 5 个项目,他可以使用 300dip 显示近 5 个项目,如果他有超过 5 个项目,其余的将作为可滚动的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多