setOnItemClickListener() in navigation drawer not working?

可紊 提交于 2019-12-24 22:03:06

问题


My .java code is :

public class welcome extends Activity 
{
    Button button1;
    private DrawerLayout mDrawerLayout;

    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private String mTitle = "";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        mTitle = "JAVATECHIG.COM";
        getActionBar().setTitle(mTitle);

        // Getting reference to the DrawerLayout
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerList = (ListView) findViewById(R.id.drawer_list);
        mDrawerList.setClickable(true);
        mDrawerList.setTextFilterEnabled(true);
        // Getting reference to the ActionBarDrawerToggle
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();

            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("JAVATECHIG.COM");
                Toast.makeText(getApplicationContext(), "Inside onDrawerOpened...", Toast.LENGTH_SHORT).show();
                invalidateOptionsMenu();
            }

        };

        // Setting DrawerToggle on DrawerLayout
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // Getting an array of rivers
                String[] menuItems = getResources().getStringArray(R.array.menus);
                Toast.makeText(getApplicationContext(), "Inside ClickListener...", Toast.LENGTH_SHORT).show();
                // Currently selected river
                mTitle = menuItems[position];

                // Creating a fragment object
                WebViewFragment rFragment = new WebViewFragment();

                // Passing selected item information to fragment
                Bundle data = new Bundle();
                data.putInt("position", position);
                data.putString("url", getUrl(position));
                rFragment.setArguments(data);


                // Getting reference to the FragmentManager
                FragmentManager fragmentManager = getFragmentManager();

                // Creating a fragment transaction
                FragmentTransaction ft = fragmentManager.beginTransaction();

                // Adding a fragment to the fragment transaction
                ft.replace(R.id.content_frame, rFragment);

                // Committing the transaction
                ft.commit();

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);

            }
        });

        // Creating an ArrayAdapter to add items to the listview mDrawerList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
                R.layout.drawer_list_item, getResources().getStringArray(R.array.menus));

        // Setting the adapter on mDrawerList
        mDrawerList.setAdapter(adapter);
        Toast.makeText(getApplicationContext(), "After ArrayAdapter decraration...", Toast.LENGTH_SHORT).show();
        // Enabling Home button
        getActionBar().setHomeButtonEnabled(true);

        // Enabling Up navigation
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Setting item click listener for the listview mDrawerList

    }

    protected String getUrl(int position) {
        switch (position) {
        case 0:
            return "http://javatechig.com";
        case 1:
            return "http://javatechig.com/category/android/";
        case 2:
            return "http://javatechig.com/category/blackberry/";
        case 3:
            return "http://javatechig.com/category/j2me/";
        case 4:
            return "http://javatechig.com/category/sencha-touch/";
        case 5:
            return "http://javatechig.com/category/phonegap/";
        case 6:
            return "http://javatechig.com/category/java/";
        default:
            return "http://javatechig.com";
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /** Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Also my XML code is:

<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" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:descendantFocusability="blocksDescendants"
        />
</android.support.v4.widget.DrawerLayout>

WebViewFragement.java code is:

public class WebViewFragment extends Fragment {

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

        // Retrieving the currently selected item number
        int position = getArguments().getInt("position");

        String url = getArguments().getString("url");

        // List of rivers
        String[] menus = getResources().getStringArray(R.array.menus);

        // Creating view corresponding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Updating the action bar title
        getActivity().getActionBar().setTitle(menus[position]);

        //Initializing and loading url in webview
        //WebView webView = (WebView)v.findViewById(R.id.webView); 
        //webView.getSettings().setJavaScriptEnabled(true);
        //webView.loadUrl(url);
        Toast.makeText(getActivity(),position+" ... "+url,Toast.LENGTH_SHORT).show();
        return v;
    }
}

.

.

My Question is whenever I run this code and click on any of the side menu option, it is not displaying anything. Means it is not going inside setOnItemClickListener(). I have tried all possible solution posted in this form but, these are not working. Kindly help.


回答1:


the error is probably in this line mDrawerList.setOnItemClickListener(new OnItemClickListener()) instead of implementing onItemClickListener implement list_item.setOnItemClickListener(new DrawerItemClickListener());



来源:https://stackoverflow.com/questions/26271249/setonitemclicklistener-in-navigation-drawer-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!