Как перейти с одной страницы на следующую с помощью ExpandableListView

#android #navigation #expandablelistview #onclicklistener #expandablelistadapter

#Android #навигация #расширяемый список #onclicklistener

Вопрос:

Кто-нибудь знает, как перейти на другую страницу из ExpandableListView. Я новичок в Android, так что это немного сложно. Пока у меня есть ELV с заголовками и дочерними элементами. Однако я хочу, чтобы, когда кто-то нажимает на одного из дочерних элементов, он отправлял их на другую страницу. Я потерялся, поэтому любая помощь будет оценена.

Вот код, который я сделал до сих пор:

           public class MainMenu extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;


      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_menu);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.expandableListView1);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
       }

        /     *
      * Preparing the list data
      */
    private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Lesson 1");
    listDataHeader.add("Lesson 2");
    listDataHeader.add("Lesson 3");
    listDataHeader.add("Lesson 4");
    listDataHeader.add("Lesson 5");

    // Adding child data
    List<String> Lesson1 = new ArrayList<String>();
    Lesson1.add("The Alphabet");
    Lesson1.add("Common Expressions");
    Lesson1.add("Vocabulary: Things in a Room");
    Lesson1.add("Grammar: Demonstrative Pronouns");
    Lesson1.add("Questions and Answers");


    List<String> Lesson2 = new ArrayList<String>();
    Lesson2.add("Numbers 1-10");
    Lesson2.add("Common Expressions");
    Lesson2.add("Vocabulary: Classroom Tools");
    Lesson2.add("Grammar: Plurals");
    Lesson2.add("Grammar: Using Plurals");
    Lesson2.add("Questions and Answers");

    List<String> Lesson3 = new ArrayList<String>();
    Lesson3.add("Numbers 11-20");
    Lesson3.add("Common Expressions");
    Lesson3.add("Vocabulary: School");
    Lesson3.add("Grammar: Possessives");
    Lesson3.add("Questions and Answers");

    List<String> Lesson4 = new ArrayList<String>();
    Lesson4.add("The Tens (20-100)");
    Lesson4.add("Common Expressions");
    Lesson4.add("Vocabulary: Food");
    Lesson4.add("Grammar: Subject Pronouns, Using 'Want'");
    Lesson4.add("Questions and Answers");

    List<String> Lesson5 = new ArrayList<String>();
    Lesson5.add("Numbers 21-30");
    Lesson5.add("Common Expressions");
    Lesson5.add("Vocabulary: Common Verbs I");
    Lesson5.add("Grammar: Using 'Want'   Infinitive");
    Lesson5.add("Questions and Answers");

    listDataChild.put(listDataHeader.get(0), Lesson1); // Header, Child data
    listDataChild.put(listDataHeader.get(1), Lesson2);
    listDataChild.put(listDataHeader.get(2), Lesson3);
    listDataChild.put(listDataHeader.get(3), Lesson4);
    listDataChild.put(listDataHeader.get(4), Lesson5);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
    return true;
}
return super.onOptionsItemSelected(item);
     }
  

Я создал класс адаптера следующим образом:

        public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

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

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

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

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  

Комментарии:

1. я думаю, вы найдете этот урок полезным androidhive.info/2013/07/android-expandable-list-view-tutorial

2. также этот androidexample.com /…