0

遵循androidhive的教程創建可展開的列表視圖。在選項卡片段中實現可展開的列表視圖

http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html 按照教程創建水平製表符。

編輯了Tab Widget示例(第二個教程),將可擴展列表視圖放入選項卡中。這是通過在項目中創建可展開列表適配器java文件並將可擴展列表代碼複製到選項卡活動文件中來實現的。 (帶有所需的xml文件)選項卡活動然後看起來像這樣:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ExpandableListView; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class Tab1Activity extends Activity 
{ 
    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.expandable_listview); 

     // get the listview 
     expListView = (ExpandableListView) findViewById(R.id.lvExp); 

     // preparing list data 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 
    } 

    // Preparing the list data 
    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); 
    } 
} 

工作正常。試圖做從這裏項目相同: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ 在這裏,我有一些錯誤。我認爲setContentView不能與碎片一起使用,所以我擺脫了它並保留了原始的充氣器。 同樣,我想我需要使用別的東西,而不是findViewById,並更改ExpandableListAdapter()的參數。真的不知道這是什麼需要有所改變不過..

我的標籤片段:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ExpandableListView; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class GamesFragment extends Fragment 
{ 

    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader; 
    HashMap<String, List<String>> listDataChild; 

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

     View rootView = inflater.inflate(R.layout.fragment_games, container, false); 


     super.onCreate(savedInstanceState); 

     // get the listview 
     expListView = (ExpandableListView) findViewById(R.id.lvExp); 

     // preparing list data 
     prepareListData(); 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 

     return rootView; 
    } 

    // Preparing the list data 
    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); 
    } 
} 

我的標籤片段佈局:

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

    <ExpandableListView 
     android:id="@+id/lvExp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </ExpandableListView> 

</RelativeLayout> 

不要讓我知道如果我要對這個錯誤的方法。我所要做的就是創建帶有可擴展列表視圖的選項卡的滑動視圖。

回答

0

1)上查看呼叫findViewById()返回 findViewById in Fragment

2)從 '本' 改變的第一個參數在ExpandableListAdapter爲 'getActivity()'

我的新onCreateView看起來是這樣的:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.tab_expandable_list, container, false); 

    // get the listview 
    expListView = (ExpandableListView) view.findViewById(R.id.lvExp); 

    // preparing list data 
    prepareListData(); 

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); 

    // setting list adapter 
    expListView.setAdapter(listAdapter); 

    return view; 
} 
相關問題