【问题标题】:Dynamically add views to Child of ExpandableListView将视图动态添加到 ExpandableListView 的子级
【发布时间】:2014-02-14 17:05:21
【问题描述】:

大家好,

我正在尝试将一些动态生成的视图从 ExpandableListView 添加到 ChildView 的 Linearlayout。代码是:

 @Override
 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    if (convertView == null)
        convertView = inflater.inflate(R.layout.child_fragment_item_menu_child_view, null);

    TextView childName                              = (TextView) convertView.findViewById(R.id.childName);
    TextView childDetails                           = (TextView) convertView.findViewById(R.id.childDetails);
    LinearLayout childPriceWeightHolder_Single      = (LinearLayout) convertView.findViewById(R.id.childPriceWeightHolder_Single);
    LinearLayout childPriceWeightHolder_Multiple    = (LinearLayout) convertView.findViewById(R.id.childPriceWeightHolder_Multiple);

    JSONObject menuItem = null;
    try {
        menuItem = new JSONObject( getChild(groupPosition, childPosition) );
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if(menuItem != null){
        childName.setText( menuItem.optString("name") );
        childDetails.setText( menuItem.optString("description") );

        final TextView tv1 = new TextView(context), tv2 = new TextView(context);

        if( menuItem.optJSONArray("advance_prices") == null || menuItem.optJSONArray("advance_prices").length() == 0 ){
            if(menuItem.optString("price") != null && menuItem.optString("price").compareToIgnoreCase("null") != 0){
                tv1.setText(menuItem.optString("price") + " " + menuItem.optString("currency_name"));
                tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
            }

            if(menuItem.optString("weight") != null && menuItem.optString("weight").compareToIgnoreCase("null") != 0){
                tv2.setText(menuItem.optString("weight"));
                tv2.setTextColor(context.getResources().getColor(R.color.gray));
                tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
            }

            childPriceWeightHolder_Single.addView(tv1);
            childPriceWeightHolder_Single.addView(tv2);
        }else{
            // COMPOSED VIEW(multiple prices and multiple weights, etc...)
        }
    }

    return convertView;
 }

问题在于,在第一页之后,适配器生成的新子项会保留对这些动态添加的 textViews 的引用或其他内容,并且会弄乱一切。我试图添加 tv1.setId(..);但它没有用。

有什么解决办法吗?谢谢!

【问题讨论】:

  • 完成了!只需添加 childPriceWeightHolder_Single.removeAllViews();到你想要膨胀(和之前膨胀)的视图,所以它只添加当前动态生成的视图!

标签: android expandablelistview


【解决方案1】:

expandablelistview 的基本示例是.. here....

在您的 Activity 中实现 OnChildClickListener 并实现此方法..

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

   //Add new chid data in your list (ex ArrayLis or Array whatever you use)
   adapter.notifyDataSetChanged() // BaseExpandableListAdapter's adapter...


return true;
}

它对你有用吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2016-05-16
    • 2012-04-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多