ExpandableListView 父母中的不同切换意图 - 前一个不起作用

Posted

技术标签:

【中文标题】ExpandableListView 父母中的不同切换意图 - 前一个不起作用【英文标题】:Different Switch Intents In ExpandableListView Parents - Previous One Doesn't Work 【发布时间】:2021-07-11 16:29:42 【问题描述】:

我正在尝试创建一个具有切换到新类的意图的 ExpandibleListView。当只使用一个开关时,没有问题,但是当我想让不同的父母切换不同的班级时,只有一个工作,另一个保持空(它的箭头向上和向下但没有任何反应)。我怎样才能让它们一起工作?

这是我的代码:

ExpandibleListAdapter.java

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.widget.TextView;


public class ExpandableListAdapter extends BaseExpandableListAdapter 
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHashMap;



    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) 
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;
    



    @Override
    public int getGroupCount() 
        return listDataHeader.size();
    

    @Override
    public int getChildrenCount(int i) 
        return listHashMap.get(listDataHeader.get(i)).size();
    

    @Override
    public Object getGroup(int i) 
        return listDataHeader.get(i);
    

    @Override
    public Object getChild(int i, int i1) 
        return listHashMap.get(listDataHeader.get(i)).get(i1); //  i = group item , i1= ChildItem

    

    @Override
    public long getGroupId(int i) 
        return i;
    

    @Override
    public long getChildId(int i, int i1) 
        return i1;
    

    @Override
    public boolean hasStableIds() 
        return false;
    

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup ViewGroup) 
        String headerTitle = (String)getGroup(i);
        if(view == null)
        
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_group,null);
        
        TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return view;

        

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup ViewGroup) 
        final String childText = (String)getChild(i,i1);
        if(view == null)
        
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item,null);
        
        TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return view;

    

    @Override
    public boolean isChildSelectable(int i, int i1) 
        return true;
    

MainActivity.java

public class MainActivity extends AppCompatActivity 
        


private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listView = (ExpandableListView)findViewById(R.id.lvExp);
    initData();
    listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
    listView.setAdapter(listAdapter);

    


private void initData() 
    listDataHeader = new ArrayList<>();
    listHash = new HashMap<>();

    listDataHeader.add("Line One");
    listDataHeader.add("Line Two");
    


    List<String> genel = new ArrayList<>();
**//The problem starts here**

    listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() 
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
            final String selected = (String) listAdapter.getGroup(groupPosition);
            Intent intent;

            switch (selected) 
                case "Line One":
                    intent = new Intent(MainActivity.this,LineOne.class);
                    startActivity(intent);
                    break;
            
            return false; //return true doesn't let the other parents to open.
        
    );


    List<String> terims = new ArrayList<>();
    listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() 
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
            final String terims = (String) listAdapter.getGroup(groupPosition);
            Intent niyet;//I used "intent" instead of "niyet" as same above but nothing has changed.

            switch (terims) 
                case "Line Two":
                    niyet = new Intent(MainActivity.this, LineTwo.class);
                    startActivity(niyet);
                    break;
            
            return false; 
        
    );
    


    listHash.put(listDataHeader.get(0),genel);
    listHash.put(listDataHeader.get(1),terims);
    
    



【问题讨论】:

【参考方案1】:

您不能设置 2 个 OnGroupClickListener。第二个取消第一个。您应该设置一个侦听器,然后在一个侦听器中检查两组条件。

【讨论】:

非常感谢,我将第二个 switch case 替换为第一个 onGroupClick 并且两者都有效。上帝保佑你

以上是关于ExpandableListView 父母中的不同切换意图 - 前一个不起作用的主要内容,如果未能解决你的问题,请参考以下文章

是否可以在 ExpandableListView 中为子项放置不同的布局?

当父母在Android中折叠时如何更改可扩展列表视图子项的视图?

监听器无法识别 ExpandableListView OnLongClick

将焦点保存在 ExpandableListView 中的展开组上

Expandablelistview 中的奇怪行为 - Android

使用 SQLite 中的数据填充 ExpandableListView