Android popwindow和fragment结合 左侧弹出下拉菜单 切换界面

      延续上一篇文章Android 实现对话框圆角功能 ,在项目推进的过程当中,之前是已经用popwindow实现了点击按钮,在按钮下方弹出下拉菜单,实现了类似微信右上角加好友的功能,有兴趣的朋友,可以下载这个资源。回归主题,之前popwindow的使用,是固定在了登陆刚进去的界面,假设现在点击了左侧菜单的其他按钮,这就要求标题下方的内容必须更新所要显示的内容,一开始想都没想,就用了如下代码进行跳转:

      

Intent intent = new Intent(Intent.ACTION_EDIT, null);
startActivity(intent);
       这样做的确是能跳转到另一个显示界面,但所有的xml文件、下拉popwindow菜单,都得重新在activity重复使用,关键是跳转到这个界面,如果点击下拉菜单的其他按钮,这个时候又得重新写

 
Intent intent = new Intent(Intent.ACTION_EDIT, null);
 startActivity(intent);
    陷入了一个死循环,假设有10个菜单项,我就得写相同的代码10次,而且intent跳来跳去的,代码太乱,无法进行管理,非常难以忍受。所以就想着android应该有提供这样的类可以保持左侧不变,或者其他部分可以动态更新,很高兴找到了actvitygroup这个类,下载的demo运行之后,的确是能解决,但已经不推荐使用这个类,所以就决定使用fragment来进行界面的动态切换。下面是工程代码:
 

     1.主界面文件,里面存放着一个FrameLayout,可以用你想呈现的界面进行更换,也就是fragment,类似于单独的activity。

<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"
    tools:context=".MainActivity" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@color/menu_background"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/popBtn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/image2"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="测试"
            android:textColor="@color/white"
            android:textSize="30dp" />

        <Button
            android:id="@+id/menu_person"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="8dp"
            android:background="@drawable/image1" />
    </RelativeLayout>

    <span style="font-size:18px;color:#ff0000;background-color: rgb(255, 255, 102);"><strong>//这是关键点,用来动态替换frament,更换界面</strong></span>
    <FrameLayout 
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/background"
        />
</LinearLayout>

           2.定义好了主界面,就得编写你要替换的界面xml文件,就是不同的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical" >
    <!-- 用戶狀態欄 -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="测试"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/price_trademargin"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="测试"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/price_floatpl"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="12dp" />
    </LinearLayout>

</LinearLayout>
       第二个fragment2的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="vertical">

    <!-- 用戶狀態欄 -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/trademargin"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/price_trademargin"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/floatpl"
            android:textColor="@color/white"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/price_floatpl"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="@dimen/activity_price_inst_width"
            android:layout_height="@dimen/price_table_comm_height"
            android:background="@drawable/tableheader"
            android:gravity="center"
            android:text="@string/price_table_ccy"
            android:textColor="@color/price_tableheader_forcolor"
            android:textSize="@dimen/price_table_header_font_size" />

        <TextView
            android:layout_width="@dimen/activity_price_chart_width"
            android:layout_height="@dimen/price_table_comm_height"
            android:background="@drawable/tableheader"
            android:gravity="center"
            android:text=""
            android:textColor="@color/white" />

        <com.android.fragmentnormal.AlloneHorizontalScrollView
            android:id="@+id/HorizontalScrollView_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" >
        </com.android.fragmentnormal.AlloneHorizontalScrollView>
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="@color/background" >

            <TableLayout
                android:id="@+id/left_table"
                android:layout_width="@dimen/price_left_table_width"
                android:layout_height="fill_parent"
                android:background="@color/background"
                android:orientation="vertical" >
            </TableLayout>

            <com.android.fragmentnormal.AlloneHorizontalScrollView
                android:id="@+id/HorizontalScrollView_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/left_table"
                android:background="@color/background" >

                <TableLayout
                    android:id="@+id/data_table"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@color/background"
                    android:orientation="vertical" >
                </TableLayout>
            </com.android.fragmentnormal.AlloneHorizontalScrollView>
        </RelativeLayout>
    </ScrollView>

</LinearLayout>

      3. 编写主类,对你的两个fragment进行管理,决定一开始显示哪个,点击按钮之后切换显示哪个fragment。

