listview项目在Navigation Drawer中不起作用

时间:2015-04-12 14:26:07

标签: android listview navigation-drawer

原谅我的英语,我是android的新手并创建应用程序。每件事情都很好但我有一个非常小的问题是在导航抽屉里当我点击项目只是导航抽屉被关闭它没有错误没有任何东西错误的logcat真的我不知道但我猜mybe从布局。 我的布局:

    <?xml version="1.0" encoding="utf-8"?>
      <android.support.v4.widget.DrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

        <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/container"
           xmlns:tools="http://schemas.android.com/tools"
           android:layout_width="match_parent"
           tools:context=".MyPayamak"
           android:layout_height="match_parent"
           android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:background="#ffffff"
        android:dividerHeight="10sp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
    </ListView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tt"/>

    <ProgressBar
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_gravity="center"
        android:id="@+id/progressBar"/>

      </LinearLayout>
   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/bar"
        android:orientation="vertical">

        <!-- Side navigation drawer UI -->


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="کاربر میهمان"
            android:textColor="#3D3D3D"
            android:background="#ffffff"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="15dp"
            android:id="@+id/ShowFakenameOfUser"/>

        <ImageView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/usericon"/>

        <ListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:divider="@null"
            android:dividerHeight="10dp"
            android:layout_marginTop="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"/>

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

另一件事我的导航抽屉代码在所有活动中都是相同的并且工作正常,但有2个活动有此问题使用此布局。

在我的oncreate中有这段代码:

mDrawerList = (ListView)findViewById(R.id.navList);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();



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

            if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ورود")) {
                Intent it = new Intent(List_Payamak_ha.this, loginUser.class);
                startActivity(it);
            }else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("ثبت نام")){
                Intent it = new Intent(List_Payamak_ha.this, RegisterUser.class);
                startActivity(it);
            }else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران فعال")) {
                Intent it = new Intent(List_Payamak_ha.this, ActiveUsers.class);
                startActivity(it);
            }else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("کاربران برتر")) {
                Intent it = new Intent(List_Payamak_ha.this, TopUsers.class);
                startActivity(it);
            }else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("درباره نرم افزار")) {
                Intent it = new Intent(List_Payamak_ha.this, AboutPage.class);
                startActivity(it);
            }else if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("خروج")) {
                int pid = android.os.Process.myPid();
                android.os.Process.killProcess(pid);
                finish();
                System.exit(0);
            }

        }
    });


    addDrawerItems();
    setupDrawer();

整个代码:

    private void addDrawerItems() {
        String[] osArray = getResources().getStringArray(R.array.SlideMenu);
        mAdapterCopy = new MenuSideAdapterItem(this,
                android.R.layout.simple_list_item_1,
                R.id.textViewMenuSide,
                osArray);
        mDrawerList.setAdapter(mAdapterCopy);

    }

    private void setupDrawer() {
;
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view);
                getSupportActionBar().setTitle("منو");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }


            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

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

    private class MenuSideAdapterItem extends ArrayAdapter<String> {

        public MenuSideAdapterItem(Context context, int resource, int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View Row = inflater.inflate(R.layout.menu_item,parent,false);
            String[] items =  getResources().getStringArray(R.array.SlideMenu);

            ImageView IconView = (ImageView) Row.findViewById(R.id.imageViewMenuSide);
            TextView textMenuSide = (TextView) Row.findViewById(R.id.textViewMenuSide);
            textMenuSide.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Far_TitrDF.ttf"));

            textMenuSide.setText(items[position]);

            if (items[position].equalsIgnoreCase("ورود")){
                IconView.setImageResource(R.drawable.loginicon);
            }else if (items[position].equalsIgnoreCase("ثبت نام")){
                IconView.setImageResource(R.drawable.registericoon);
            }else if (items[position].equalsIgnoreCase("کاربران فعال")){
                IconView.setImageResource(R.drawable.activeuser);
            }else if (items[position].equalsIgnoreCase("کاربران برتر")){
                IconView.setImageResource(R.drawable.topuser);
            }else if (items[position].equalsIgnoreCase("درباره نرم افزار")){
                IconView.setImageResource(R.drawable.about);
            }else if (items[position].equalsIgnoreCase("خروج")){
                IconView.setImageResource(R.drawable.exit);
            }
            return Row;
        }
    }

3 个答案:

答案 0 :(得分:1)

问题来自另一个片段,用于屏幕底部的显示图像,片段在顶部抽屉上,当我点击抽屉实际上是我点击片段因此我从未点击按钮,因为我用{替换fragment {1}}并解决我的问题。

答案 1 :(得分:1)

在您的情况下,使用function时非常糟糕,但现在您可以使用NavigationViewtutorial

答案 2 :(得分:0)

我认为控制不会在你的if条件下进行,所以按照

进行

使用

在每个if语句中打印1条消息
Log.i("tag","any_message"); 

System.out.println("any_message");

2同时使用正确的消息打印添加else语句

并通过这种方式检查您的logcat,您将了解控制是否在if语句内部。