【问题标题】:Add a new item to a listview将新项目添加到列表视图
【发布时间】:2012-04-22 10:16:11
【问题描述】:

我不太确定我的问题是否真的与listview 有关。有一个名为Gleeo Time Tracker 的应用程序,here 有它的屏幕视图。当你按下符号"+" 时,一个新的项目将被创建,您可以通过按"-"删除一个项目。更多的是当我点击项目左侧的记录按钮时,背景会改变。

我的问题是,列表视图到底是什么?我怎样才能做到这一点?谢谢大家!

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您想要做的是构建一个自定义 ListView。 这是通过提供一个布局文件来完成的,例如

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent">
    
      <ImageView android:id="@+id/Plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" />
    
      <TextView android:id="@+id/Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" />
    
    </LinearLayout>
    

    然后通过覆盖其 Adapter 的 getView() 方法将此布局应用于 ListView 的每一行,例如:

    public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);  
                }    
                ImageView plus = (ImageView) v.findViewById(R.id.plus);
                icon.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.plus));
                TextView title = (TextView) v.findViewById (R.id.Browse_FileName);
    
    // add a listener to your button and you're done
    
                return v;
            }
    

    【讨论】:

      【解决方案2】:

      阅读开发者文档中的列表视图,这里是example

      【讨论】:

        猜你喜欢
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 2016-11-24
        相关资源
        最近更新 更多