本文介绍了setOnItemClickListener()的抽屉式导航不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.java code是:

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

此外我的XML code是:

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是:

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

我的问题是,每当我运行此code和点击任何侧面菜单选项,它并不显示任何内容。意味着它是不是里面去setOnItemClickListener()。
我已经试过张贴在这种形式的所有可能的解决办法,但是,这些都没有工作。
请帮助。

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.

推荐答案

该错误可能是在这一行 mDrawerList.setOnItemClickListener(新OnItemClickListener()),而不是实施 onItemClickListener 实施 list_item.setOnItemClickListener(新DrawerItemClickListener());

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

这篇关于setOnItemClickListener()的抽屉式导航不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:14