【问题标题】:Embedding a sized ListView in activity - android在活动中嵌入一个大小的 ListView - android
【发布时间】:2011-08-22 15:27:50
【问题描述】:

我正在看这个教程:http://developer.android.com/resources/tutorials/views/hello-listview.html

是否可以将列表嵌入到我当前的活动中,以便我可以设置它的高度。我的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/mobile_vforum_bg"
    >
    <ImageView 
        android:src="@drawable/mobile_vforum" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/logo"
        android:layout_marginLeft="57px"
        android:layout_marginTop="10px">
    </ImageView>
    <TextView 
        android:layout_height="30px"
        android:layout_width="fill_parent"
        android:id="@+id/welcomeText"
        android:textSize="16px"
        android:textColor="#317c73"
        android:background="#eeeeee"
        android:shadowColor="#333333"
        android:shadowDx="1"
        android:shadowDy="1"
        android:paddingTop="4px"
        android:paddingLeft="7px">
    </TextView>
    <ListView 
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"
        android:id="@+id/projectList">
    </ListView>
</LinearLayout>

您可以在布局的末尾看到我的 ListView。基本上我有一个图像和一行文本,我希望列表位于其下方。我发布的教程链接扩展了 ListActivity,这是我感到困惑的地方,因为我在当前的扩展 Activity 的布局 java 文件中。任何帮助或建议表示赞赏。谢谢。

【问题讨论】:

    标签: android list listview


    【解决方案1】:

    是的,可以这样做。我已经做过几次了,我的方法是通过扩展 android.widget.BaseAdapter 为列表创建一个自定义适配器。这可能超出了您解决问题所需的范围,但希望这可以满足您的需求。

    这是主要的 Activity 布局:

    <?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:padding="6dip"
        android:background="@drawable/backrepeat"
    >
    <Button
        android:id="@+id/cat_done_button"
        android:layout_width="75dip"
        android:layout_height="50dip"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="6dip"
        android:text="@string/done"
    />
    <EditText
        android:id="@+id/category_entry"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:hint="@string/category_hint"
        android:layout_margin="10dip"
        android:layout_below="@+id/cat_done_button"
    />
    <ImageButton
        android:id="@+id/category_add_button"
        android:background="#00000000"
        android:src="@drawable/ic_menu_add_ld"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_marginLeft="5dip"
        android:scrollbars="vertical"
        android:fadingEdge="vertical"
        android:layout_alignBottom="@+id/category_entry"
        android:layout_toRightOf="@+id/category_entry"
    />
    <ListView  
        android:id="@+id/category_list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dip"
        android:layout_marginBottom="5dip"
        android:background="#00000000"
        android:layout_below="@+id/category_notification"
    />
    </RelativeLayout>
    

    这是列表中每一行的单独布局:

    <?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:padding="6dip"
        android:background="#00000000"
    >
    <TextView  
        android:id="@+id/category_name"
        android:layout_width="200dip" 
        android:layout_height="wrap_content" 
        android:textSize="20sp"
        android:layout_marginTop="10dip"
        android:scrollbars="vertical"
        android:fadingEdge="vertical"
        android:layout_alignParentLeft="true"
        android:background="#00000000"
    />
    <ImageButton
        android:id="@+id/category_delete_button"
        android:background="#00000000"
        android:src="@drawable/ic_menu_delete_ld"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:scrollbars="vertical"
        android:fadingEdge="vertical"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
    </RelativeLayout>
    

    以下是主要活动(CategoryActivity)如何设置其 List 对象:

    private void setUpCategoryList() {
            categoryList = (ListView) this.findViewById(R.id.category_list);
    
            categoryAdapter = new CategoryAdapter(this, android.R.layout.simple_list_item_1,
                    categoryManager.getCategoriesList());
            categoryList.setAdapter(categoryAdapter);
        }
    

    这是自定义的 CategoryAdapter:

    public class CategoryAdapter extends BaseAdapter implements OnClickListener {
    
        private CategoryActivity catActivity;
        private List<String> categoryList;
        private final String CAT_DELETED = "Category has been deleted.";
        private final String DEFAULT_DELETE = "Cannot delete Default category.";
        private final String NON_EMPTY_DELETE = "Category has entries.  Entries re-assigned to Default category.";
    
        public CategoryAdapter(CategoryActivity catActivity, int resource, List<String> items) {
            this.catActivity = catActivity;
            categoryList = items;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String category = categoryList.get(position);
    
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) catActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.category_row_layout, null);
            }
    
            TextView categoryView = (TextView) convertView.findViewById(R.id.category_name);
            categoryView.setText(category);
    
            ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.category_delete_button);
            deleteButton.setOnClickListener(this);
            deleteButton.setTag(category);
    
            return convertView;
        }
    
        @Override
        public void onClick(View view) {
    //Do stuff here
    }
    

    【讨论】:

    • 是的,这对我来说有点难以接受。我对这个东西很陌生,但我很感激你的回应。我正在尝试将其适应我当前的代码,对我来说如此简单。首先,如何导入 CategoryAdapter 以便在 Main.java 中解析类型?另外,我在哪里保存 CategoryAdapter.java?我在包含我的 Main.java 文件的 project/src/title/ 文件夹中创建了一个新的 Class 文件,但它没有解析
    • 我想通了。一个简单的 ArrayAdapter 工作正常。 projectList.setAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, COUNTRIES));
    • 上面的 CategoryAdapter 类只是自定义适配器类的示例。我同意这个例子可能比你需要的要多。如果您想在列表中添加多个字符串,您会这样做。例如,您可以在列表行中添加几个视图或一个按钮。你所做的也应该有效。
    猜你喜欢
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多