【问题标题】:How to programmatically change the height of a list element after the list has been populated如何在填充列表后以编程方式更改列表元素的高度
【发布时间】:2012-08-03 13:02:33
【问题描述】:

我正在制作一份新闻列表。我的列表是一个扩展ListActivityActivity。我还创建了一个ArrayAdapter,用于扩充列表中的元素。 ArrayAdapter 使用一个简单的布局,一个ImageView 和两个TextViews。一切正常,但是当我覆盖 onListItemClick() 的方法并尝试调整列表元素的大小时,单击它们时没有任何反应。

protected void onListItemClick(ListView l, View v, int position, long id) {
    v.getLayoutParams().height=300;
}

我也用LayoutParams 尝试过上述方法

这是我用来填充列表的每一行的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:background="@color/white"
     >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        android:textSize="10dp"
        android:textStyle="bold" >

    </TextView>  

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textColor="@color/black"
        android:textColorHint="@color/black"
        android:textSize="10dp" >

    </TextView>
    </LinearLayout>

</LinearLayout> 

有人遇到过同样的问题吗? 任何人都可以为 Android 初学者提供一些启示吗?

【问题讨论】:

    标签: android android-layout android-listview android-arrayadapter


    【解决方案1】:

    我终于找到了解决问题的方法,我想与社区的其他人分享。

    我正在使用 arrayAdapter 来扩展我的行布局,下面是它的代码:

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;
        private final Bitmap[] images;
        private final String[] names;
    
        public MySimpleArrayAdapter(Context context, String[] values, Bitmap[] images, String [] names) {
            super(context, R.layout.rowlayout, values);
            this.context = context;
            this.values = values;
            this.images=images;
            this.names=names;
        }
    
    
        View rowView;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //View rowView;
            
            rowView = inflater.inflate(R.layout.rowlayout, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.label);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
            TextView title=(TextView) rowView.findViewById(R.id.title1);
            textView.setText(values[position]);
            imageView.setImageBitmap(images[position]);
            title.setText(names[position]);
            return rowView;
        }
    } 
    

    我的主要活动扩展了 ListActivity,其中列表的项目是从异步任务中的一些 json 文件中获取的。在 AsyncTask 的 onPostExecute 方法中,我的列表是使用获取的项目创建的。

    以下是 onListItemClick 方法的代码:

        @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        int hei=v.getLayoutParams().height;
        System.out.println("Height   "+hei);
        if (hei==LayoutParams.WRAP_CONTENT) {
            v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,105));
            v.requestLayout();
    
        }
        else {
            v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            v.requestLayout();
        }
    }
    

    诀窍是调用 requestLayout 方法来刷新你的布局。

    【讨论】:

    • 我一直在尝试在适配器的 getView 方法中设置 LayoutParams,以便将所有 ListView 项设置为相同的高度。不幸的是,执行此行时出现空指针异常:AbsListView.LayoutParams listViewRow_params = (AbsListView.LayoutParams) convertView.getLayoutParams(); 有关如何访问 getView 中的 LayoutParams 的任何建议?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多