如何在导航抽屉上设置onClick侦听器

时间:2015-11-26 03:03:47

标签: android onclicklistener

它在 SlidingMenuFragment.java 中,我希望每个项目都可以点击。请帮助实现这一目标。

抽奖项目

我希望这3个可点击,以便我可以打开一个新活动。

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();
            }
        });
}
}

navigation drawer items

感谢您的帮助。 以下代码来自http://manishkpr.webheavens.com/android-material-design-navigation-drawer-example/

person

3 个答案:

答案 0 :(得分:1)

在这里的android文档中可以找到更清晰的解释更清晰的实现: http://developer.android.com/training/implementing-navigation/nav-drawer.html

也就是说,看起来你已经有了一些可点击的列表项,现在你需要做的就是在你的ItemClickListener中启动新的活动。您可以通过引用“position”参数使每个项目执行不同的操作:

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)
            }
        }
    });

我认为重要的是你要学习一些更基本的东西,比如如何创建新的活动,然后在与导航抽屉等具体事物过于紧密的情况下启动它们。

答案 1 :(得分: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));
            }
        });

答案 2 :(得分: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);
}