【问题标题】:How does ExpandableListView works in FragmentExpandableListView 如何在 Fragment 中工作
【发布时间】:2014-09-27 17:06:03
【问题描述】:

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

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;




        }

【问题讨论】:

  • 你真的认为它与片段有关吗?为什么?您是否在活动中尝试过相同的代码?
  • 是的,我尝试了同样的活动,一切正常

标签: android android-fragments expandablelistview


【解决方案1】:

This might help you out.

另外,要知道在ListViews 中,视图稍后会被回收,因此您需要确保您确实重置了所有内容。使用ViewHolder 模式应该会有所帮助。谷歌该词以查找更多示例。

您还可以让您的 HistorialOferta 对象包含您需要的两个布局值,因此您可以这样说,而不是使用 if(groupId==0) 代码:

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

此外,除了放大视图之外,您实际上从未对您的convertView 做任何事情;这是你想要的吗?

【讨论】:

  • 我已经调试了getChildView方法,问题就在这里: HistorialOferta ho = (HistorialOferta) getChild(groupId, childId);当我展开第二组时,这里 groupId 表示 0 而不是 1;这就是为什么在第二组中我看到来自第一组的相同数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多