国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android實現左右側滑菜單效果

2019-12-12 01:55:58
字體:
來源:轉載
供稿:網友

在android開發中,左右側滑菜單的開發已成為我們現在開發的必備技術之一,再次之前,我沒有做過相類似的demo,但是項目的開發有要求有這樣的效果,而且大家都知道,雖然網上由開源的代碼,但是不僅種類多,看著一個頭兩個大,而且代碼不好分離。因此我們無法簡化成自己的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、靜態和動態Fragment的使用 * 靜態 直接在布局中使用<fragment /> * 動態 使用管理器 得到一個事務 然后使用事務調用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 把數據添加進去 不會覆蓋之前的內容  */ dl.addDrawerListener(new DrawerListener() {  //滑動狀態發生改變的時候 會調用該方法  @Override  public void onDrawerStateChanged(int arg0) {  // TODO Auto-generated method stub  Log.i("===============================", "StateChanged" + arg0);  }  //監聽滑動過程中 邊界的位置  @Override  public void onDrawerSlide(View arg0, float arg1) {  // TODO Auto-generated method stub  Log.i("===============================", "DrawerSlide" + arg1);  }  //監聽側拉是否完全展開  @Override  public void onDrawerOpened(View arg0) {  // TODO Auto-generated method stub  Log.i("===============================", "DrawerOpened");  }  //監聽側拉是否被關閉  @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() { //動態加載Fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //參數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側滑效果匯總》學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙泉市| 栾城县| 福海县| 仙游县| 碌曲县| 堆龙德庆县| 洛浦县| 城步| 吉隆县| 筠连县| 高平市| 中卫市| 塔城市| 玛多县| 石林| 紫金县| 新泰市| 广水市| 寻甸| 株洲市| 宜良县| 韩城市| 靖远县| 根河市| 中阳县| 博野县| 浑源县| 庄浪县| 修水县| 商都县| 泸溪县| 黑山县| 兴文县| 商南县| 桦甸市| 宁德市| 惠安县| 九江县| 通河县| 丰顺县| 连城县|