【问题标题】:Resize listView element after textView changed it size在 textView 更改其大小后调整 listView 元素的大小
【发布时间】:2014-10-30 03:30:03
【问题描述】:

我遇到了下一个问题:我将 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;
}

}

【问题讨论】:

标签: android android-listview textview expandablelistview


【解决方案1】:

我认为问题在于 adapter.notifyDataSetChanged()。

this

【讨论】:

    【解决方案2】:

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

    youradapter.notifyDataSetChanged();
    

    这告诉您的适配器“刷新”您的小部件(例如您的可扩展小部件)。

    希望有所帮助。

    编辑

    youradapter.notifyDataSetChanged();
    

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

    【讨论】:

    • 看看这个链接codexplo.wordpress.com/2013/09/07/android-expandable-textview 我必须把 notifyDataSetChanged() 放在哪里?
    • 在您的setText() 呼叫之后。
    • 观看我编辑的答案,我已将 notifyDataSetChabged 添加到我的适配器,但对 mt 没有帮助
    • 看我的,你没有在你的适配器上使用通知。你没有任何适配器,这就是原因。您应该在之前创建一个,例如:Adapter myAdapter = new Adapter()... 和 myAdapter.notifySetDataChanged 之后。
    • 不,它是 udapter,看我编辑的问题。我想从它那里通知DataSetChanged,而不是从活动中
    【解决方案3】:

    所以,更简单的方法是在您的 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();
                }
            });
    

    这应该可以解决问题。

    【讨论】:

    • 观看我编辑的答案,我已将 notifyDataSetChabged 添加到我的适配器,但对 mt 没有帮助
    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2017-07-28
    • 1970-01-01
    • 2012-12-02
    • 2022-12-03
    相关资源
    最近更新 更多