2015-11-26 99 views
0

其在SlidingMenuFragment.java我希望每個項目都列出可點擊。請幫助實現這一點。如何在導航抽屜上設置onClick監聽器

抽屜ITEMS

我想這3點擊,這樣我可以打開一個新的活動。

data.add("One"); 
data.add("Two"); 
data.add("Three"); 

navigation drawer items

謝謝你的幫助。 下面的代碼是從http://manishkpr.webheavens.com/android-material-design-navigation-drawer-example/

package com.manishkpr.androidmaterialnavigationdrawer; 

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

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.ListView; 
import android.widget.*; 

public class SlidingMenuFragment extends Fragment { 
List<String>data; 
ListView list_view; 
SlidingMenuListAdapter adapter; 

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_sliding_menu, null); 
    setUpView(root); 
    return root; 
} 
void setUpView(ViewGroup root){ 
    list_view = (ListView)root.findViewById(R.id.list_view); 
    initList(); 
    setUpClick(); 
} 
void initList(){ 
    data = new ArrayList<String>(); 

    data.add("One"); 
    data.add("Two"); 
    data.add("Three"); 

    adapter = new SlidingMenuListAdapter(getActivity(),data); 
    list_view.setAdapter(adapter); 
} 
void setUpClick(){ 
    list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       MainActivity.obj.closeDrawer(); 
       Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show(); 
      } 
     }); 
} 
} 
+0

請更詳細地 – NaviRamyle

+0

指出你的問題,我想的是項目一,二,三,可以點擊,這樣我可以開啓新的活動。 –

+0

第一個鏈接是導航抽屜圖像 –

回答

0

試試這個

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       MainActivity.obj.closeDrawer(); 
       Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show(); 

       //Try to include this to your code 
       getActivity().startActivity(new Intent(this, YourAnotherActivity.class)); 
      } 
     }); 
+0

我如何使用開關。你能舉一個例子嗎?謝謝。 –

1

有更深入的解釋一個清潔的實現可以在Android文檔在這裏找到: http://developer.android.com/training/implementing-navigation/nav-drawer.html

這就是說,它看起來你已經有了一些可點擊的列表項目,你現在需要做的就是在你的ItemClickListener中啓動新的活動。你可以讓每個項目通過引用「位置」參數做不同的事情:

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      MainActivity.obj.closeDrawer(); 
      Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show(); 

      // Start 'YourActivity' when first item is clicked 
      if(position == 0){ 
       Intent = new Intent(getActivity(), YourActivity.class); 
       getActivity().startActivity(intent) 
      } 
     } 
    }); 

我認爲你多學習一些基本的東西,比如如何創建新的活動和過於前開始他們來說,這是非常重要的與導航抽屜等特定事物捆綁在一起。

0

舉個例子,你也可以嘗試在你的代碼是這樣的:

if(position == 0) { 
    Intent AlbumsIntent = new Intent(MainActivity.this, avtivity.class); 
    startActivity(AlbumsIntent); 
}else if (position==1){ 
     Intent ArtistsIntent = new Intent(MainActivity.this, avtivity1.class); 
     startActivity(ArtistsIntent); 
}else if (position==2){ 
     Intent SongsIntent = new Intent(MainActivity.this, activity2.class); 
     startActivity(SongsIntent); 
}else if (position==3){ 
     Intent CardIntent = new Intent(MainActivity.this, activity3.class); 
     startActivity(CardIntent); 
}else if (position==4){ 
     Intent LogoutIntent = new Intent(MainActivity.this, avtivity4.class); 
     startActivity(LogoutIntent); 
}