Skip to content

Commit

Permalink
增加XPageActivity构建方法,方便自定义Fragment的容器
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed May 30, 2018
1 parent 5b2bf9c commit 8d472c0
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ dependencies {
implementation deps.butterknife.runtime
annotationProcessor deps.butterknife.compiler

// implementation project(':xpage-lib')
// annotationProcessor project(':xpage-compiler')
implementation project(':xpage-lib')
annotationProcessor project(':xpage-compiler')

implementation 'com.github.xuexiangjys.XPage:xpage-lib:2.2.0'
annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:2.2.0'
// implementation 'com.github.xuexiangjys.XPage:xpage-lib:2.2.0'
// annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:2.2.0'

implementation 'com.github.xuexiangjys.XUtil:xutil-core:1.1.2'

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ComplexActivity"/>
</application>

</manifest>
56 changes: 56 additions & 0 deletions app/src/main/java/com/xuexiang/xpagedemo/ComplexActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.xuexiang.xpagedemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.xuexiang.xpage.base.XPageActivity;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xpagedemo.fragment.DateReceiveFragment;
import com.xuexiang.xpagedemo.fragment.TestFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
* @author xuexiang
* @since 2018/5/30 下午5:41
*/
public class ComplexActivity extends XPageActivity {

@BindView(R.id.btn_1)
Button btn1;
@BindView(R.id.btn_2)
Button btn2;

@Override
protected int getLayoutId() {
return R.layout.activity_complex;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
openPage(TestFragment.PAGE_NAME, null, CoreAnim.none);
}


@OnClick({R.id.btn_1, R.id.btn_2})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_1:
changePage(TestFragment.PAGE_NAME, null, CoreAnim.none);
break;
case R.id.btn_2:
Bundle params = new Bundle();
params.putBoolean(DateReceiveFragment.KEY_IS_NEED_BACK, false);
int id = (int) (Math.random() * 100);
params.putString(DateReceiveFragment.KEY_EVENT_NAME, "事件" + id);
params.putString(DateReceiveFragment.KEY_EVENT_DATA, "事件" + id + "携带的数据");
changePage("数据接收", params, CoreAnim.none);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.xuexiang.xpagedemo.fragment;

import android.content.Intent;

import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xpagedemo.ComplexActivity;

import java.util.List;

/**
* @author xuexiang
* @since 2018/5/30 下午6:11
*/
@Page(name = "复杂使用")
public class ComplexPageFragment extends XPageSimpleListFragment {
/**
* 初始化例子
*
* @param lists
* @return
*/
@Override
protected List<String> initSimpleData(List<String> lists) {
lists.add("复杂页面使用");
return lists;
}

/**
* 条目点击
*
* @param position
*/
@Override
protected void onItemClick(int position) {
switch(position) {
case 0:
startActivity(new Intent(getContext(), ComplexActivity.class));
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class MainFragment extends XPageContainerListFragment {
protected Class[] getPagesClasses() {
return new Class[]{
DataTransmitFragment.class,
AnimationFragment.class
AnimationFragment.class,
ComplexPageFragment.class
};
}

Expand Down
60 changes: 60 additions & 0 deletions app/src/main/res/layout/activity_complex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启动A界面" />

<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启动B界面" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"/>

<FrameLayout
android:id="@id/fragment_container"
android:layout_width="match_parent"
android:layout_height="400dp">

</FrameLayout>

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这里是底部"
android:textSize="18sp" />

</LinearLayout>

</LinearLayout>
63 changes: 61 additions & 2 deletions xpage-lib/src/main/java/com/xuexiang/xpage/base/XPageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,42 @@ public Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim) {
return openPage(page);
}

/**
* 切换fragment[直接替换,不增加到返回堆栈]
*
* @param pageName 页面名
* @return 切换的fragment对象
*/
public Fragment changePage(String pageName) {
CoreSwitchBean page = new CoreSwitchBean(pageName, null, CoreAnim.none).setAddToBackStack(false);
return openPage(page);
}

/**
* 切换fragment[直接替换,不增加到返回堆栈]
*
* @param pageName 页面名
* @param bundle 参数
* @return 切换的fragment对象
*/
public Fragment changePage(String pageName, Bundle bundle) {
CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, CoreAnim.none).setAddToBackStack(false);
return openPage(page);
}

/**
* 切换fragment[直接替换,不增加到返回堆栈]
*
* @param pageName 页面名
* @param bundle 参数
* @param coreAnim 动画
* @return 切换的fragment对象
*/
public Fragment changePage(String pageName, Bundle bundle, CoreAnim coreAnim) {
CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim).setAddToBackStack(false);
return openPage(page);
}

/**
* 打开fragment
*
Expand Down Expand Up @@ -636,7 +672,8 @@ public Fragment openPage(String pageName, Bundle bundle, int[] anim) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getBaseLayout());
setContentView();

//处理新开activity的情况
Intent newIntent = getIntent();
if (savedInstanceState != null) {
Expand Down Expand Up @@ -669,6 +706,7 @@ protected void onCreate(Bundle savedInstanceState) {

/**
* 获取是否将activity添加到堆栈中
*
* @return {@code true} :添加<br> {@code false} : 不添加
*/
protected boolean getIsAddActivityToStack() {
Expand All @@ -677,18 +715,39 @@ protected boolean getIsAddActivityToStack() {

/**
* 获取是否注册页面结束的广播
*
* @return {@code true} :注册<br> {@code false} : 不注册
*/
protected boolean getIsRegisterExitBroadcast() {
return true;
}


/**
* 设置根布局
*/
protected void setContentView() {
int layoutId = getLayoutId();
if (layoutId != -1) {
setContentView(layoutId);
} else {
setContentView(getBaseLayout());
}
}

/**
* @return 获取布局的id
*/
protected int getLayoutId() {
return -1;
}

/**
* 设置根布局
*
* @return
*/
protected FrameLayout getBaseLayout() {
protected View getBaseLayout() {
FrameLayout baseLayout = new FrameLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
baseLayout.setId(R.id.fragment_container);
Expand Down

0 comments on commit 8d472c0

Please sign in to comment.