在textView更改大小后调整listView元素的大小

时间:2014-09-05 08:18:47

标签: android android-listview textview expandablelistview

我有下一个问题:我将expandable TextView添加到我的listView元素中。现在我想在文本视图扩展后调整listView元素的大小。 ExpandableTextView工作正常,但现在listView元素的大小没有改变。我的listView元素是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="12dp"
android:paddingTop="5dip">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_dish"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:clickable="true"></ImageView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingLeft="10dp">

        <TextView
            android:id="@+id/tv_dish_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <khuta.zavtrakov.other.ExpandableTextView
            android:id="@+id/tv_dish_description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


        <TextView
            android:id="@+id/tv_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
</LinearLayout>

我的适配器是:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;

ImageLoaderConfiguration config;
DisplayImageOptions options;
ImageLoader imageLoader;

public ExpandableListAdapter(Context pContext,
                             ExpandableListView pExpandableListView,
                             List<GroupEntity> pGroupCollection) {
    mContext = pContext;
    mGroupCollection = pGroupCollection;
    mExpandableListView = pExpandableListView;
    groupStatus = new int[mGroupCollection.size()];
    config = new ImageLoaderConfiguration.Builder(mContext)
            // You can pass your own memory cache implementation
            .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
            .build();

    options = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(360)) //rounded corner bitmap
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

    imageLoader = ImageLoader.getInstance();
    setListEvent();
}

private void setListEvent() {

    mExpandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    // TODO Auto-generated method stub
                    groupStatus[groupPosition] = 1;
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    // TODO Auto-generated method stub
                    groupStatus[groupPosition] = 0;
                }
            });
}

@Override
public Dish getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                         ViewGroup parent) {
    Dish child = (Dish) getChild(groupPosition, childPosition);
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.list_child_item, null);

    } else {
    }
    if (isLastChild) {
        convertView.findViewById(R.id.divider_description_child).setVisibility(View.GONE);
    } else {
        convertView.findViewById(R.id.divider_description_child).setVisibility(View.VISIBLE);
    }
    ((TextView) convertView.findViewById(R.id.tv_dish_name)).setText(child.getName());
    final ExpandableTextView expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.tv_dish_description);
    expandableTextView.setText(child.getDescription());
    expandableTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            expandableTextView.trim = !expandableTextView.trim;
            expandableTextView.setText();
            notifyDataSetChanged();

        }
    });

    ((TextView) convertView.findViewById(R.id.tv_price)).setText(String.valueOf(child.getPrice()));
    imageLoader.init(config);
    imageLoader.displayImage(child.getImageUrl(), (ImageView) convertView.findViewById(R.id.iv_dish), options);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(groupPosition).GroupItemCollection.size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return mGroupCollection.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return mGroupCollection.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    GroupEntity group = (GroupEntity) getGroup(groupPosition);
    GroupHolder groupHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                null);
        groupHolder = new GroupHolder();

        ((TextView) convertView.findViewById(R.id.tv_group_name)).setText(group.Name);
        if (groupPosition == 0) {
            ((ImageView) convertView.findViewById(R.id.iv_group_backgraund)).setVisibility(View.GONE);
        }
        convertView.setTag(groupHolder);
    } else {
        groupHolder = (GroupHolder) convertView.getTag();
    }

    return convertView;
}

class GroupHolder {
    ImageView img;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

}

3 个答案:

答案 0 :(得分:0)

我认为你必须使用这种方法:

youradapter.notifyDataSetChanged();

告诉您的适配器&#34;刷新&#34;你的小部件(比如你的可扩展)。

希望有所帮助。

修改

youradapter.notifyDataSetChanged();

您必须在主代码中创建适配器的对象。实例。 之后,您应该在ExpandableTextView对象上设置Adapter。 最后,当您添加内容时,只需调用适配器对象通知功能。

答案 1 :(得分:0)

因此,在ExpandableTextView类中,更简单的方法是更改​​:

private boolean trim = true;

public boolean trim = true;

然后,在您的活动中添加以下内容:

expandableTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                expandableTextView.trim = !expandableTextView.trim;
                expandableTextView.setText();
                expandableTextView.requestFocusFromTouch();

                //important thing
                youradapter.notifyDataSetChanged();
            }
        });

这应该可以解决问题。

答案 2 :(得分:0)

我认为问题是ix adapter.notifyDataSetChanged()。

请参阅this