ExpandableListView没有扩展

时间:2016-03-09 08:28:21

标签: android expandablelistview

我在ExpandableList中创建Fragment视图,组项目显示在列表中,但是当我触摸组项目时,它没有展开。

以下是我的片段

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>
下面的

是childView xml

<?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>

3 个答案:

答案 0 :(得分:3)

嗯,很容易。

imagebutton中有groupview拦截触摸事件。添加以下参数:

    android:focusable="false"
    android:focusableInTouchMode="false"

childview做同样的事情。那里你的情况完全相同。

P.S。为什么你还要使用imagebuttons?您可以使用imageviews代替

答案 1 :(得分:0)

你需要这样做。

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;
        }
  });

希望这会对你有所帮助。

答案 2 :(得分:0)

尝试在适配器中传递ExpandableListView:

 [self startmonitoring:dict];

然后你的适配器应该是这样的:

- (void) startmonitoring:(CLLocation *)currentLocation {

   double latitude = currentLocation.coordinate.latitude ;
double Longitude = currentLocation.coordinate.longitude;
NSString *radius = @"100";
NSString *regionID = @"GeoFenceTrack";
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:CLLocationCoordinate2DMake(latitude ,Longitude) radius:[radius doubleValue] identifier:regionID];
location = [[CLLocationManager alloc]init];
location.delegate = self ;
location.desiredAccuracy = kCLLocationAccuracyBest;
[location startMonitoringForRegion:region];
 }

在您的论坛视图中添加以下内容:

SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);