我是app开发的新手。我目前正在使用tabview和其中一个标签中的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)
我尝试了你的代码,它工作正常。同时分享您打开片段类的活动代码。