android實現(xiàn)左右側(cè)滑菜單效果
在android開發(fā)中,左右側(cè)滑菜單的開發(fā)已成為我們現(xiàn)在開發(fā)的必備技術(shù)之一,再次之前,我沒有做過相類似的demo,但是項目的開發(fā)有要求有這樣的效果,而且大家都知道,雖然網(wǎng)上由開源的代碼,但是不僅種類多,看著一個頭兩個大,而且代碼不好分離。因此我們無法簡化成自己的demo,為此,還查閱了很多別人的資料,最后做出了自己想要的效果,具體效果如下所示:
圖1 左邊菜單
圖2 右邊菜單
今天要做的是把兩個效果結(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è)拉菜單 主頁面顯示的效果 要寫在布局的最上面 首先進(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)和動態(tài)Fragment的使用 * 靜態(tài) 直接在布局中使用<fragment /> * 動態(tài) 使用管理器 得到一個事務(wù) 然后使用事務(wù)調(diào)用replace方法 把一個Fragment對象替換到指定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 一般是只有一個 如果再次調(diào)用會把前面的覆蓋掉 * 和 * add 把數(shù)據(jù)添加進(jìn)去 不會覆蓋之前的內(nèi)容 */ dl.addDrawerListener(new DrawerListener() { //滑動狀態(tài)發(fā)生改變的時候 會調(diào)用該方法 @Override public void onDrawerStateChanged(int arg0) { // TODO Auto-generated method stub Log.i("===============================", "StateChanged" + arg0); } //監(jiān)聽滑動過程中 邊界的位置 @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); //把點擊的listview控件中的值 賦值到主Fragment對象中 MainFragment fragment = (MainFragment) getSupportFragmentManager().findFragmentByTag("main"); fragment.setData(list.get(position)); } }); } /** * 在側(cè)拉效果的頁面中 用來顯示主頁面的效果 */ private void showMain() { //動態(tài)加載Fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //參數(shù)1:FramLayout控件的id, 要替換的Fragment對象 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)容,可以點擊《Android側(cè)滑效果匯總》學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android自定義實現(xiàn)側(cè)滑菜單效果
- Android實現(xiàn)QQ側(cè)滑菜單效果
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- android側(cè)滑菜單控件DrawerLayout使用方法詳解
- Android使用DrawerLayout實現(xiàn)雙向側(cè)滑菜單
- Android中DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android Drawerlayout實現(xiàn)側(cè)滑菜單效果
- Android使用DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android實現(xiàn)側(cè)滑菜單DrawerLayout
相關(guān)文章
一款A(yù)ndroid APK的結(jié)構(gòu)構(gòu)成解析
本篇文章介紹了我在學(xué)習(xí)過程中對于Android 程序的理解總結(jié),刨析了apk的組成與產(chǎn)生過程,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下2021-10-10Android EditText長按菜單中分享功能的隱藏方法
Android EditText控件是經(jīng)常使用的控件,下面這篇文章主要給大家介紹了關(guān)于Android中EditText長按菜單中分享功能的隱藏方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Python基礎(chǔ)教程學(xué)習(xí)筆記 第二章 列表和元組
這篇文章主要介紹了Python基礎(chǔ)教程學(xué)習(xí)筆記 第二章 列表和元組,需要的朋友可以參考下2015-03-03Android Studio 利用Splash制作APP啟動界面的方法
這篇文章主要介紹了Android Studio 利用Splash制作APP啟動界面,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Android實戰(zhàn)教程第七篇之如何在內(nèi)存中存儲用戶名和密碼
這篇文章主要為大家詳細(xì)介紹了Android如何實現(xiàn)在內(nèi)存中存儲用戶名和密碼的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11android自定義popupwindow仿微信右上角彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11