无法在Fragment中设置ExpandableListView

时间:2014-12-12 08:20:01

标签: android android-fragments android-listview

我创建了ExpandableListView并在Activity中运行正常。 然后我改为Fragment相同的适配器,但无法运行它显示黑屏。

请告诉我我的代码有什么问题。

我尝试了3个小时却无法解决。 搜索但无法看到很多关于expandlists视图的帖子,其中大部分内容都显示在Activity

==============

现在我发现了我的错误,我得去打电话给prapareData();

谢谢

片段

public class PM_Fragment extends Fragment {


private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
private ExpandableListAdapter listAdapter;
private View root;


@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    root =  inflater.inflate( R.layout.expandable_catalog, container, false);
    ExpandableListView expandable = (ExpandableListView) root.findViewById(R.id.catalogExpandableListView); 
    listAdapter = new ExpandableListAdapter( getActivity() , listDataHeader, listDataChild);
    expandable.setAdapter(listAdapter );   //<-- if comment this line works fine but no data in view.

    return root;
}

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


private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Top 250");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    top250.add("The Godfather");
    top250.add("The Godfather: Part II");
    top250.add("Pulp Fiction");
    top250.add("The Good, the Bad and the Ugly");
    top250.add("The Dark Knight");
    top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    nowShowing.add("Despicable Me 2");
    nowShowing.add("Turbo");
    nowShowing.add("Grown Ups 2");
    nowShowing.add("Red 2");
    nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    comingSoon.add("The Smurfs 2");
    comingSoon.add("The Spectacular Now");
    comingSoon.add("The Canyons");
    comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}
}

适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.test_listitem, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.test_list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

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

expandable_catalog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="MY  EXPANDABLE VIEW"
    android:id="@+id/textView3" />

<ExpandableListView
    android:id="@+id/catalogExpandableListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
     />
</LinearLayout>

test_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

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

<fragment

    android:id="@+id/r_fragment"
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:name="com.test.PM_Fragment" />

</LinearLayout>

主要活动文件

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test_fragment);
}

4 个答案:

答案 0 :(得分:1)

测试一下:

使用

root.findViewById

而不是

getActivity().findViewById

答案 1 :(得分:1)

 ExpandableListView expandable = (ExpandableListView) getActivity().findViewById(R.id.catalogExpandableListView);

这就是你获得黑屏的原因。将其更改为:

 ExpandableListView expandable = (ExpandableListView) root.findViewById(R.id.catalogExpandableListView);

答案 2 :(得分:1)

public class ExpandableListFragment extends Fragment {

View v;
 ExpandableListAdapter mAdapter;
List<String> _listDataHeader;
HashMap<String, List<String>> _listDataChild;
private Parent parent;
private Child child;
ExpandableListView lv;
Context con;




public ExpandableListFragment() {
    // Required empty public constructor

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    v= inflater.inflate(R.layout.expandable_fragements,
            container, false);


    return v;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onActivityCreated(savedInstanceState);
    parent=new Parent();
    child=new Child();
     ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.expandableListView1);

     //here setting all the values to Parent and child classes
     setDataValues();
     prepareListData();//here get the values and set this values to adoptor and set it visible
     con=getActivity();

     mAdapter=new ExpandabelListAdoptor(con,_listDataHeader, _listDataChild) ; //here i didnt set list values to this adoptor



       // mAdapter = new ExpandableListAdapter(this, _listDataHeader, _listDataChild);

        // setting list adapter
        lv.setAdapter(mAdapter);



}





public void prepareListData()
{
    // testing purpose
    _listDataHeader = new ArrayList<String>();
    _listDataChild = new HashMap<String, List<String>>();


    // declare the references
    //add the parent values to List
    _listDataHeader.add(parent.getCardName());
    _listDataHeader.add(String.valueOf(parent.getMinimum_salary()));
    _listDataHeader.add(String.valueOf(parent.getInterest_rate()));


    //set Child views to parent
    List<String> cardDetails=new ArrayList<String>();
    cardDetails.add("");

    List<String> mininum_sal_details=new ArrayList<String>();
    mininum_sal_details.add(child.GetMinimumSalDetails());

    List<String> interest_details=new ArrayList<String>();
    interest_details.add(child.get_interest_rate_details());

    //set to adoptor

    _listDataChild.put(_listDataHeader.get(0),  cardDetails);
    _listDataChild.put(_listDataHeader.get(1),mininum_sal_details);

    //

     for(int i = 0; i < _listDataHeader.size(); i++) //cars name of arraylist
        {
           String value=_listDataHeader.get(i);  
           Toast toast = Toast.makeText(getActivity(),value, Toast.LENGTH_LONG);
           toast.setGravity(Gravity.CENTER, 0, 0);
           toast.show();

        }



}

public void setDataValues()
{
    //set Parent values
    parent.setCardName("Platinum credit Card");
    parent.setMinimum_salary(15000.00);
    parent.setInterest_Rate(1.2);

    //set Child values
    child.set_card_details("You require minimum salary of 1500 per month");
    child.set_interest_rate_details("interest rate is 2.0%");


}





 }  

 class ExpandabelListAdoptor extends BaseExpandableListAdapter
{

private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;





ExpandabelListAdoptor(Context con,List<String> listDataHeader ,HashMap<String, List<String>>  listDataChild )
{
    this._context=con;

    this._listDataChild=listDataChild;
    this._listDataHeader=listDataHeader;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
     return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
     return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

   final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;

}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
     return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
     return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

}

答案 3 :(得分:0)

我遇到了同样的问题,但是我从片段中的onCreateView返回空对象。因此片段无法识别ExpandableListAdapter