欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實(shí)現(xiàn)左右側(cè)滑菜單效果

 更新時(shí)間:2017年10月10日 10:31:02   作者:人走丿茶涼  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)左右側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在android開發(fā)中,左右側(cè)滑菜單的開發(fā)已成為我們現(xiàn)在開發(fā)的必備技術(shù)之一,再次之前,我沒有做過相類似的demo,但是項(xiàng)目的開發(fā)有要求有這樣的效果,而且大家都知道,雖然網(wǎng)上由開源的代碼,但是不僅種類多,看著一個(gè)頭兩個(gè)大,而且代碼不好分離。因此我們無法簡(jiǎn)化成自己的demo,為此,還查閱了很多別人的資料,最后做出了自己想要的效果,具體效果如下所示:

圖1 左邊菜單

這里寫圖片描述 

圖2 右邊菜單

這里寫圖片描述

今天要做的是把兩個(gè)效果結(jié)合在一起,左右側(cè)滑菜單

話不多說,直接上代碼:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <android.support.v4.widget.DrawerLayout
 android:id="@+id/dl"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_above="@+id/tv" >

 <!-- 作為側(cè)拉菜單 主頁(yè)面顯示的效果 要寫在布局的最上面 首先進(jìn)行加載 -->
 <FrameLayout
  android:id="@+id/fl"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
 </FrameLayout>

 <ListView
  android:id="@+id/lv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ff0"
  android:layout_gravity="left" >
 </ListView>

 <LinearLayout
  android:id="@+id/ll"
  android:layout_width="200dp"
  android:layout_height="match_parent"
  android:layout_gravity="right"
  android:background="#0ff"
  android:orientation="vertical" >

  <ImageView
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:src="@drawable/ic_launcher" />

  <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margin="20dp"
  android:text="呵呵呵" />
 </LinearLayout>
 </android.support.v4.widget.DrawerLayout>

</LinearLayout>


frag_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/tv_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="20dp"
 android:text="標(biāo)題" />


 <ImageView
 android:id="@+id/iv"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:src="@drawable/ic_launcher" />

</LinearLayout>

MainActivity.java:

import java.util.ArrayList;
import java.util.List;

import com.example.day12drawerlayout1.fragment.MainFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * 1、靜態(tài)和動(dòng)態(tài)Fragment的使用
 * 靜態(tài) 直接在布局中使用<fragment />
 * 動(dòng)態(tài) 使用管理器 得到一個(gè)事務(wù) 然后使用事務(wù)調(diào)用replace方法 把一個(gè)Fragment對(duì)象替換到指定id的FramLayout幀布局中
 * @author Administrator
 *
 */
public class MainActivity extends FragmentActivity {

 DrawerLayout dl;
 ListView lv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 dl = (DrawerLayout) findViewById(R.id.dl);
 // FrameLayout fl = (FrameLayout) findViewById(R.id.fl);
 // fl.setOnClickListener(new OnClickListener() {
 //  
 //  @Override
 //  public void onClick(View v) {
 //  // TODO Auto-generated method stub
 //  dl.openDrawer(Gravity.RIGHT);
 //  }
 // });

 /**
  * set 一般是只有一個(gè) 如果再次調(diào)用會(huì)把前面的覆蓋掉
  * 和 
  * add 把數(shù)據(jù)添加進(jìn)去 不會(huì)覆蓋之前的內(nèi)容
  */
 dl.addDrawerListener(new DrawerListener() {

  //滑動(dòng)狀態(tài)發(fā)生改變的時(shí)候 會(huì)調(diào)用該方法
  @Override
  public void onDrawerStateChanged(int arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "StateChanged" + arg0);
  }

  //監(jiān)聽滑動(dòng)過程中 邊界的位置
  @Override
  public void onDrawerSlide(View arg0, float arg1) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerSlide" + arg1);
  }

  //監(jiān)聽側(cè)拉是否完全展開
  @Override
  public void onDrawerOpened(View arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerOpened");
  }

  //監(jiān)聽側(cè)拉是否被關(guān)閉
  @Override
  public void onDrawerClosed(View arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerClosed");
  }
 });

 showMain();
 showLV();
 }

 public DrawerLayout getDL(){
 return dl;
 }

 private void showLV() {
 lv = (ListView) findViewById(R.id.lv);
 final List<String> list = new ArrayList<String>();
 for (int i = 1; i < 30; i++) {
  list.add("條目"+i);
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
 lv.setAdapter(adapter);

 lv.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view,
   int position, long id) {
  // TODO Auto-generated method stub
  dl.closeDrawer(Gravity.LEFT);
  //把點(diǎn)擊的listview控件中的值 賦值到主Fragment對(duì)象中
  MainFragment fragment = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
  fragment.setData(list.get(position));
  }
 });
 }

 /**
 * 在側(cè)拉效果的頁(yè)面中 用來顯示主頁(yè)面的效果
 */
 private void showMain() {
 //動(dòng)態(tài)加載Fragment
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 //參數(shù)1:FramLayout控件的id, 要替換的Fragment對(duì)象
 transaction.replace(R.id.fl, new MainFragment(), "main");
 transaction.commit();
 }

}

