Android多级可扩展列表视图集第三级子项已单击
我有来自项目的代码,我想实现可扩展的第三级子对象OnItemClick,我尝试使用来自2级可扩展列表视图子对象单击侦听器的概念,但它不起作用。 所以,如何让用户知道已选择了某些父级/组级、第二级和第三级项目 这是我的密码: MainPageActivity.javaAndroid多级可扩展列表视图集第三级子项已单击,android,android-layout,expandablelistview,android-drawable,animationdrawable,Android,Android Layout,Expandablelistview,Android Drawable,Animationdrawable,我有来自项目的代码,我想实现可扩展的第三级子对象OnItemClick,我尝试使用来自2级可扩展列表视图子对象单击侦听器的概念,但它不起作用。 所以,如何让用户知道已选择了某些父级/组级、第二级和第三级项目 这是我的密码: MainPageActivity.java public class MainPageActivity extends AppCompatActivity { private ExpandableListView expandableListView; String[
public class MainPageActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
String[] parent = new String[]{"group 1", "group 2"};
String[] q1 = new String[]{"Child Level 1", "Child level 2"};
String[] q2 = new String[]{"Child Level 1B", "Child Level 2B"};
String[] q3 = new String[]{"Child Level 1C"};
String[] des1 = new String[]{"A","B","C"};
String[] des2 = new String[]{"D","E","F"};
String[] des3 = new String[]{"G"};
String[] des4 = new String[]{"H","J"};
String[] des5 = new String[]{"U."," R"," V"};
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
* Second level array list
*/
List<String[]> secondLevel = new ArrayList<>();
/**
* Inner level data
*/
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
setUpAdapter();
}
private void setUpAdapter() {
secondLevel.add(q1);
secondLevel.add(q2);
secondLevel.add(q3);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des2);
thirdLevelq2.put(q2[0], des3);
thirdLevelq2.put(q2[1], des4);
thirdLevelq3.put(q3[0], des5);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
//passing three level of information to constructor
ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
expandableListView.setAdapter(threeLevelListAdapterAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
// ExpandableListView on child click listener
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String[] value= secondLevel.get(groupPosition);
Toast.makeText(
getApplicationContext(),
secondLevel.get(groupPosition)
+ " : ", Toast.LENGTH_SHORT)
.show();
return false;
}
}); }}
SecondLevelAdapter.java
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
List<String[]> data;
String[] headers;
ImageView ivGroupIndicator;
public SecondLevelAdapter(Context context, String[] headers, List<String[]> data) {
this.context = context;
this.data = data;
this.headers = headers;
}
@Override
public Object getGroup(int groupPosition) {
return headers[groupPosition];
}
@Override
public int getGroupCount() {
return headers.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.rowSecondText);
String groupText = getGroup(groupPosition).toString();
text.setText(groupText);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String[] childData;
childData = data.get(groupPosition);
return childData[childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView textView = (TextView) convertView.findViewById(R.id.rowThirdText);
String[] childArray = data.get(groupPosition);
String text = childArray[childPosition];
textView.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
String[] children = data.get(groupPosition);
return children.length;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
公共类SecondLevelAdapter扩展了BaseExpandableListAdapter{
私人语境;
列出数据;
字符串[]头;
ImageView-ivGroupIndicator;
公共SecondLevelAdapter(上下文上下文、字符串[]头、列表数据){
this.context=上下文;
这个数据=数据;
this.headers=标题;
}
@凌驾
公共对象getGroup(int-groupPosition){
返回标题[groupPosition];
}
@凌驾
public int getGroupCount(){
返回页眉长度;
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.row_second,null);
TextView text=(TextView)convertView.findViewById(R.id.rowSecondText);
字符串groupText=getGroup(groupPosition).toString();
setText(groupText);
返回视图;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
字符串[]子数据;
childData=data.get(groupPosition);
返回childData[childPosition];
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.row_third,null);
TextView TextView=(TextView)convertView.findViewById(R.id.rowThirdText);
String[]childArray=data.get(groupPosition);
String text=childArray[childPosition];
textView.setText(text);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
String[]children=data.get(groupPosition);
返回子项长度;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
java
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
公共类ThreeLevelListAdapter扩展了BaseExpandableListAdapter{
字符串[]父标题;
二级名单;
私人语境;
列出数据;
/**
*建造师
*@param上下文
*@param parentHeader
*@param二级
*@param数据
*/
public ThreeLevelListAdapter(上下文上下文,字符串[]父标题,列表第二级,列表数据){
this.context=上下文;
this.parentHeaders=parentHeader;
this.secondLevel=secondLevel;
这个数据=数据;
}
@凌驾
public int getGroupCount(){
返回parentHeaders.length;
}
@凌驾
公共整数getChildrenCount(整数组位置){
//不知道为什么这个代码会起作用
返回1;
}
@凌驾
公共对象getGroup(int-groupPosition){
返回组位置;
}
@凌驾
公共对象getChild(int组,int子对象){
返回儿童;
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.row_first,null);
TextView text=(TextView)convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
final SecondLevelExpandableListView secondLevelELV=新的SecondLevelExpandableListView(上下文);
String[]headers=secondLevel.get(groupPosition);
List childData=new ArrayList();
HashMap secondLevelData=data.get(groupPosition);
for(字符串键:secondLevelData.keySet()){
add(secondLevelData.get(key));
}
setAdapter(新的SecondLevelAdapter(上下文、头、子数据));
secondLevelELV.setGroupIndicator(空);
secondLevelELV.setOnGroupExpandListener(新的ExpandableListView.OnGroupExpandListener(){
int-previousGroup=-1;
@凌驾
public void onGroupExpand(int groupPosition){
if(groupPosition!=previousGroup)
二级折叠组(前一组);
previousGroup=groupPosition;
}
});
返回二级ELV;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}您可以通过在ThreeLevelListAdapter中创建界面,在第三级单击项目,并在活动中实现。请尝试以下内容: MainPageActivity.java:
public class MainPageActivity extends AppCompatActivity implements ThreeLevelListAdapter.ThreeLevelListViewListener{
private ExpandableListView expandableListView;
String[] parent = new String[]{"group 1", "group 2"};
String[] q1 = new String[]{"Child Level 1", "Child level 2"};
String[] q2 = new String[]{"Child Level 1B", "Child Level 2B"};
String[] q3 = new String[]{"Child Level 1C"};
String[] des1 = new String[]{"A","B","C"};
String[] des2 = new String[]{"D","E","F"};
String[] des3 = new String[]{"G"};
String[] des4 = new String[]{"H","J"};
String[] des5 = new String[]{"U."," R"," V"};
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
* Second level array list
*/
List<String[]> secondLevel = new ArrayList<>();
/**
* Inner level data
*/
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
setUpAdapter();
}
private void setUpAdapter() {
secondLevel.add(q1);
secondLevel.add(q2);
secondLevel.add(q3);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des2);
thirdLevelq2.put(q2[0], des3);
thirdLevelq2.put(q2[1], des4);
thirdLevelq3.put(q3[0], des5);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
expandableListView = (ExpandableListView) findViewById(R.id.expandable_listview);
//passing three level of information to constructor
ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data, this);
expandableListView.setAdapter(threeLevelListAdapterAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
@Override
public void onFinalChildClick(int plpos, int slpos, int tlpos) {
Toast.makeText(this, plpos + ", " + slpos + ", " + tlpos, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinalItemClick(String plItem, String slItem, String tlItem) {
Toast.makeText(this, plItem + ", " + slItem + ", " + tlItem, Toast.LENGTH_SHORT).show();
}
}
公共类MainPageActivity扩展AppCompative实现ThreeLevelListAdapter.ThreeLevelListViewListener{
私有可扩展列表视图可扩展列表视图;
字符串[]父项=新字符串[]{“组1”、“组2”};
字符串[]q1=新字符串[]{“子级别1”,“子级别2”};
字符串[]q2=新字符串[]{“子级别1B”,“子级别2B”};
字符串[]q3=新字符串[]{“子级别1C”};
字符串[]des1=新字符串[]{“A”、“B”、“C”};
字符串[]des2=新字符串[]{“D”、“E”、“F”};
字符串[]des3=新字符串
public class MainPageActivity extends AppCompatActivity implements ThreeLevelListAdapter.ThreeLevelListViewListener{
private ExpandableListView expandableListView;
String[] parent = new String[]{"group 1", "group 2"};
String[] q1 = new String[]{"Child Level 1", "Child level 2"};
String[] q2 = new String[]{"Child Level 1B", "Child Level 2B"};
String[] q3 = new String[]{"Child Level 1C"};
String[] des1 = new String[]{"A","B","C"};
String[] des2 = new String[]{"D","E","F"};
String[] des3 = new String[]{"G"};
String[] des4 = new String[]{"H","J"};
String[] des5 = new String[]{"U."," R"," V"};
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
* Second level array list
*/
List<String[]> secondLevel = new ArrayList<>();
/**
* Inner level data
*/
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
setUpAdapter();
}
private void setUpAdapter() {
secondLevel.add(q1);
secondLevel.add(q2);
secondLevel.add(q3);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des2);
thirdLevelq2.put(q2[0], des3);
thirdLevelq2.put(q2[1], des4);
thirdLevelq3.put(q3[0], des5);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
expandableListView = (ExpandableListView) findViewById(R.id.expandable_listview);
//passing three level of information to constructor
ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data, this);
expandableListView.setAdapter(threeLevelListAdapterAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
@Override
public void onFinalChildClick(int plpos, int slpos, int tlpos) {
Toast.makeText(this, plpos + ", " + slpos + ", " + tlpos, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinalItemClick(String plItem, String slItem, String tlItem) {
Toast.makeText(this, plItem + ", " + slItem + ", " + tlItem, Toast.LENGTH_SHORT).show();
}
}
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
ThreeLevelListViewListener mThreeLevelListViewListener;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
* @param listener
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel,
List<LinkedHashMap<String, String[]>> data,
ThreeLevelListViewListener listener) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
mThreeLevelListViewListener = listener;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return parentHeaders[groupPosition];
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
secondLevelELV.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
int ppos = (int)expandableListView.getTag();
mThreeLevelListViewListener.onFinalChildClick(ppos, i, i1);
String plItem = (String)getGroup(ppos);
SecondLevelAdapter adapter = (SecondLevelAdapter)expandableListView.getExpandableListAdapter();
String slItem = (String)adapter.getGroup(i);
String tlItem = (String)adapter.getChild(i, i1);
mThreeLevelListViewListener.onFinalItemClick(plItem, slItem, tlItem);
return true;
}
});
secondLevelELV.setTag(groupPosition);
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public interface ThreeLevelListViewListener{
void onFinalChildClick(int plpos, int slpos, int tlpos);
void onFinalItemClick(String plItem, String slItem, String tlItem);
}
}