ExpandableListView 没有展开
ExpandableListView is not expanding
我正在 Fragment
中创建 ExpandableList
视图,组项目显示在列表中,但是当我触摸组项目时,它没有展开。
下面是我的片段
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
下面是适配器
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.get(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) {
final String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
@Override
public int getGroupCount() {
return this.subjectName.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
下面是institute_fragment.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="@+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
下面是我的群视图
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/_10sdp"
android:layout_margin="@dimen/_5sdp"
android:background="@android:color/white"
android:id="@+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_10sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="@dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
下面是childViewxml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:background="@color/colorPrimary"
android:id="@+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="@dimen/_10sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="@dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
你需要做这个。
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
希望对您有所帮助。
嗯,很简单。
您在 groupview
中有 imagebutton
正在拦截触摸事件。添加这些参数:
android:focusable="false"
android:focusableInTouchMode="false"
childview
也一样。你那里的情况完全一样。
P.S。你为什么要使用 imagebuttons
呢?你可以使用 imageviews
而不是
尝试在适配器中传递您的 ExpandableListView:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
那么你的适配器应该是这样的:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
在您的群组视图中添加以下内容:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});
我正在 Fragment
中创建 ExpandableList
视图,组项目显示在列表中,但是当我触摸组项目时,它没有展开。
下面是我的片段
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
下面是适配器
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.get(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) {
final String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
@Override
public int getGroupCount() {
return this.subjectName.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
下面是institute_fragment.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="@+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
下面是我的群视图
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/_10sdp"
android:layout_margin="@dimen/_5sdp"
android:background="@android:color/white"
android:id="@+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginBottom="@dimen/_10sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="@dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
下面是childViewxml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_5sdp"
android:background="@color/colorPrimary"
android:id="@+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="@dimen/_10sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:textColor="@android:color/black" />
<ImageButton
android:id="@+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="@dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
你需要做这个。
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
希望对您有所帮助。
嗯,很简单。
您在 groupview
中有 imagebutton
正在拦截触摸事件。添加这些参数:
android:focusable="false"
android:focusableInTouchMode="false"
childview
也一样。你那里的情况完全一样。
P.S。你为什么要使用 imagebuttons
呢?你可以使用 imageviews
而不是
尝试在适配器中传递您的 ExpandableListView:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
那么你的适配器应该是这样的:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
在您的群组视图中添加以下内容:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});