Jarday 2017-02-17 13:53 采纳率: 0%
浏览 1339

Android在Fragment中ExpandableView不能展开

求助各位大神,我在Fragment中设置了一个ExpandableView,但是不能展开,Log日志发现getChildView方法没有执行
查询资料说是因为Button,但我的布局里没有Button。
而且发现这个代码在Activity中就正常,Fragment中就不正常
这是单独拿出来做最简单的测试的东西,Activity中只有一个fragment
Activity的布局

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dachuang.androidtest.MainActivity">

   <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       class="com.dachuang.androidtest.MainIndexFragment"/>
</RelativeLayout>

Fragment的两个布局,group

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:id="@+id/canteen_food_list_group_window_name"/>


</LinearLayout>

child的布局

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/canteen_food_list_child_food_cover"
        />

    <LinearLayout
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/canteen_food_list_child_food_name"/>

        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/ratingBarStyleSmall"
            android:id="@+id/canteen_food_list_child_food_rating"/>

    </LinearLayout>


</LinearLayout>

这是Fragment的代码


public class MainIndexFragment extends Fragment {

    View view;
    ExpandableListView canteenList = null;
    CanteenFoodListAdapter canteenFoodListAdapter = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_index, container, false);
        canteenList = (ExpandableListView) view.findViewById(R.id.canteen_food_list);
        canteenFoodListAdapter = new CanteenFoodListAdapter(getActivity());
        canteenList.setAdapter(canteenFoodListAdapter);
        return view;
    }
}

Fragment的Adapter

 package com.dachuang.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.dachuang.campusfood.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by Unicorn on 2017/2/14.
 */

public class CanteenFoodListAdapter extends BaseExpandableListAdapter {

    private List<String> groupList = new ArrayList<>();
    private List<Map<String,Object>> childMapList = new ArrayList<>();
    private int windowFoodSize[] = {1,2};     //存放每个窗口有多少种饭菜
    private LayoutInflater mInflater;
    private Context paramContext;

    public CanteenFoodListAdapter(Context paramContext, List<String> group,
                                  List<Map<String, Object>> childMap,int[] windowSize){
        this.paramContext = paramContext;
        this.mInflater = LayoutInflater.from(paramContext);
        //
    }

    //测试用构造方法
    public CanteenFoodListAdapter(Context paramContext){
        this.paramContext = paramContext;
        this.mInflater = LayoutInflater.from(paramContext);
    }
    @Override
    public int getGroupCount() {
        return 2;
       // return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return windowFoodSize[groupPosition];
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @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 groupView, ViewGroup parent) {
        Log.e("groupView","groupView");
        GroupHolder groupHolder = null;
        if(groupView == null){
            groupView = mInflater.inflate(R.layout.canteen_food_list_group,null);
            groupHolder = new GroupHolder();
            groupHolder.windowName = (TextView) groupView.findViewById(R.id.canteen_food_list_group_window_name);
            groupView.setTag(groupHolder);
            //给view对象一个标签,告诉计算机我们已经在缓冲区里放了一个view,下回直接来拿就行了
        }
        else{
            groupHolder = (GroupHolder) groupView.getTag();
        }
        groupHolder.windowName.setText("民族风味");
        return groupView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View childView, ViewGroup parent) {
        Log.e("childView","childView");
        ChildHolder childHolder = null;
        if(childView == null){
            childView = mInflater.inflate(R.layout.canteen_food_list_child,null);
            childHolder = new ChildHolder();
            childHolder.foodCover = (ImageView) childView.findViewById(R.id.canteen_food_list_child_food_cover);
            childHolder.foodName = (TextView) childView.findViewById(R.id.canteen_food_list_child_food_name);
            childHolder.foodRating = (RatingBar) childView.findViewById(R.id.canteen_food_list_child_food_rating);
            childView.setTag(childHolder);
        }
        else{
            childHolder = (ChildHolder) childView.getTag();
        }
        childHolder.foodCover.setImageResource(R.drawable.girl_icon_default);
        childHolder.foodName.setText("清蒸");
        childHolder.foodRating.setNumStars(3);
        return childView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder {
        public TextView windowName;
    }

    class ChildHolder {
        public ImageView foodCover;
        public TextView foodName;
        public RatingBar foodRating;
    }

}

请问有谁知道吗?Activity中就可以正常展开,Fragment中就不可以。查了半天也没看出来什么结果。有知道的大神请指点迷津,谢谢大家

  • 写回答

1条回答 默认 最新

  • devmiao 2017-02-17 15:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形