可展开的列表视图组不可点击?

时间:2017-07-09 19:00:14

标签: java android listview expandablelistview

您好我的Expandalbe列表视图有自定义适配器,但我不能使其中的项目可点击? 以下是一些片段:
createAdapter()

中调用onCreate()
    private void createAdapter(){
    expListView = (ExpandableListView) findViewById(R.id.lv_exp);
    final myAdapter listAdapter = new myAdapter(realm, this);
    expListView.setClickable(true);
    expListView.setOnGroupClickListener((ExpandableListView expandableListView, View view, int i, long l) -> {
        final boolean groupExpanded = expandableListView.isGroupExpanded(i);
        if(groupExpanded)   {   expandableListView.collapseGroup(i);    }
        else    {   expandableListView.expandGroup(i);  }
        return false;
    });        
    expListView.setAdapter(listAdapter);
}

此外,我尝试在customAdapter的onCreateGroupView()方法中添加一些代码:(查看此处的评论)

    @Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {        //Group
    final Task myTask = getGroup(i);
    final String parentCode = myTask.getCode();
    final String text = myTask.getTaskText();
    final ExpandableListView myExpandableListView = (ExpandableListView) viewGroup;
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.parent_item, viewGroup, false);
    }
       //SEE HERE
    if (!view.isClickable()) {  view.setClickable(true);} //SEE HERE!<<
    final TextView myTextview = (TextView) view.findViewById(R.id.parent_text_view);
    myTextview.setText(text);
    final ImageButton myAddSubtaskBtn = (ImageButton) view.findViewById(R.id.button_add_subtask);
    myAddSubtaskBtn.setOnClickListener((viewL) -> ((MainActivity) context).createSubTask(parentCode));
    if (!view.isLongClickable()){    view.setLongClickable(true);     }
    view.setOnLongClickListener((viewL2) -> {
                final CharSequence[] optionsDialog = {"Edit", "Delete"};
                ((MainActivity) context).myDialog.setSingleChoiceItems(optionsDialog, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if(i==0){
                            final Intent myIntent = new Intent (context, InputActivity.class);
                            myIntent.putExtra("request", 6);
                            myIntent.putExtra("oldTask", myTask.getTaskText());
                            myIntent.putExtra("taskCode", myTask.getCode());
                            ((MainActivity) context).startActivityForResult(myIntent, 6);
                            dialogInterface.dismiss();
                        }
                        if(i==1){
                            realm.executeTransaction((realm) -> myTask.deleteTask());
                            realm.refresh();
                            notifyDataSetChanged();
                            dialogInterface.dismiss();
                        }
                    }
                });
                ((MainActivity) context).myDialog.show();
                return false;
            }
    );
    return view;
}

但是,当我在模拟器中运行它时,这些项目是不可点击的。 (只能长按)

1 个答案:

答案 0 :(得分:0)

也许它正在发生,因为您没有像onClick()那样覆盖myAddSubtaskButton view方法