最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月10日 10:31:02   作者:人走丿茶涼  
這篇文章主要為大家詳細介紹了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è)拉菜單 主頁面顯示的效果 要寫在布局的最上面 首先進行加載 -->
 <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) 使用管理器 得到一個事務(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ù)添加進去 不會覆蓋之前的內(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

蒙自县| 东源县| 涿鹿县| 兖州市| 京山县| 永新县| 安图县| 宜阳县| 安徽省| 遵化市| 电白县| 翁牛特旗| 津南区| 桃园县| 宝应县| 五峰| 蛟河市| 永兴县| 涿州市| 雅江县| 利津县| 岳普湖县| 华阴市| 科尔| 凭祥市| 鄂伦春自治旗| 香港 | 荆门市| 进贤县| 衡阳市| 托里县| 当阳市| 宁阳县| 奉贤区| 突泉县| 酒泉市| 南陵县| 阿鲁科尔沁旗| 遵义县| 广宁县| 永寿县|