2013-06-05 17 views
7

Tôi cố gắng tạo ngăn điều hướng với 2 listview riêng biệt bên trong.Thêm 2 listview trong ngăn điều hướng, chỉ có một tác phẩm

Chế độ xem danh sách đầu tiên được gọi là "mDrawerList" cũng được hiển thị. - Chỉ có một mục trong danh sách này.

Chế độ xem danh sách thứ hai được gọi là "mListProcheDeChezVous"không bao giờ được hiển thị. - Có 3 mục trong listview này.

Khi tôi đưa vào nhận xét, listview đầu tiên, thứ hai không được hiển thị, vì vậy tôi nghĩ rằng có vấn đề khi tạo danh sách xem thứ hai .. nhưng tôi không biết ở đâu?

Đây là mã cho cách bố trí hàng hải ngăn kéo:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/dernieres_news" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="#E3E9E3" 
     android:dividerHeight="1dp" 
     android:background="#F3F3F4"/> 

    <ListView 
     android:id="@+id/pres_de_chez_vous" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="#E3E9E3" 
     android:dividerHeight="1dp" 
     android:background="#F3F3F4"/> 

</android.support.v4.widget.DrawerLayout> 

Và đây là một đoạn mã của lớp MainActivity tôi:

public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList, mListProcheDeChezVous; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    private String[] mPlanetTitles; 

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

     mTitle = mDrawerTitle = getTitle(); 
     mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     // Declaration of the 2 listview's 
     mDrawerList = (ListView) findViewById(R.id.dernieres_news); 
     mListProcheDeChezVous= (ListView) findViewById(R.id.pres_de_chez_vous); 

     LayoutInflater inflater = getLayoutInflater(); 

     // Add header news title 
     ViewGroup header_news = (ViewGroup)inflater.inflate(R.layout.header_dernieres_news, mDrawerList, false); 
     mDrawerList.addHeaderView(header_news, null, false); 

     // Add header "proche de chez vous title" 
     ViewGroup header_pres_de_chez_vous = (ViewGroup)inflater.inflate(R.layout.header_pres_de_chez_vous, mListProcheDeChezVous, false); 
     mListProcheDeChezVous.addHeaderView(header_pres_de_chez_vous, null, false); 

     // set a custom shadow that overlays the main content when the drawer opens 
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

     /* 
     * FIRST ADAPTER FOR FIRST LISTVIEW 
     */ 

     String[] names=new String[]{"Dernières News"}; 

     /*Array of Images*/ 
     int[] image = new int[] {R.drawable.ic_action_feed}; 

     List<HashMap<String, String>> listinfo = new ArrayList<HashMap<String, String>>(); 
     listinfo.clear(); 
     for(int i=0;i<1;i++){ 
      HashMap<String, String> hm = new HashMap<String, String>(); 
      hm.put("name", names[i]); 
      hm.put("image", Integer.toString(image[i])); 
      listinfo.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "image", "name" }; 
     int[] to = { R.id.img, R.id.txt }; 
     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to); 
     mDrawerList.setAdapter(adapter); 

     /* 
     * SECOND ADAPTER FOR SECOND LISTVIEW 
     */ 

     String[] names_pres_de_chez_vous = new String[]{"Finistère", "Morbihan", "Côtes d'Armor"}; 

     /*Array of Images*/ 
     int[] image_pres_de_chez_vous = new int[] {R.drawable.ic_action_gear, R.drawable.ic_action_gear, R.drawable.ic_action_gear}; 

     List<HashMap<String, String>> listinfo_pres_de_chez_vous = new ArrayList<HashMap<String, String>>(); 
     listinfo_pres_de_chez_vous.clear(); 
     for(int i=0;i<3;i++){ 
      HashMap<String, String> hm_pres_de_chez_vous = new HashMap<String, String>(); 
      hm_pres_de_chez_vous.put("name", names_pres_de_chez_vous[i]); 
      hm_pres_de_chez_vous.put("image", Integer.toString(image_pres_de_chez_vous[i])); 
      listinfo_pres_de_chez_vous.add(hm_pres_de_chez_vous); 
     } 

     // Keys used in Hashmap 
     String[] from_pres_de_chez_vous = { "image", "name" }; 
     int[] to_pres_de_chez_vous = { R.id.img, R.id.txt }; 
     SimpleAdapter adapter_pres_de_chez_vous = new SimpleAdapter(getBaseContext(), listinfo_pres_de_chez_vous, R.layout.drawer_list_item_pres_de_chez_vous, from_pres_de_chez_vous, to_pres_de_chez_vous); 
     mListProcheDeChezVous.setAdapter(adapter_pres_de_chez_vous); 

     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
     mListProcheDeChezVous.setOnItemClickListener(new DrawerItemClickListener()); 

