【问题标题】:Android - possible to insert unique parent and unique child items to ExpandableList by using xml array?Android - 可以使用 xml 数组将唯一的父项和唯一的子项插入 ExpandableList?
【发布时间】:2017-03-02 05:13:26
【问题描述】:

所以我是 android 的初学者,在阅读了一些关于 ExpandableList 的在线教程后,我在我的应用中制作了一个常见问题解答列表。

但是,我想知道是否可以通过读取 xml 数组将唯一的父项和唯一的子项添加到 ExpandableList?比如在xml中有一个问题数组和答案数组,答案会按照问题列表的相同顺序出现,像这样:

<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>

目前,我必须像这样手动将答案字符串放入他们各自的问题中:

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;
    }
}

适配器 - 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;
    }
}

活动 - 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;
            }
        });
    }
}

如果有任何方法可以实现这一点,或者有更好的方法来实现相同的结果,请告诉我。谢谢。

【问题讨论】:

    标签: android arrays xml expandablelistview expandablelistadapter


    【解决方案1】:

    将上下文从主要活动发送到您的班级:

    expandableListDetail = ExpandableListDataPump.getData(getApplicationContext());
    

    并将您的 ExpandableListDataPump 更改为:

    public class ExpandableListDataPump extends AppCompatActivity {
    
        public static HashMap<String, List<String>> getData(Context context) {
            HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
    
            String[] questions = context.getResources().getStringArray(R.array.faq_question);
            String[] answers = context.getResources().getStringArray(R.array.faq_answer);
    
            //add error checking in case the length of questions and answers do not match
            for (int i = 0; i < questions.length; i++)  {
                List<String> answerList = new ArrayList<>();
                answerList.add(answers[i]);
                expandableListDetail.put(questions[i], answerList);
            }
            return expandableListDetail;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2022-01-15
      相关资源
      最近更新 更多