它只显示child中的一个父项,它首先在expandableListview的所有其他父项上单击

时间:2015-11-09 05:30:05

标签: android expandablelistview expandablelistadapter

我的下面的代码只显示了孩子中的一个父项,该项首先点击expandableListview的所有其他父项

    public class AffiliationAdapter extends BaseExpandableListAdapter {
        Context context;
        LayoutInflater inflater;

        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private ArrayList<HashMap<String, String>> _listDataChild;
        DbFields dbFields = new DbFields();


        public AffiliationAdapter(Context activity, List<String> listDataHeader, ArrayList<HashMap<String, String>> listDataChild) {

            this.context = activity;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listDataChild;
        }


        @Override
        public void registerDataSetObserver(DataSetObserver dataSetObserver) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

        }

        @Override
        public int getGroupCount() {
            return this._listDataHeader.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(groupPosition).get(childPosititon);
        }

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

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }



        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {


            if (view == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = infalInflater.inflate(R.layout.faq_group_expand, null);
            }

            TextView getFaqAnswer = (TextView) view.findViewById(R.id.answer);

            getFaqAnswer.setTypeface(null, Typeface.BOLD);
            getFaqAnswer.setText((_listDataHeader.get(i)));

            return view;
        }

        @Override
        public int getChildType(int groupPosition, int childPosition)
        {
            int result = 0;
            if (groupPosition == getGroupCount()-1)//logical fail ?
            {
                result = 1;
            }
            return result;

        }



        @Override
        public View getChildView(final int i, int i2, boolean b, View v, ViewGroup viewGroup) {

            if (v==null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.child_affiliation, null);
                int itemType = getChildType(i, i2);


                LinearLayout layout_about = (LinearLayout) v.findViewById(R.id.layout_about);
                LinearLayout layout_vc = (LinearLayout) v.findViewById(R.id.layout_vc);
                LinearLayout layout_registrar = (LinearLayout) v.findViewById(R.id.layout_registrar);
                LinearLayout layout_examination_controller = (LinearLayout) v.findViewById(R.id.layout_examination_controller);
                LinearLayout layout_link = (LinearLayout) v.findViewById(R.id.layout_link);

                TextView about_detail = (TextView) v.findViewById(R.id.about_detail);
                TextView vc_name = (TextView) v.findViewById(R.id.vc_name);
                TextView vc_Contact = (TextView) v.findViewById(R.id.vc_Contact);
                TextView vc_email = (TextView) v.findViewById(R.id.vc_email);

                TextView registrar_name = (TextView) v.findViewById(R.id.registrar_name);
                TextView registarar_Contact = (TextView) v.findViewById(R.id.registarar_Contact);
                TextView registrar_email = (TextView) v.findViewById(R.id.registrar_email);


                TextView controller_name = (TextView) v.findViewById(R.id.controller_name);
                TextView controller_Contact = (TextView) v.findViewById(R.id.controller_Contact);
                TextView controller_email = (TextView) v.findViewById(R.id.controller_email);
                TextView link = (TextView) v.findViewById(R.id.link);

                about_detail.setText(_listDataChild.get(i2).get(dbFields.AFFILIATION_DESCRIPTION));
                vc_name.setText(_listDataChild.get(i2).get(dbFields.VC_NAME));
                vc_Contact.setText(_listDataChild.get(i2).get(dbFields.VC_PHONE));
                vc_email.setText(_listDataChild.get(i2).get(dbFields.VC_EMAIL));

                registrar_name.setText(_listDataChild.get(i2).get(dbFields.REGISTRAR_NAME));
                registarar_Contact.setText(_listDataChild.get(i2).get(dbFields.REGISTRAR_PHONE));
                registrar_email.setText(_listDataChild.get(i2).get(dbFields.REGISTRAR_EMAIL));

                controller_name.setText(_listDataChild.get(i2).get(dbFields.EXAM_CONTROLLER_NAME));
                controller_Contact.setText(_listDataChild.get(i2).get(dbFields.EXAM_CONTROLLER_PHONE));
                controller_email.setText(_listDataChild.get(i2).get(dbFields.EXAM_CONTROLLER_EMAIL));

                link.setText("Notice Link :" +_listDataChild.get(i2).get(dbFields.AFFILIATION_NOTICE_LINK)+
                        "\n Result Link : "+_listDataChild.get(i2).get(dbFields.AFFILIATION_RESULT_LINK)+
                        " \n Schedule Link :"+_listDataChild.get(i2).get(dbFields.AFFILIATION_SCHEDULE_LINK));




                int grup_pos = (int) getGroupId(i);
                switch (grup_pos) {
                    case 0:
                        layout_link.setVisibility(View.GONE);
                        layout_examination_controller.setVisibility(View.GONE);
                        layout_registrar.setVisibility(View.GONE);
                        layout_vc.setVisibility(View.GONE);
                        layout_about.setVisibility(View.VISIBLE);
                        //   v = inflater.inflate(R.layout.dialog_contact, null);
                        Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        layout_link.setVisibility(View.GONE);
                        layout_examination_controller.setVisibility(View.GONE);
                        layout_registrar.setVisibility(View.GONE);
                        layout_vc.setVisibility(View.VISIBLE);
                        layout_about.setVisibility(View.GONE);
                        //   v = inflater.inflate(R.layout.item_faq_answer, null);
                        Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        layout_link.setVisibility(View.GONE);
                        layout_examination_controller.setVisibility(View.GONE);
                        layout_registrar.setVisibility(View.VISIBLE);
                        layout_vc.setVisibility(View.GONE);
                        layout_about.setVisibility(View.GONE);
                        //     v = inflater.inflate(R.layout.dialog_contact, null);
                        Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
                        break;
    //                case 3:
    //                    layout_link.setVisibility(View.GONE);
    //                    layout_examination_controller.setVisibility(View.GONE);
    //                    layout_registrar.setVisibility(View.GONE);
    //                    layout_vc.setVisibility(View.GONE);
    //                    layout_about.setVisibility(View.GONE);
    //               //     v = inflater.inflate(R.layout.events_items, null);
    //                    Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
    //                    break;
                    case 3:
                        layout_link.setVisibility(View.GONE);
                        layout_examination_controller.setVisibility(View.VISIBLE);
                        layout_registrar.setVisibility(View.GONE);
                        layout_vc.setVisibility(View.GONE);
                        layout_about.setVisibility(View.GONE);
                        //     v = inflater.inflate(R.layout.dialog_program, null);
                        Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
                        break;

                    case 4:
                        layout_link.setVisibility(View.VISIBLE);
                        layout_examination_controller.setVisibility(View.GONE);
                        layout_registrar.setVisibility(View.GONE);
                        layout_vc.setVisibility(View.GONE);
                        layout_about.setVisibility(View.GONE);
                        //    v = inflater.inflate(R.layout.dialog_program, null);
                        Toast.makeText(context.getApplicationContext(), "position is" + grup_pos, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
            //     v.invalidate();
            return v;
        }


        @Override
        public boolean isChildSelectable(int i, int i2) {
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void onGroupExpanded(int i) {
            super.onGroupExpanded(i);


        }

        @Override
        public void onGroupCollapsed(int i) {
            super.onGroupCollapsed(i);
        }

        @Override
        public long getCombinedChildId(long l, long l2) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long l) {
            return 0;
        }
    }

这是我的主要活动 我的下面的代码只显示了孩子中的一个父项,首先点击expandableListview的所有其他父项,这是主要活动

public class AffiliationActivity extends AppCompatActivity {
ExpandableListView expandableListView;
int previousItem = -1;


//variables to holds course entities
// Getting data from


LayoutInflater layoutInflater;
DbHelper dbHelper;
DbFields dbFields;
private ArrayList<HashMap<String, String>> affiliationChildList;
List<String> faqListHeader;
ScrollView scrollView;
LinearLayout mainLayout,header;
CC_Application application;
Intent intent;
String collegeId;
TextView estdDate,setting,location,type,totAffiliation,totStudent,universityName,fax,phone,email,staff_num;


private ArrayList<String> parentItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_affiliation);

    //initialise all fields

    expandableListView = (ExpandableListView) findViewById(R.id.expandListViewFaq);
    layoutInflater = getLayoutInflater();
    dbFields = new DbFields();
    dbHelper = new DbHelper(this);
    scrollView = (ScrollView) findViewById(R.id.scroll);
    mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
    header = (LinearLayout) findViewById(R.id.header);
    faqListHeader = new ArrayList<>();
    application= CC_Application.getInstance();

    estdDate = (TextView) findViewById(R.id.estd_date);
    setting = (TextView) findViewById(R.id.setting);
    location = (TextView) findViewById(R.id.location);
    type = (TextView) findViewById(R.id.type);
    totAffiliation = (TextView) findViewById(R.id.total_affiliation);
    totStudent = (TextView) findViewById(R.id.totStudent);
    universityName = (TextView) findViewById(R.id.titleC);
    fax = (TextView) findViewById(R.id.fax);
    phone = (TextView) findViewById(R.id.phone);
    email = (TextView) findViewById(R.id.email);
    staff_num = (TextView) findViewById(R.id.staff_num);

    intent = getIntent();
    if(intent!=null) {
        collegeId = intent.getStringExtra("getCollegeId").toString();
    }


    //set parent name for the child

    parentItems.add("About Us");
    parentItems.add("VC");
    parentItems.add("Registrar");
    parentItems.add("Examination Controller");
    parentItems.add("Links");

    //set actionbar customized logo

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.college_connect_logo));

    affiliationChildList = application.getAllAffiliationList(collegeId);

    //hold data in expandable listview through adapter
    AffiliationAdapter adapter =  new AffiliationAdapter(this, parentItems, affiliationChildList);

    expandableListView.setAdapter(adapter);

    estdDate.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_FOUNDATION_YEAR));
    setting.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_SETTING));
    universityName.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_NAME));
    type.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_TYPE));
    totAffiliation.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_AFFILIATED_COLLEGE_NUMBER));
    totStudent.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_ENROLLED_STUDENTS));
    location.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_ADDRESS));
    fax.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_FAX));
    phone.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_PHONE));
    email.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_EMAIL));
    staff_num.setText(affiliationChildList.get(0).get(dbFields.AFFILIATION_ACADEMIC_STAFF_NUMBER));

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousItem){
                expandableListView.collapseGroup(previousItem);
                previousItem = groupPosition;
                mainLayout.setVisibility(View.GONE);

            }
            else{
                expandableListView.collapseGroup(groupPosition);
                previousItem = -1;
                mainLayout.setVisibility(View.VISIBLE);

            }
        }
    });

    // Listview Group collasped listener
    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            mainLayout.setVisibility(View.VISIBLE);


            Toast.makeText(getApplicationContext(),"Group Collapsed",Toast.LENGTH_SHORT).show();

        }
    });

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_affiliation, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

包括主要活动。

2 个答案:

答案 0 :(得分:0)

我认为你必须通过适当的孩子大小而不是1.一个小小的改变:

来自:

@Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

@Override
    public int getChildrenCount(int groupPosition) {
        return _listDataChild .size();
    }

修改

当你还带着孩子的时候。

自:

 @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(groupPosition).get(childPosititon);
    }

要:

试试(不确定):

 @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(childPosititon);
    }

答案 1 :(得分:0)

我想在你的getChildView()方法中你已经在一个条件中编写代码,例如if(v==null),只需编写初始化代码,如果将数据分配给视图,则将该代码写入if条件之外。

当您点击下一个标题(父级)时,没有新视图会膨胀,因为此时间视图不会为空。