|
| 1 | +package io.neurolab.activities; |
| 2 | + |
| 3 | +import android.support.v7.app.AppCompatActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.text.Html; |
| 6 | +import android.text.method.LinkMovementMethod; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.BaseExpandableListAdapter; |
| 11 | +import android.widget.ExpandableListAdapter; |
| 12 | +import android.widget.ExpandableListView; |
| 13 | +import android.widget.TextView; |
| 14 | + |
| 15 | +import io.neurolab.R; |
| 16 | + |
| 17 | +public class FAQActivity extends AppCompatActivity { |
| 18 | + |
| 19 | + private String[] questions; |
| 20 | + private String[][] answers; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + setContentView(R.layout.activity_faq); |
| 26 | + |
| 27 | + questions = getResources().getStringArray(R.array.faq_questions); |
| 28 | + |
| 29 | + String[] ans = getResources().getStringArray(R.array.faq_answers); |
| 30 | + answers = new String[ans.length][]; |
| 31 | + for (int i = 0; i < ans.length; i++) { |
| 32 | + answers[i] = new String[]{ans[i]}; |
| 33 | + } |
| 34 | + |
| 35 | + ExpandableListView listView; |
| 36 | + |
| 37 | + listView = findViewById(R.id.expListView); |
| 38 | + listView.setAdapter(new ExpandableListAdapter(questions, answers)); |
| 39 | + listView.setGroupIndicator(null); |
| 40 | + } |
| 41 | + |
| 42 | + public class ExpandableListAdapter extends BaseExpandableListAdapter { |
| 43 | + |
| 44 | + private final LayoutInflater inf; |
| 45 | + private String[] questions; |
| 46 | + private String[][] answers; |
| 47 | + |
| 48 | + public ExpandableListAdapter(String[] questions, String[][] answers) { |
| 49 | + this.questions = questions; |
| 50 | + this.answers = answers; |
| 51 | + inf = LayoutInflater.from(getApplication()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int getGroupCount() { |
| 56 | + return questions.length; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int getChildrenCount(int questionPosition) { |
| 61 | + return answers[questionPosition].length; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object getGroup(int questionPosition) { |
| 66 | + return questions[questionPosition]; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Object getChild(int questionPosition, int answerPosition) { |
| 71 | + return answers[questionPosition][answerPosition]; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public long getGroupId(int questionPosition) { |
| 76 | + return questionPosition; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public long getChildId(int questionPosition, int answerPosition) { |
| 81 | + return answerPosition; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean hasStableIds() { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public View getChildView(int questionPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { |
| 91 | + |
| 92 | + ViewHolder holder; |
| 93 | + View v = convertView; |
| 94 | + if (v == null) { |
| 95 | + v = inf.inflate(R.layout.list_item, parent, false); |
| 96 | + holder = new ViewHolder(); |
| 97 | + |
| 98 | + holder.text = (TextView) v.findViewById(R.id.lblListItem); |
| 99 | + v.setTag(holder); |
| 100 | + } else { |
| 101 | + holder = (ViewHolder) v.getTag(); |
| 102 | + } |
| 103 | + |
| 104 | + holder.text.setClickable(true); |
| 105 | + holder.text.setMovementMethod(LinkMovementMethod.getInstance()); |
| 106 | + holder.text.setText(Html.fromHtml(getChild(questionPosition, childPosition).toString(), Html.FROM_HTML_MODE_COMPACT)); |
| 107 | + |
| 108 | + return v; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public View getGroupView(int questionPosition, boolean isExpanded, View convertView, ViewGroup parent) { |
| 113 | + ViewHolder holder; |
| 114 | + View v = convertView; |
| 115 | + if (v == null) { |
| 116 | + v = inf.inflate(R.layout.list_group, parent, false); |
| 117 | + |
| 118 | + holder = new ViewHolder(); |
| 119 | + holder.text = (TextView) v.findViewById(R.id.lblListHeader); |
| 120 | + v.setTag(holder); |
| 121 | + } else { |
| 122 | + holder = (ViewHolder) v.getTag(); |
| 123 | + } |
| 124 | + |
| 125 | + holder.text.setText(getGroup(questionPosition).toString()); |
| 126 | + |
| 127 | + return v; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean isChildSelectable(int questionPosition, int answerPosition) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + private class ViewHolder { |
| 136 | + private TextView text; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments