【问题标题】:Cannot set ExpandableListView in Fragment无法在 Fragment 中设置 ExpandableListView
【发布时间】:2014-12-12 08:20:00
【问题描述】:

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

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

我尝试了 3 个小时但无法解决。 搜索它,但在片段中看不到很多关于展开列表视图的帖子,其中大部分显示在 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);
}

【问题讨论】:

  • 您是否在活动中添加了片段?
  • (ExpandableListView) root.findViewById(R.id.catalogExpandableListView);替换(ExpandableListView) getActivity().findViewById(R.id.catalogExpandableListView);
  • @DhavalGondaliya 如果我评论 expandable.setAdapter(listAdapter);它可以工作,但列表中没有数据。
  • 如果有帮助,请接受我的回答

标签: android android-fragments android-listview


【解决方案1】:

测试一下:

使用

root.findViewById

而不是

getActivity().findViewById

【讨论】:

  • 仍然不行,如果我评论 expandable.setAdapter(listAdapter),它可以工作但没有数据。
  • 您应该在设置适配器之前调用 prepareListData()。你做到了吗?
  • @HamidrezaSamadi 是的,我忘了打电话 prepareData() 现在我工作了。谢谢。
【解决方案2】:
 ExpandableListView expandable = (ExpandableListView) getActivity().findViewById(R.id.catalogExpandableListView);

这就是您出现黑屏的原因。将其更改为:

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

【讨论】:

    【解决方案3】:
    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;
    }
    

    }

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多