展开式列表视图子点击新活动

时间:2018-12-01 03:51:10

标签: android expandablelistview expandablelistadapter

我为扩展列表视图编写了代码。正常运作。但是我想,当我单击每个孩子时,它将是“打开一个新活动”。你能帮我么。我将非常感谢您的帮助。

  

CourseAdapter.java

public class CourseAdapter extends BaseExpandableListAdapter {
    private Context ctx;
    private HashMap<String, List<String>> Topics_Category;
    private List<String> Introduction_List;
    public CourseAdapter(Context ctx, HashMap<String, List<String>> Topics_Category, List<String> Introduction_List){
        this.ctx= ctx;
        this.Topics_Category = Topics_Category;
        this.Introduction_List = Introduction_List;
    }
    @Override
    public int getGroupCount() {
        return Introduction_List.size();
    }
    @Override
    public int getChildrenCount(int i) {
        return Topics_Category.get(Introduction_List.get(i)).size();
    }
    @Override
    public Object getGroup(int i) {
        return Introduction_List.get(i);
    }
    @Override
    public Object getChild(int parent, int child) {
        return Topics_Category.get(Introduction_List.get(parent)).get(child);
    }
    @Override
    public long getGroupId(int i) {
        return i;
    }
    @Override
    public long getChildId(int parent, int child) {
        return child;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentview) {
        String group_title = (String) getGroup(parent);
        if (convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.activity_parent_list_view_layout, parentview, false);
        }
        TextView parent_textview = convertView.findViewById(R.id.parent_txt);
        parent_textview.setTypeface(null, Typeface.BOLD);
        parent_textview.setText(group_title);
        return convertView;
    }
    @Override
    public View getChildView(int parent, int child, boolean lastChild, View convertview, ViewGroup parentview) {
        String child_title = (String) getChild(parent, child);
        if (convertview == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = inflator.inflate(R.layout.activity_child_list_view_layout, parentview, false);
        }
        TextView child_textview = (TextView) convertview.findViewById(R.id.child_txt);
        child_textview.setText(child_title);
        return convertview;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

我为扩展列表视图编写了代码。正常运作。但是我想,当我单击每个孩子时,它将是“打开一个新活动”。你能帮我么。我将非常感谢您的帮助。

  

MainListViewActivity.java

    public class MainListViewActivity extends AppCompatActivity {

    HashMap<String, List<String>> Introduction;
    List<String> Introduction_list;
    ExpandableListView Exp_list;
    CourseAdapter adapter;

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

        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);

        Introduction = DataProvider.getInfo();
        Introduction_list = new ArrayList<String>(Introduction.keySet());
        adapter = new CourseAdapter(this, Introduction, Introduction_list);
        Exp_list.setAdapter(adapter);

        Exp_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getBaseContext(), Introduction_list.get(groupPosition)+ " is Expanded", Toast.LENGTH_LONG).show();
            }
        });

        Exp_list.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getBaseContext(), Introduction_list.get(groupPosition)+ " is Collapsed", Toast.LENGTH_LONG).show();
            }
        });

        Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getBaseContext(), Introduction.get(Introduction_list.get(groupPosition)).get(childPosition)+ " from category "+
                        Introduction_list.get(groupPosition)+ " is selected ", Toast.LENGTH_LONG).show();

                return false;
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

您只需要在setOnChildClickListener方法中传递Intent。

Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Toast.makeText(getBaseContext(), Introduction.get(Introduction_list.get(groupPosition)).get(childPosition)+ " from category "+
                Introduction_list.get(groupPosition)+ " is selected ", Toast.LENGTH_LONG).show();

        Intent intent = new Intent(this, NextActivity.class);
        startActivity(intent);

        return false;
    }
});
相关问题