Android---ListView只显示一行的解决方案

Android编程中,ScrollView嵌套ListView时,会无法正确的计算ListView的大小。解决的办法有如下两种:

解决方案1:直接把包含ListView控件的ScrollView控件从布局文件中去除,留下ListView控件,这是最简单快捷的解决办法,如果一定要在ScrollView中包含ListView,则参考解决方案2:

去除Scroll之前的XML:

<LinearLayout 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:background="#FFFFFF"   
    android:orientation="vertical" >   
    <ScrollView   
        android:layout_width="match_parent"   
        android:layout_height="match_parent" >   
        <LinearLayout   
            android:layout_width="match_parent"   
            android:layout_height="match_parent" >   
            <ListView   
                android:id="@+id/test_listView"   
                android:layout_width="match_parent"   
                android:layout_height="match_parent"   
                android:fadingEdge="vertical"/>   
        </LinearLayout>   
    </ScrollView>   
</LinearLayout> 

去除ScrollView之后的XML:

<LinearLayout 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:background="#FFFFFF"   
    android:orientation="vertical" >   
        <LinearLayout   
            android:layout_width="match_parent"   
            android:layout_height="match_parent" >   
            <ListView   
                android:id="@+id/test_listView"   
                android:layout_width="match_parent"   
                android:layout_height="match_parent"   
                android:fadingEdge="vertical"  />   
        </LinearLayout>   
</LinearLayout>   

解决方案2:通过代码,根据当前的ListView的列表子项计算列表的高度:

public class MainActivity extends Activity {   
    private ListView listView;  
    private String[] adapterData = new String[] { "标题1", "标题2", "标题3"}; 
    
   @Override   
    protected void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.mainActivity);   
        listView = (ListView) findViewById(R.id.test_listView);   
        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));   
        fixListViewHeight(listView);   
    }   
   
    public void fixListViewHeight(ListView listView) {   
        // 如果没有设置数据适配器,则ListView没有子项,返回。  
        ListAdapter listAdapter = listView.getAdapter();  
        int totalHeight = 0;   
        if (listAdapter == null) {   
            return;   
        }   
        for (int index = 0, len = listAdapter.getCount(); index < len; index++) {     
            View listViewItem = listAdapter.getView(index , null, listView);  
            // 计算子项View 的宽高   
            listViewItem.measure(0, 0);    
            // 计算所有子项的高度和
            totalHeight += listViewItem.getMeasuredHeight();    
        }   
   
        ViewGroup.LayoutParams params = listView.getLayoutParams();   
        // listView.getDividerHeight()获取子项间分隔符的高度   
        // params.height设置ListView完全显示需要的高度    
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));   
        listView.setLayoutParams(params);   
    }   
}   

原文链接: http://jingyan.baidu.com/article/afd8f4de4695af34e386e969.html


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Android ListView的一行显示多个元素,可以使用自定义布局。以下是如何实现它的步骤: 1. 创建一个自定义布局文件,例如row_layout.xml。在此文件中,可以添加多个元素,例如ImageView、TextView等。 2. 在适配器中重写getView()方法。在此方法中,可以将自定义布局文件中的元素与数据绑定,并将其添加到ListView的每一行中。 3. 在Activity中,将适配器绑定到ListView上。 以下是一个示例代码: row_layout.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="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout> ``` CustomAdapter.java: ``` public class CustomAdapter extends ArrayAdapter<String> { public CustomAdapter(Context context, ArrayList<String> data) { super(context, 0, data); } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = convertView; if (itemView == null) { LayoutInflater inflater = LayoutInflater.from(getContext()); itemView = inflater.inflate(R.layout.row_layout, parent, false); } ImageView imageView = itemView.findViewById(R.id.imageView); TextView textView = itemView.findViewById(R.id.textView); // 绑定数据 String item = getItem(position); imageView.setImageResource(R.drawable.icon); textView.setText(item); return itemView; } } ``` MainActivity.java: ``` public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); ArrayList<String> data = new ArrayList<>(); data.add("Item 1"); data.add("Item 2"); data.add("Item 3"); CustomAdapter adapter = new CustomAdapter(this, data); listView.setAdapter(adapter); } } ``` 在此示例中,我们创建了一个自定义布局文件row_layout.xml,其中包含一个ImageView和一个TextView。然后,我们在CustomAdapter中重写了getView()方法,将数据绑定到自定义布局文件中的元素上,并将每个元素添加到ListView的每一行中。最后,在MainActivity中,我们将适配器绑定到ListView上,以显示多个元素的每一行

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值