ExpandableListView如何在Fragment中工作

时间:2014-09-27 17:06:03

标签: android android-fragments expandablelistview

我有一个带有2组的自定义expandableListView。当我扩展第一组时一切都很好;但当我展开第二组时,它再次显示第一组的内容。我该如何解决这个问题?

public class MiCuentaActivity extends Fragment {


    Usuario user;

     ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<HistorialOferta>> listDataChild;
        HashMap<String, List<HistorialOferta>> listWonSubastas;


     public MiCuentaActivity(Usuario user) {
        // TODO Auto-generated constructor stub
         this.user=user;
    }

     public MiCuentaActivity(){

     }

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.activity_mi_cuenta, container, false);

            //StrictMode.enableDefaults();

            expListView= (ExpandableListView) rootView.findViewById(R.id.listaExpan);

            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getActivity(), arg0+" expandeed", Toast.LENGTH_SHORT).show();
                }
            });

            new TareaSync().execute(new ApiConnector("cuenta", this.user));

            return rootView;
        }




    private class TareaSync extends AsyncTask<ApiConnector, Void, HashMap<String, List<HistorialOferta>>>{

        @Override
        protected HashMap<String, List<HistorialOferta>> doInBackground(ApiConnector... params) {
            // TODO Auto-generated method stub
            listDataHeader=new ArrayList<String>();
            listDataChild=new HashMap<String, List<HistorialOferta>>();

            //ApiConnector api=new ApiConnector("cuenta", this.user);
            List<HistorialOferta> ofertas=params[0].getCuenta();
            List<HistorialOferta> lstWon=params[0].getWon();

            listDataHeader.add("Historial Ofertas");
            listDataHeader.add("Subastas Ganadas");



            listDataChild.put(listDataHeader.get(0), ofertas);
            listDataChild.put(listDataHeader.get(1), lstWon);

            return listDataChild;

        }

        @Override
        protected void onPostExecute( HashMap<String, List<HistorialOferta>> list) {
            // TODO Auto-generated method stub
            setListAdapter(list);
        }

        private void setListAdapter( HashMap<String, List<HistorialOferta>> list) {
            // TODO Auto-generated method stub
            expListView.setAdapter(new ExpandableList(getActivity().getApplicationContext(), listDataHeader, list));
        }




    }
}

适配器:

@Override
public Object getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return this.listOfertas.get(this._listDataHeader.get(arg0)).get(arg1);
}


@Override
    public View getChildView(int groupId, int childId, boolean arg2,
            View convertView, ViewGroup arg4) {
        // TODO Auto-generated method stub

        HistorialOferta ho = (HistorialOferta) getChild(groupId, childId);

        TableLayout table;

        if (convertView == null) {

            if(groupId==0){

            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater
                    .inflate(R.layout.list_expand_item, null);

            table = (TableLayout) convertView.findViewById(R.id.maintable);

            }

            if(groupId==1){

                LayoutInflater infla=(LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView=infla.inflate(R.layout.list_expand_won_item, null);

                table=(TableLayout) convertView.findViewById(R.id.maintableWon);

            }



}


return convertView;




        }

1 个答案:

答案 0 :(得分:0)

This might help you out.

此外,请注意,在ListView中,视图会在以后回收,因此您需要确保实际重置所有内容。使用ViewHolder模式应该有所帮助。谷歌那个术语可以找到更多的例子。

你也可以认为你的HistorialOferta对象包含你需要的两个布局值,所以你可以这样说,而不是使用if(groupId==0)代码:

convertView = infalInflater.inflate(ho.getViewId(), null)
table = (TableLayout) convertView.findViewById(ho.getTableViewID());

此外,除了让视图膨胀之外,你实际上从未对convertView做任何事情;这是你的意图吗?