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

今天要做的是把兩個效果結合在一起,左右側滑菜單
話不多說,直接上代碼:
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" > <!-- 作為側拉菜單 主頁面顯示的效果 要寫在布局的最上面 首先進行加載 --> <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="標題" /> <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) 使用管理器 得到一個事務 然后使用事務調用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 一般是只有一個 如果再次調用會把前面的覆蓋掉
* 和
* add 把數(shù)據(jù)添加進去 不會覆蓋之前的內容
*/
dl.addDrawerListener(new DrawerListener() {
//滑動狀態(tài)發(fā)生改變的時候 會調用該方法
@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)聽側拉是否完全展開
@Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
Log.i("===============================", "DrawerOpened");
}
//監(jiā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));
}
});
}
/**
* 在側拉效果的頁面中 用來顯示主頁面的效果
*/
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);
}
}
更多學習內容,可以點擊《Android側滑效果匯總》學習。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android側滑菜單之DrawerLayout用法詳解
- Android自定義實現(xiàn)側滑菜單效果
- Android實現(xiàn)QQ側滑菜單效果
- Android側滑菜單控件DrawerLayout使用詳解
- android側滑菜單控件DrawerLayout使用方法詳解
- Android使用DrawerLayout實現(xiàn)雙向側滑菜單
- Android中DrawerLayout實現(xiàn)側滑菜單效果
- Android Drawerlayout實現(xiàn)側滑菜單效果
- Android使用DrawerLayout實現(xiàn)側滑菜單效果
- Android實現(xiàn)側滑菜單DrawerLayout
相關文章
Android EditText長按菜單中分享功能的隱藏方法
Android EditText控件是經常使用的控件,下面這篇文章主要給大家介紹了關于Android中EditText長按菜單中分享功能的隱藏方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-02-02
Android Studio 利用Splash制作APP啟動界面的方法
這篇文章主要介紹了Android Studio 利用Splash制作APP啟動界面,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android實戰(zhàn)教程第七篇之如何在內存中存儲用戶名和密碼
這篇文章主要為大家詳細介紹了Android如何實現(xiàn)在內存中存儲用戶名和密碼的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
android自定義popupwindow仿微信右上角彈出菜單效果
這篇文章主要為大家詳細介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

