2015-11-14 22 views
0

我是Android編程的新手。最近我試圖構建一個需要drawerlayout的應用程序。我遵循Android開發人員指南中的示例,似乎發現但我發現我無法將onItemClicklistener設置爲drawerLayout中的listView。默認的listview在drawerlayout中的onitemclick不起作用

我試圖谷歌它,發現沒有什麼幫助(setfocusable(false)不工作)。我的片段包含一個listview和一個recyclerview。當我使用調試模式時,我發現程序甚至無法輸入onItemClick方法。希望任何人都能幫忙!

private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView parent, View view, int position, long id) { 
     mDrawerList.setItemChecked(position, true); 
    } 
} 


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

    View rootView = inflater.inflate(R.layout.fragment_enter, container, false); 
    RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_main_content); 
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); 

    rootView.findViewById(R.id.button_add_question).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(getActivity(), NewQuestionActivity.class); 
      startActivity(intent); 
     } 
    }); 

    DrawerLayout mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) rootView.findViewById(R.id.left_drawer); 

    mDrawerList.setAdapter(new ArrayAdapter<>(this.getActivity(), R.layout.list_item_main_drawer, mDrawerItem)); 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    mRecyclerView.setLayoutManager(mLayoutManager); 

    mAdapter = new MainContentListAdapter(mList); 
    mRecyclerView.setAdapter(mAdapter); 


    mRecyclerView.addOnItemTouchListener(
      new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { 
       @Override 
       public void onItemClick(View view, int position) { 
        Intent intent = new Intent(getActivity(), DetailActivity.class); 
        intent.putExtra("questionId", mList.get(position).getQuestionId()); 
        startActivity(intent); 
       } 
      }) 
    ); 

    return rootView; 
} 

我的XML文件

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


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/recycler_main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <com.getbase.floatingactionbutton.FloatingActionsMenu 
     android:id="@+id/button_click" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     fab:fab_addButtonColorNormal="@color/black_semi_transparent" 
     fab:fab_addButtonColorPressed="@color/black_semi_transparent" 
     fab:fab_addButtonPlusIconColor="@color/half_black" 
     fab:fab_labelStyle="@style/menu_labels_style" 
     fab:fab_labelsPosition="left" 
     android:longClickable="false" 
     android:layout_marginBottom="49dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true"> 

     <com.getbase.floatingactionbutton.FloatingActionButton 
      android:id="@+id/button_add_question" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      fab:fab_colorNormal="@color/blue_semi_transparent" 
      fab:fab_title="Raise Quesion" 
      fab:fab_colorPressed="@color/blue_semi_transparent_pressed"/> 

     <com.getbase.floatingactionbutton.FloatingActionButton 
      android:id="@+id/button_nothing" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      fab:fab_colorNormal="@color/blue_semi_transparent" 
      fab:fab_title="nothing" 
      fab:fab_colorPressed="@color/blue_semi_transparent_pressed"/> 

    </com.getbase.floatingactionbutton.FloatingActionsMenu> 

</RelativeLayout> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:dividerHeight="2dp" 
    android:background="#111"/></android.support.v4.widget.DrawerLayout> 

更新:

我發現ListView控件總是落後recyclerView,也許這就是問題所在。不過,我嘗試了bringToFront,但似乎也不起作用。任何幫助?

+0

您可以發佈您'list_item_main_drawer' XML?該佈局的根目錄需要是一個實現Checkable的類,你也可以看看['NavigationView'](https://developer.android.com/reference/android/support/design/widget/ NavigationView.html),它使用菜單資源並處理列表。 – Brucelet

回答

0

試着做這樣的事情:

drawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View view, 
         int position, long id) { 
       Intent intent = new Intent(getActivity(), DetailActivity.class); 
       intent.putExtra("questionId", mList.get(position).getQuestionId()); 
       startActivity(intent); 
    } 
      }); 

相反的:

mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
+0

實際上,startActivity事件由另一個偵聽器(RecyclerView)處理。 – Oh2

+0

但我試過這個,不行。 :( – Oh2

+0

你想做什麼?你想在用戶點擊你的drawerList中的一個項目時開始另一個活動,對嗎?並且取決於點擊了什麼項目,活動應該相應地開始。 –