So I'm a beginner on android, and after reading some tutorial about ExpandableList online, I've made a FAQ list in my app.
However, I would like to know is it possible to add unique parent and unique child items to ExpandableList by reading xml array? For example with a question array and answer array in the xml, and the answer will appear in the same order of question list, like this:
<string-array name="faq_question"> <item>Q1</item> <item>Q2</item> ... </string-array> <string-array name="faq_answer"> <item>A1</item> <item>A2</item> ... </string-array>
Currently, I have to put the answer string to their respective question manually like this:
ExpandableListDataPump.java
public class ExpandableListDataPump extends AppCompatActivity { public static HashMap<String, List<String>> getData() { HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); String q1 = MainActivity.getContext().getResources().getString(R.string.faq_q1); String q2 = MainActivity.getContext().getResources().getString(R.string.faq_q2); String q3 = MainActivity.getContext().getResources().getString(R.string.faq_q3); String a1 = MainActivity.getContext().getResources().getString(R.string.faq_a1); String a2 = MainActivity.getContext().getResources().getString(R.string.faq_a2); String a3 = MainActivity.getContext().getResources().getString(R.string.faq_a3); List<String> qt3 = new ArrayList<String>(); qt3.add(a3); List<String> qt2 = new ArrayList<String>(); qt2.add(a2); List<String> qt1 = new ArrayList<String>(); qt1.add(a1); expandableListDetail.put(q1, qt1); expandableListDetail.put(q2, qt2); expandableListDetail.put(q3, qt3); return expandableListDetail; } }
The adapter - ExpandableListAdapter.java
public class ExpandableListAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter { private Context context; private List<String> expandableListTitle; private HashMap<String, List<String>> expandableListDetail; public ExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) { this.context = context; this.expandableListTitle = expandableListTitle; this.expandableListDetail = expandableListDetail; } @Override public Object getChild(int listPosition, int expandedListPosition) { return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) .get(expandedListPosition); } @Override public long getChildId(int listPosition, int expandedListPosition) { return expandedListPosition; } @Override public View getRealChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String expandedListText = (String) getChild(listPosition, expandedListPosition); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.activity_help_faq_content, null); } TextView expandedListTextView = (TextView) convertView .findViewById(R.id.child_list); expandedListTextView.setText(expandedListText); return convertView; } @Override public int getRealChildrenCount(int listPosition) { return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) .size(); } @Override public Object getGroup(int listPosition) { return this.expandableListTitle.get(listPosition); } @Override public int getGroupCount() { return this.expandableListTitle.size(); } @Override public long getGroupId(int listPosition) { return listPosition; } @Override public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) { String listTitle = (String) getGroup(listPosition); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context. getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.activity_help_faq_title, null); } TextView listTitleTextView = (TextView) convertView .findViewById(R.id.header_list); listTitleTextView.setText(listTitle); ViewFlipper flip = (ViewFlipper) convertView.findViewById(R.id.arrow_flipper); if (isExpanded) { flip.setDisplayedChild(1); } else { flip.setDisplayedChild(0); } return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int listPosition, int expandedListPosition) { return false; } }
The activity - HelpFaqActivity.java
public class HelpFaqActivity extends AppCompatActivity { AnimatedExpandableListView expandableListView; ExpandableListAdapter expandableListAdapter; List<String> expandableListTitle; HashMap<String, List<String>> expandableListDetail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_help_faq); setTitle(R.string.help_faq); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); expandableListView = (AnimatedExpandableListView) findViewById(R.id.lvExp); expandableListDetail = ExpandableListDataPump.getData(); expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail); expandableListView.setAdapter(expandableListAdapter); expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if (expandableListView.isGroupExpanded(groupPosition)) { expandableListView.collapseGroupWithAnimation(groupPosition); Log.d("MainActivity", "Collapsing"); } else { expandableListView.expandGroupWithAnimation(groupPosition); Log.d("MainActivity", "Expanding"); } return true; } }); expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { } }); expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { } }); expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { return false; } }); } }
Please let me know if there are any ways to implement this, or better ways to do the same result. Thanks.
No comments:
Post a Comment