2017-08-08 81 views
0

我是新開發的應用程序。我目前正在一個tabview和一個ExpandableListView在其中一個選項卡上工作,因此我試圖在片段中使用expandablelistview。ExpandableListView在片段中空白

我的expandablelistview在直接從活動運行時看起來完美無瑕,但是當我試圖從一個片段運行它時,它顯得空白。

HomeFragment.java

public class HomeFragment extends Fragment { 

ExpandableListView expandableListView; 
View homeFragmentView; 


public HomeFragment() { 
    // Required empty public constructor 
} 

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

    homeFragmentView = inflater.inflate(R.layout.fragment_home, container, false); 
    expandableListView = (ExpandableListView) homeFragmentView.findViewById(R.id.exp_listview); 


    return homeFragmentView; 

} 

@Override 
public void onViewCreated(View v, Bundle savedInstanceState){ 

    List<String> Headings = new ArrayList<String>(); 
    List<String> L1 = new ArrayList<String>(); 
    List<String> L2 = new ArrayList<String>(); 
    List<String> L3 = new ArrayList<String>(); 
    HashMap<String, List<String>> ChildList = new HashMap<String, List<String>>(); 
    String heading_items[] = getResources().getStringArray(R.array.header_titles); 
    String h1[] = getResources().getStringArray(R.array.h1_items); 
    String h2[] = getResources().getStringArray(R.array.h2_items); 
    String h3[] = getResources().getStringArray(R.array.h3_items); 

    for(String title : heading_items){ 
     Headings.add(title); 
    } 
    for (String title : h1){ 
     L1.add(title); 
    } 
    for (String title : h2){ 
     L2.add(title); 
    } 
    for (String title : h3){ 
     L3.add(title); 
    } 
    ChildList.put(Headings.get(0), L1); 
    ChildList.put(Headings.get(1), L2); 
    ChildList.put(Headings.get(2), L3); 

    ExpListViewAdapter expListViewAdapter = new ExpListViewAdapter(getActivity(), Headings, ChildList); 
    expandableListView.setAdapter(expListViewAdapter); 


} 
} 

適配器類:

public class ExpListViewAdapter extends BaseExpandableListAdapter { 

private List<String> header_titles; 
private HashMap<String, List<String>> child_titles; 
private Context ctx; 

ExpListViewAdapter(Context ctx, List<String> header_titles, HashMap<String, List<String>> child_titles){ 
    this.ctx = ctx; 
    this.child_titles = child_titles; 
    this.header_titles = header_titles; 
} 



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

@Override 
public int getChildrenCount(int groupPosition) { 
    return child_titles.get(header_titles.get(groupPosition)).size(); 
} 

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

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return child_titles.get(header_titles.get(groupPosition)).get(childPosition); 
} 

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

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    String title = (String)this.getGroup(groupPosition); 
    if (convertView == null){ 
     LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.parent_layout, null); 
    } 
    TextView textView = (TextView) convertView.findViewById(R.id.heading_item); 
    textView.setTypeface(null, Typeface.BOLD); 
    textView.setText(title); 

    return convertView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    String title = (String)this.getChild(groupPosition,childPosition); 

    if (convertView == null){ 
     LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.child_layout, null); 
    } 

    TextView textView = (TextView) convertView.findViewById(R.id.child_item); 
    textView.setText(title); 

    return convertView; 
} 

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

頭(父)佈局:

<?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="50dp" 
android:background="#151515"> 

<TextView 
    android:id="@+id/heading_item" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Hello World" 
    android:textColor="#ffffff" 
    android:textSize="25dp" 
    android:gravity="center_vertical" 
    android:layout_marginLeft="10dp" 


    /> 

</LinearLayout> 

子佈局的:

<?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="50dp" 
    android:background="#252525"> 

    <TextView 
     android:id="@+id/child_item" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Hello World" 
     android:textColor="#f9f9f9" 
     android:gravity="center_vertical" 
     android:textSize="20dp" 
     android:layout_marginLeft="10dp" 
     /> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 



    Toolbar toolbar; 
    TabLayout tabLayout; 
    ViewPager viewPager; 
    ViewPagerAdapter viewPagerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     overridePendingTransition(R.anim.slide_in, R.anim.slide_out); 

     toolbar = (Toolbar) findViewById(R.id.toolBar); 
     setSupportActionBar(toolbar); 

     tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 

     viewPagerAdapter.addFragments(new HomeFragment(), "Home"); 
     viewPagerAdapter.addFragments(new Group_Invites(), "Group invites"); 
     viewPagerAdapter.addFragments(new New_Group(), "New group"); 
     viewPager.setAdapter(viewPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 
    } 
} 

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter { 

    ArrayList<Fragment> fragments = new ArrayList<>(); 
    ArrayList<String> tabTitles = new ArrayList<>(); 

    public void addFragments(Fragment fragments, String titles){ 

     this.fragments.add(fragments); 
     this.tabTitles.add(titles); 

    } 

    public ViewPagerAdapter(FragmentManager fm){ 
     super (fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 

     return fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position){ 
     return tabTitles.get(position); 
    } 
} 

回答

0

我想你的代碼,它工作正常。還要分享您打開片段課程的活動代碼。

+0

和以前一樣。仍然顯示擴展列表視圖應該放置的空白空間。 –

+0

我知道它的工作。 無論出於何種原因,我的MainActivity被設置爲RelativeLayout。將其更改爲LinearLayout使其工作。 非常感謝您的努力,並遺憾浪費您的時間! –