English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Introduction à l'utilisation de DrawerLayout dans Android (Menu glissant)

Dans android support.v4 Il y a un contrôle de vue de tiroir DrawerLayout. En utilisant ce contrôle, vous pouvez générer un menu qui peut être ouvert ou fermé en glissant horizontalement sur l'écran, ce qui offre une bonne expérience utilisateur.

DrawerLayout est divisé en deux parties : le menu latéral et la zone de contenu principal. Le menu latéral peut être déployé et masqué en fonction des gestes, et la zone de contenu principal peut changer en fonction des clics sur le menu. DrawerLayout est en fait un contrôle, similaire à LinearLayout, et peut être utilisé directement.

DrawerLayout properties

1、drawerPosition: Specifies that the drawer will slide from one side of the screen.

2、drawerWidth : Specifies the width of the drawer, that is, the exact width from the edge of the window to the view.

3、keyboardDismissMode : Determines whether the keyboard responds to the rejection of dragging.

  • 'none' (default value), dragging does not affect the keyboard.
  • 'on-drag', dragging starts, the keyboard is rejected.

4、onDrawerClose : The function is called when the navigation view is closed.

5、onDrawerOpen : The function is called when the navigation view is opened.

6、onDrawerSlide : The function is called when interacting with the navigation view.

7、onDrawerStateChanged : The function is called when the drawer state changes, drawer has 3 types of status: 

  •  idle -- means there is no interaction with the navigation view
  •  dragging -- means there is currently interaction with the navigation view
  •  settling -- means there is interaction with the navigation view and the navigation view is currently closing or opening.

8、renderNavigationView : The navigation view will be rendered on one side of the screen and can be pulled out.

example

using import dependency library

compile 'com.android.support:appcompat-v7:24.2.1' 

layout file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@"+id/v4_drawerlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <FrameLayout
    android:id="@"+id/v4_drawerlayout_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@"+id/v4_text"
      android:textSize="22sp"
      android:textColor="@color/colorAccent"
      android:gravity="center"
      />
  </FrameLayout>
  <ListView
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:id="@"+id/v4_listview"
    android:choiceMode="singleChoice"
    android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout> 

Activity

public class DrawerActivity extends AppCompatActivity {
  private ListView listView;
  private DrawerLayout drawerLayout;
  private TextView textView;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_activity);
    initView();
  }
  private void initView()
  {
    listView=(ListView) findViewById(R.id.v4_listview);
    drawerLayout=(DrawerLayout) findViewById(R.id.v4_drawerlayout);
    textView=(TextView) findViewById(R.id.v4_text);
    initDate();
  }
  private void initDate(){
    final List<String> list = new ArrayList<String>();
    list.add("网易");
    list.add("腾讯");
    list.add("新浪");
    list.add("搜狐");
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<63;> parent, View view, int position, long id) {
        textView.setText(list.get(position));
        showDrawerLayout();
      }
    });
    drawerLayout.openDrawer(Gravity.LEFT);//Ouvrir le glissaire latéral, sans configuration, il ne s'ouvrira pas par défaut
  }
  private void showDrawerLayout() {
    if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
      drawerLayout.openDrawer(Gravity.LEFT);
    }
      drawerLayout.closeDrawer(Gravity.LEFT);
    }
  }
} 

L'effet de fonctionnement est représenté dans l'image suivante :

Adresse de téléchargement :Drawerlayout_jb51.rar

C'est tout le contenu de cet article, j'espère qu'il vous aidera dans vos études, et j'espère que vous soutiendrez également le tutoriel criant.

Déclaration : Le contenu de cet article est fourni par Internet, la propriété intellectuelle appartient aux auteurs respectifs, le contenu est apporté par les utilisateurs d'Internet de manière spontanée et auto-publiée, ce site ne détient pas de propriété, n'a pas été traité par l'éditeur humain et n'assume aucune responsabilité juridique. Si vous trouvez du contenu susceptible de violer les droits d'auteur, vous êtes invité à envoyer un e-mail à : notice#w3Déclaration : Le contenu de cet article est issue du réseau, la propriété intellectuelle appartient à ses auteurs respectifs, le contenu est apporté par les utilisateurs d'Internet de manière spontanée et auto-publiée, ce site ne détient pas de propriété, n'a pas été traité par l'éditeur humain et n'assume aucune responsabilité juridique. Si vous trouvez du contenu susceptible de violer les droits d'auteur, vous êtes invité à envoyer un e-mail à : notice#w

Vous pourriez aussi aimer