Cảm ơn bạn đã giúp đỡ của bạn,

BR

+0

Tiến sĩ awerLayout chỉ hỗ trợ hai chế độ xem con. Tại sao bạn cần hai ListView? – Ahmad

Trả lời

4

Theo Tạo một Navigation Drawer guide, DrawerLayouts chỉ nên có hai con. Đầu tiên là chế độ xem nội dung chính và thứ hai là chế độ xem ngăn kéo.

FrameLayout với id của "content_frame" được hiểu là chế độ xem có nội dung chính và ListView có id "dernieres_news" đang được hiểu là bố cục ngăn kéo.

Do đó, bỏ qua ListView thứ ba.

Nếu bạn cần cả hai ListView như một phần của ngăn kéo, bạn nên bọc chúng trong bố cục khác chẳng hạn như LinearLayout.

+2

Cảm ơn, rõ ràng với sự giải thích của bạn.Tôi đã cố gắng nhóm 2 listviews trong một layout tuyến tính nhưng tôi có lỗi này khi ứng dụng được bắt đầu: 'Không thể bắt đầu hoạt động ComponentInfo {com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java .lang.ClassCastException: android.widget.LinearLayout $ LayoutParams không thể chuyển sang android.support.v4.widget.DrawerLayout $ LayoutParams' – wawanopoulos

+0

Hey wawanopoulos, tôi cũng gặp vấn đề giống như của bạn.maybe đây là hạn chế của google, có thể bất kỳ một cung cấp cho một số giải pháp? –

+0

Chỉ trong trường hợp ai đó tình cờ uppon một 'ClassCastException' như @ wawanopoulos và tôi. Ít nhất trong lý do trường hợp của tôi là mã xung quanh của hoạt động vẫn sử dụng ListView đơn cũ cho openDrawer(), closeDrawer(), v.v. – lupz

3

Bọc ListView trong LinearLayout là một giải pháp tuyệt vời. Tôi chỉ làm theo cách này và nó hoạt động. Đây là xml demo của tôi:

<LinearLayout 
     android:id="@+id/drawer_Linearlayout" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/btn_clear" 
      android:layout_width="240dp" 
      android:layout_height="wrap_content" 
      android:text="ABC"/> 

     <ListView 
      android:id="@+id/left_drawer" 
      android:layout_width="240dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:background="#111" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" /> 
    </LinearLayout> 

và chắc chắn để sử dụng drawer_Linearlayout khi bạn sử dụng như openDrawer, closeDrawer. isDrawerMở làm thông số.

0

listview đầu tiên đã android: layout_height = "match_parent" điều này có nghĩa rằng nó chiếm toàn bộ DrawerLayout ... sử dụng android: layout_height = "wrap_content" trong cả listviews

1

Bạn có thể định nghĩa nhiều hơn một ListView/bất kỳ giao diện tùy chỉnh thêm bên trong một ViewGroup:

Mã cho chính hoạt động DrawerLayout XML:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/drawerlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

<!-- This is the fragment Layout --> 

     <FrameLayout 
      android:id="@+id/fragmnetlayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </FrameLayout> 

<!-- this LinearLayout as a Drawerlayout inside it create two ListView With Its Items Title --> 

     <LinearLayout 
      android:id="@+id/lldrawercontent" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/tvsocialsites" 
       android:layout_width="match_parent" 
       android:layout_height="30dp" 
       android:background="#12ccad" 
       android:gravity="center" 
       android:text="@string/socialsites" 
       android:textSize="15sp" /> 

      <ListView 
       android:id="@+id/drawerlistleft" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:background="#ffccee" 
       android:choiceMode="singleChoice" 
       android:divider="@null" /> 

      <TextView 
       android:id="@+id/tvweakdays" 
       android:layout_width="match_parent" 
       android:layout_height="30dp" 
       android:background="#12ccad" 
       android:gravity="center" 
       android:text="@string/weaksdy" 
       android:textSize="15sp" /> 

      <ListView 
       android:id="@+id/drawerlistweakdy" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:background="#32fbdc" 
       android:choiceMode="singleChoice" > 
      </ListView> 
     </LinearLayout> 

    </android.support.v4.widget.DrawerLayout> 
Các vấn đề liên quan