单击导航抽屉列表视图项每次都会打开未单击的活动

时间:2018-03-13 06:51:25

标签: android listview navigation-drawer

我正在SettingsActivity中实施服务和接收器,当我第一次打开应用程序时没有问题,但是一旦我访问SettingsActivity然后回来,关闭导航抽屉每次打开SettingsActivity即使不点击列表视图中的任何项目 这是我的MainActivity布局文件,我已经实现了导航抽屉: -

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

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/top_view"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.3"
                android:background="#00AFF0"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/ship_name"
                    android:layout_width="wrap_content"
                    android:minWidth="120dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:textColor="#00AFF0"
                    android:background="@drawable/button_background"
                    android:padding="10dp"
                    android:textSize="16sp"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:layout_marginTop="10dp">

                    <ImageButton
                        android:id="@+id/date_reset"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_reset"
                        android:background="@android:color/transparent"
                        android:layout_marginEnd="10dp"
                        android:visibility="gone"/>
                    <TextView
                        android:id="@+id/current_date"
                        android:layout_width="wrap_content"
                        android:minWidth="120dp"
                        android:layout_height="40dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:textColor="@android:color/white"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"
                        android:textSize="16sp"
                        />
                    <ImageButton
                        android:id="@+id/date_select"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_action_date"
                        android:background="@android:color/transparent"
                        android:layout_marginStart="10dp"/>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tickets_layout"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="10dp"
                    android:layout_gravity="center"
                    android:layout_marginEnd="20dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/button_border"
                    android:gravity="center"
                    android:minWidth="120dp"
                    android:orientation="horizontal"
                    android:textColor="@android:color/white">

                    <TextView
                        android:id="@+id/ticket_no_count"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="0.2"
                        android:gravity="end|center"
                        android:padding="2dp"
                        android:text=""
                        android:textColor="#FFFF00"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="0.2"
                        android:padding="2dp"
                        android:gravity="center_vertical"
                        android:text="@string/tickets"
                        android:textAllCaps="true"
                        android:textColor="@android:color/white"
                        android:textSize="16sp" />

                    <View
                        android:layout_width="2dp"
                        android:layout_height="match_parent"
                        android:background="@android:color/white" />

                    <TextView
                        android:id="@+id/no_of_people_count"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="0.2"
                        android:gravity="end|center"
                        android:padding="2dp"
                        android:textSize="16sp"
                        android:textColor="#FFFF00" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="0.2"
                        android:padding="2dp"
                        android:text="@string/people"
                        android:textSize="16sp"
                        android:textAllCaps="true"
                        android:gravity="center_vertical"
                        android:textColor="@android:color/white" />

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txt_no_data"
                    android:gravity="center|center_horizontal"
                    android:textSize="18sp"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"
                    android:visibility="gone"
                    android:paddingTop="15dp"
                    />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/search_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="vertical"
                android:layout_gravity="center">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp">

                    <EditText
                        android:id="@+id/ticket_no"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="0.8"
                        android:layout_marginStart="20dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginEnd="5dp"
                        android:background="@drawable/edittextbackground"
                        android:imeOptions="actionDone"
                        android:paddingStart="10dp"
                        android:paddingEnd="0dp"
                        android:maxLength="19"
                        android:inputType="number"
                        android:digits="01234 56789"
                        android:maxLines="1"
                        />

                    <Button
                        android:id="@+id/search_ticket_button"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_marginEnd="20dp"
                        android:layout_weight="0.2"
                        android:textColor="@android:color/white"
                        android:text="@string/go"
                        android:textSize="16sp"
                        android:textAllCaps="true"
                        android:background="@drawable/go_button_shape"/>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/or_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="30dp"
                    android:orientation="horizontal"
                    android:paddingRight="30dp">
                    <View
                        android:layout_width="0dp"
                        android:layout_height="2dp"
                        android:layout_weight="0.4"
                        android:layout_gravity="center_vertical"
                        android:background="@android:color/black"
                        />
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.1"
                        android:text="@string/or"
                        android:textSize="20sp"
                        android:gravity="center"/>
                    <View
                        android:layout_width="0dp"
                        android:layout_height="2dp"
                        android:layout_weight="0.4"
                        android:layout_gravity="center_vertical"
                        android:background="@android:color/black"
                        />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/scan_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:orientation="vertical"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/scanQr"
                    android:layout_width="wrap_content"
                    android:minWidth="120dp"
                    android:layout_height="40dp"
                    android:text="@string/scan_qr"
                    android:layout_gravity="center|top"
                    android:gravity="center"
                    android:textColor="@android:color/white"
                    android:background="@drawable/scan_button"
                    android:padding="10dp"
                    android:layout_marginTop="10dp"
                    android:textSize="16sp"
                    />
                <me.dm7.barcodescanner.zxing.ZXingScannerView
                    android:id="@+id/scanner"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"/>
                <TextView
                    android:id="@+id/scan_results"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginTop="50dp"
                    android:layout_marginBottom="10dp"
                    android:visibility="visible"
                    android:textStyle="bold"
                    android:textSize="18sp"
                    android:gravity="left"
                    android:layout_marginStart="20dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginEnd="5dp"
                    android:paddingStart="10dp"
                    android:paddingEnd="0dp"
                    />

            </LinearLayout>

            <TextView
                android:id="@+id/no_trips_tv"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_gravity="center"
                android:gravity="center"
                android:textColor="@android:color/darker_gray"
                android:textSize="18sp"
                android:text="@string/no_trips"
                android:visibility="gone"/>

        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/layout_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:alpha="0.5"
        />

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFF"
        android:choiceMode="singleChoice"
        />

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

在我的MainActivity i处理项目中,点击navListListviewdrawerDrawerLayout的事件为navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id){ drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){ @Override public void onDrawerClosed(View drawerView){ super.onDrawerClosed(drawerView); if (position == 0){ } else if(position == 1) { getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate); } else if(position == 2) { submitTickets(); } else if(position == 3) { Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class); startActivity(settingsIntent); } else { Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext()); finish(); } } }); drawer.closeDrawer(navList); } });

<?php

namespace App\Controller;

use App\Form\UserType;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\VarDumper\VarDumper;

class RegistrationController extends Controller
{
    /**
     * @Route("/register", name="user_registration")
     */
    public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder)
    {
        $user = new User();
        $form = $this->createForm(UserType::class, $user);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);
            dump($user);

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            return $this->redirectToRoute('number', array('slug' => 1500, 'dump' => $user));
        } else {

        return $this->render(
            'registration/register.html.twig',
            array('form' => $form->createView())
        );
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

删除drawer.setDrawerListener并将代码直接放在onItemClick

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

               switch(position)
                {
                    case:0
                         {
                             break;
                         }
                    case:1
                    {
                            getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate);
                            break;
                    }
                    case:2 
                    {

                        submitTickets();
                        break;

                    }
                    case:3 
                    {

                        Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
                        startActivity(settingsIntent);
                        break;

                    } 
                    case:4{
                        Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext());
                        finish();
                        break;
                    }
                    default:
                }
}
            });
            drawer.closeDrawer(navList);
    }