package com.android.fragmentnormal;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends FragmentActivity  {
	FragmentManager manager ;
	Fragment f1,f2,f3 ;
	Button pop;
	private PopupWindow popupWindow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		manager= getSupportFragmentManager() ;
		FragmentTransaction transaction = manager.beginTransaction() ;
		f1 = new Fragment1();
		f2 = new Fragment2();
		f3 = new Fragment3();

		pop = (Button) findViewById(R.id.popBtn);
		pop.setOnClickListener(popClick);

		transaction.add(R.id.fragment_container, f2);
		transaction.commit();
	}

	// 点击弹出左侧菜单的显示方式
	OnClickListener popClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			/*Toast toast = Toast.makeText(MainActivity.this, "这是一个代图片的Toast!", Toast.LENGTH_LONG);
			toast.show();*/

			getPopupWindow();

			// 这里是位置显示方式,在按钮的左下角
			popupWindow.showAsDropDown(v);
		}
	};


	/**
	 * 创建PopupWindow
	 */
	protected void initPopuptWindow() {
		// 获取自定义布局文件pop.xml的视图
		View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null,
				false);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		// 创建PopupWindow实例,200,150分别是宽度和高度
		popupWindow = new PopupWindow(popupWindow_view, 350,
				ViewGroup.LayoutParams.MATCH_PARENT, true);
		// popupWindow.setWidth(350);
		// popupWindow.setHeight(dm.heightPixels * 20 / 2);
		// 设置动画效果
		popupWindow.setAnimationStyle(R.style.AnimationFade);
		// 点击其他地方消失
		popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
					popupWindow = null;
				}
				return false;
			}
		});
		// pop.xml视图里面的控件
		initOpenMenuItem(popupWindow_view);
		initOpenMenuOther(popupWindow_view);
		initOpenPosition(popupWindow_view);

	}
	/*
     *  2015年7月13日17:35:24,
     *  author:qiulinhe
     *  添加对于开仓单的监听和界面增加
     */
	private void initOpenPosition(View popupWindow_view) {
		DrawableCenterTextView menu_open = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_open);
		// pop.xml视图里面的控件触发的事件

		menu_open.setOnClickListener(new OnClickListener() {
			<span style="font-size:18px;color:#ff0000;"><strong>FragmentTransaction transaction ;//对fragment进行跳转控制。</strong></span>
			@Override
			public void onClick(View v) {
				<strong><span style="font-size:18px;color:#ff0000;">transaction = manager.beginTransaction();
				transaction.replace(R.id.fragment_container, f1);//把f1的界面替换container
				transaction.commit();
				popupWindow.dismiss();</span></strong>
			}
		});

	}

        <strong><span style="font-size:18px;color:#33cc00;">//初始化左侧下拉菜单的按钮</span></strong>
	private void initOpenMenuOther(View popupWindow_view) {
		DrawableCenterTextView menu_open = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_open);
		DrawableCenterTextView menu_order = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_order);
		DrawableCenterTextView menu_orderhis = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_orderhis);
		DrawableCenterTextView menu_closehis = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_closehis);
		DrawableCenterTextView menu_summary = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_summary);
		DrawableCenterTextView menu_pricewarning = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_pricewarning);
		DrawableCenterTextView menu_news = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_news);
		DrawableCenterTextView menu_margin = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_margin);
		DrawableCenterTextView menu_message = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_message);
		DrawableCenterTextView menu_syssett = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_syssett);
		DrawableCenterTextView menu_about = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_about);
	}

	private void initOpenMenuItem(View popupWindow_view) {
		DrawableCenterTextView menu_price = (DrawableCenterTextView) popupWindow_view
				.findViewById(R.id.menu_price);
		// 打开
		menu_price.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
	}

	/***
	 * 获取PopupWindow实例
	 */
	private void getPopupWindow() {

		if (null != popupWindow) {
			popupWindow.dismiss();
			return;
		} else {
			initPopuptWindow();
		}
	}

}
        4.frament1的代码如下:

package com.android.fragmentnormal;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Toast;

public class Fragment1 extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		<span style="color:#ff0000;"><strong>View vi = inflater.inflate(R.layout.activity_openposition, container,false);</strong></span>



		//实现下方长按弹出listview,2015年7月14日10:01:19
		ActionSlideExpandableListView list = (ActionSlideExpandableListView)vi.findViewById(R.id.list);


		list.setAdapter(buildDummyData());

		list.setItemActionListener(
				new ActionSlideExpandableListView.OnActionClickListener() {

					@Override
					public void onClick(View listView, View buttonview,
										int position) {
						String actionName = "";
						if (buttonview.getId() == R.id.duichong) {
							actionName = "duichong";
						}
						Toast.makeText(
								getActivity(),
								"你点击了对冲按钮",
								Toast.LENGTH_SHORT).show();
					}

				}, R.id.duichong);
		return vi;
	}


	/**
	 * qiulinhe
	 * 2015年7月14日10:02:03
	 * 实现开仓单下方长按,弹出两个按钮功能
	 */
	public ListAdapter buildDummyData() {
		final int SIZE = 20;
		String[] values = new String[SIZE];
		for (int i = 0; i < SIZE; i++) {
			values[i] = "單號";
		}
		return new ArrayAdapter<String>(getActivity(), R.layout.expandable_list_item,
				R.id.text, values);
	}
}

   5.fragment2的代码如下:

package com.android.fragmentnormal;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.frament2, container,false);
	}
}


     代码基本完毕,如果想要demo可以到这个链接:csdn资源可以下载点击打开链接,其实原理很简单,就是通过fragment来管理切换界面。

运行界面如下:





     新增另外一个问题:这个弹出popwindow,点击任何地方都会让弹窗消失,如何使其重新点击菜单,才能消失呢,实现方法如下:

   1.首先设置整个屏幕的背景。

   2.捕获点击事件

  代码如下:


popupWindow.setOnDismissListener(new OnDismissListener() {
<span style="white-space:pre">		</span>@Override
<span style="white-space:pre">		</span>public void onDismiss() {
<span style="white-space:pre">			</span>backgroundAlpha(1f);
<span style="white-space:pre">		</span>  }
<span style="white-space:pre">		</span>});
// 设置屏幕背景
	private void backgroundAlpha(float alpha) {
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.alpha = alpha; // 0.0-1.0
		getWindow().setAttributes(lp);
	}

// 点击其他地方消失
		popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popupWindow != null && popupWindow.isShowing()
						&& <span style="font-size:24px;color:#ff0000;">event.getAction() == MotionEvent.ACTION_OUTSIDE</span>) {
					popupWindow.dismiss();
					popupWindow = null;
				}
				return false;
			}
		});





  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值