MainFragment.java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day12drawerlayout1.MainActivity;
import com.example.day12drawerlayout1.R;

public class MainFragment extends Fragment{
 TextView tv;
 ImageView iv;

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

 View view = View.inflate(getActivity(), R.layout.frag_main, null);

 tv = (TextView) view.findViewById(R.id.tv_title);
 iv = (ImageView) view.findViewById(R.id.iv);

 iv.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  MainActivity activity = (MainActivity) getActivity();
  activity.getDL().openDrawer(Gravity.RIGHT);
  }
 });

 return view;
 }

 public void setData(String str){
 tv.setText(str);
 }
}

更多學(xué)習(xí)內(nèi)容,可以點(diǎn)擊《Android側(cè)滑效果匯總》學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android架構(gòu)發(fā)展進(jìn)化詳解

    Android架構(gòu)發(fā)展進(jìn)化詳解

    Android系統(tǒng)架構(gòu)從上到下分為五層:應(yīng)用層、應(yīng)用框架層、系統(tǒng)運(yùn)行庫(kù)層、硬件抽象層、Linux內(nèi)核層,Android架構(gòu)也經(jīng)歷了多次演進(jìn),下面我們來詳細(xì)了解一下
    2022-08-08
  • Android實(shí)現(xiàn)檢測(cè)實(shí)體按鍵事件并屏蔽

    Android實(shí)現(xiàn)檢測(cè)實(shí)體按鍵事件并屏蔽

    這篇文章主要介紹了Android實(shí)現(xiàn)檢測(cè)實(shí)體按鍵事件并屏蔽 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 一款A(yù)ndroid APK的結(jié)構(gòu)構(gòu)成解析

    一款A(yù)ndroid APK的結(jié)構(gòu)構(gòu)成解析

    本篇文章介紹了我在學(xué)習(xí)過程中對(duì)于Android 程序的理解總結(jié),刨析了apk的組成與產(chǎn)生過程,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Android EditText長(zhǎng)按菜單中分享功能的隱藏方法

    Android EditText長(zhǎng)按菜單中分享功能的隱藏方法

    Android EditText控件是經(jīng)常使用的控件,下面這篇文章主要給大家介紹了關(guān)于Android中EditText長(zhǎng)按菜單中分享功能的隱藏方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Python基礎(chǔ)教程學(xué)習(xí)筆記 第二章 列表和元組

    Python基礎(chǔ)教程學(xué)習(xí)筆記 第二章 列表和元組

    這篇文章主要介紹了Python基礎(chǔ)教程學(xué)習(xí)筆記 第二章 列表和元組,需要的朋友可以參考下
    2015-03-03
  • Android Studio 利用Splash制作APP啟動(dòng)界面的方法

    Android Studio 利用Splash制作APP啟動(dòng)界面的方法

    這篇文章主要介紹了Android Studio 利用Splash制作APP啟動(dòng)界面,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Android多返回棧技術(shù)

    Android多返回棧技術(shù)

    本文將詳情講解用戶通過系統(tǒng)返回按鈕導(dǎo)航回去的一組頁(yè)面,在開發(fā)中被稱為返回棧 (back stack)。多返回棧即一堆 "返回棧",對(duì)多返回棧的支持是在 Navigation 2.4.0-alpha01 和 Fragment 1.4.0-alpha01 中開始的,有興趣的話一起參與學(xué)習(xí)
    2021-08-08
  • Android實(shí)戰(zhàn)教程第七篇之如何在內(nèi)存中存儲(chǔ)用戶名和密碼

    Android實(shí)戰(zhàn)教程第七篇之如何在內(nèi)存中存儲(chǔ)用戶名和密碼

    這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)在內(nèi)存中存儲(chǔ)用戶名和密碼的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • android自定義popupwindow仿微信右上角彈出菜單效果

    android自定義popupwindow仿微信右上角彈出菜單效果

    這篇文章主要為大家詳細(xì)介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android 手機(jī)屏幕適配解決辦法

    Android 手機(jī)屏幕適配解決辦法

    這篇文章主要介紹了Android 手機(jī)屏幕適配的相關(guān)資料,在開發(fā)Android 手機(jī)開發(fā)的時(shí)候經(jīng)常會(huì)有很多手機(jī)品牌和手機(jī)屏幕尺寸問題,需要的朋友可以參考下
    2016-10-10

最新評(píng)論