Reputation: 21
Its in SlidingMenuFragment.java and I want each item listed clickable. Please help to achieve this.
DRAWER ITEMS
I want this 3 clickable so that I can open a new activity.
data.add("One");
data.add("Two");
data.add("Three");
Thank you for helping. The code below is from 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();
}
});
}
}
Upvotes: 0
Views: 6424
Reputation: 191
As an example you can also try something like this in your code :
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);
}
Upvotes: 0
Reputation: 801
A cleaner implementation with more thorough explanations can be found in the android documentation here: http://developer.android.com/training/implementing-navigation/nav-drawer.html
That said, it looks like you already have some clickable list items, all you need to do now is start the new activity within your ItemClickListener. You can make each item do a different thing by referencing the 'position' parameter:
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)
}
}
});
I think it is important that you learn some more basic things, such as how to create new Activities and start them before getting too tied up with specific things like Navigation Drawers.
Upvotes: 1
Reputation: 4007
Try this one
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));
}
});
Upvotes: 0