Commit 1882698c by gao.chao

activity与Fragment交互

parent cd862ad3
......@@ -43,6 +43,8 @@
<activity android:name=".QRActivity" />
<activity android:name=".TitleActivity" />
<activity android:name=".DownLoadAPKActivity"/>
<activity android:name=".FragmentInteractiveActivity"/>
<activity android:name=".FragmentInteractiveActivity2"/>
</application>
<!-- 配置APP ID -->
......
package com.mayi.demo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import com.mayi.fastdevelop.base.BaseFragment;
import com.mayi.fastdevelop.base.BaseFragmentActivity;
import com.mayi.fastdevelop.comnon.AppCallback;
import java.util.ArrayList;
import java.util.List;
public class FragmentInteractiveActivity extends BaseFragmentActivity {
private TextView tv;
private ViewPager viewPager;
private List<BaseFragment> list = new ArrayList<>();
private Handler handler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_interactive);
handler = new Handler(Looper.getMainLooper());
tv = findViewById(R.id.tv);
viewPager = findViewById(R.id.viewpager);
list.add(getFragment("0"));
list.add(getFragment("1"));
list.add(getFragment("2"));
list.add(getFragment("3"));
viewPagerLoadFragments(list, viewPager);
}
@Override
public void handleInfo(final String data, final AppCallback<String> callback) {
tv.setText(data);
showToast(data);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.callBack(getClass().getName() + ":已处理数据-->" + data);
}
}
}, 1000);
}
public BaseFragment getFragment(String tag) {
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("tag", tag);
fragment.setArguments(bundle);
return fragment;
}
}
package com.mayi.demo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import com.mayi.fastdevelop.base.BaseFragment;
import com.mayi.fastdevelop.base.BaseFragmentActivity;
import com.mayi.fastdevelop.comnon.AppCallback;
import com.mayi.fastdevelop.view.OnMultiClickListener;
import java.util.ArrayList;
import java.util.List;
public class FragmentInteractiveActivity2 extends BaseFragmentActivity {
private TextView tv;
private List<BaseFragment> list = new ArrayList<>();
private Handler handler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_interactive_2);
findViewById(R.id.b1).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
showFragment(0);
}
});
findViewById(R.id.b2).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
showFragment(1);
}
});
findViewById(R.id.b3).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
showFragment(21);
}
});
handler = new Handler(Looper.getMainLooper());
tv = findViewById(R.id.tv);
list.add(getFragment("0"));
list.add(getFragment("1"));
list.add(getFragment("2"));
addFragmentsAndShowFragment(list, R.id.layout);
}
@Override
public void handleInfo(final String data, final AppCallback<String> callback) {
tv.setText(data);
showToast(data);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.callBack(getClass().getName() + ":已处理数据-->" + data);
}
}
}, 1000);
}
public BaseFragment getFragment(String tag) {
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("tag", tag);
fragment.setArguments(bundle);
return fragment;
}
}
......@@ -31,6 +31,18 @@ public class FunctionActivity extends BaseActivity{
gotoActivity(DownLoadAPKActivity.class);
}
});
findViewById(R.id.b4).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
gotoActivity(FragmentInteractiveActivity.class);
}
});
findViewById(R.id.b5).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
gotoActivity(FragmentInteractiveActivity2.class);
}
});
}
......
package com.mayi.demo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mayi.fastdevelop.base.BaseFragment;
import com.mayi.fastdevelop.comnon.AppCallback;
import com.mayi.fastdevelop.comnon.Constant;
import com.mayi.fastdevelop.view.OnMultiClickListener;
public class MyFragment extends BaseFragment {
private String fragmentTag;
private Handler handler;
@Override
public String getFragmentTag() {
return fragmentTag;
}
@Override
public void receiveAndHandleInfo(final String data, final AppCallback<String> callback) {
showToast("我是" + getFragmentTag() + "接收到-->" + data);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.callBack(getClass().getName() + ":已处理数据-->" + data);
}
}
}, 1000);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
fragmentTag = getArguments().getString("tag");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
TextView tv=view.findViewById(R.id.tv);
tv.setText("我是"+getFragmentTag());
view.findViewById(R.id.b1).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
sendInfo("0", "发送给第一个Fragment", new AppCallback<String>() {
@Override
public void callBack(String s) {
showToast(getFragmentTag() + "接收到处理的数据:" + s);
}
});
}
});
view.findViewById(R.id.b2).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
sendInfo("1", "发送给第2个Fragment,不需要返回数据", null);
}
});
view.findViewById(R.id.b3).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
sendInfo(Constant.ACTIVITY_TAG, "发送给Activity,需要返回数据", new AppCallback<String>() {
@Override
public void callBack(String s) {
showToast(getFragmentTag() + "接收到处理的数据:" + s);
}
});
}
});
view.findViewById(R.id.b4).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
sendInfo(Constant.ACTIVITY_TAG, "发送给Activity,不需要返回数据", null);
}
});
return view;
}
}
<?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">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_40" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
\ No newline at end of file
<?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">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_40" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1个fragment" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2个fragment" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3个fragment" />
</LinearLayout>
<FrameLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
\ No newline at end of file
......@@ -26,6 +26,18 @@
android:layout_height="wrap_content"
android:text="下载安装APP" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity与Fragment之间交互\n可以左右滑动" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity与Fragment之间交互\n不可以左右滑动" />
</LinearLayout>
......
<?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">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/im_dp_20"/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送给第一个Fragment,需要回调" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送给第一个Fragment,,不需要回调" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送给Activity,,需要回调" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送给Activity,,不需要回调" />
</LinearLayout>
\ No newline at end of file
......@@ -13,12 +13,6 @@ public abstract class BaseActivity extends AppCompatActivity {
private Dialog dialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showLoadingDialog() {
if (dialog == null) {
dialog = LoadingDialog.init(this).show();
......
package com.mayi.fastdevelop.base;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public abstract class BaseFragment extends Fragment{
import com.mayi.fastdevelop.comnon.AppCallback;
import com.mayi.fastdevelop.util.ToastUtil;
import com.mayi.fastdevelop.view.LoadingDialog;
public abstract class BaseFragment extends Fragment {
private Dialog dialog;
/**
* Fragment加载到Acrtivity中的Tag
*
* @return
*/
public abstract String getFragmentTag();
/**
* 接收并处理数据
*
* @param data
* @param callback
*/
public abstract void receiveAndHandleInfo(String data, AppCallback<String> callback);
/**
* 发送消息给Acticity/Fragment
*
* @param tag 发送的目的地
* @param data 发送的数据
*/
public void sendInfo(String tag, String data, AppCallback<String> callback) {
FragmentActivity activity = getActivity();
if (activity instanceof BaseFragmentActivity) {
BaseFragmentActivity fragmentActivity = (BaseFragmentActivity) activity;
fragmentActivity.receiveInfo(tag, data, callback);
} else {
throw new RuntimeException("Activity必须继承BaseFragmentActivity");
}
}
public void showLoadingDialog() {
if (dialog == null) {
dialog = LoadingDialog.init(getContext()).show();
} else {
dialog.show();
}
}
public void dismssLoadingDialog() {
if (dialog != null) {
dialog.dismiss();
}
}
/**
* 跳转页面并结束当前页面
*
* @param activity
*/
public void gotoActivityAndFinish(Class activity) {
Intent intent = new Intent(getContext(), activity);
startActivity(intent);
Activity a = getActivity();
if (a != null) {
a.finish();
}
}
/**
* 跳转页面
*
* @param activity
*/
public void gotoActivity(Class activity) {
Intent intent = new Intent(getContext(), activity);
startActivity(intent);
}
/**
* 跳转页面并结束当前页面
*
* @param activity
*/
public void gotoActivityAndFinish(Class activity, Bundle bundle) {
Intent intent = new Intent(getContext(), activity);
intent.putExtras(bundle);
startActivity(intent);
Activity a = getActivity();
if (a != null) {
a.finish();
}
}
/**
* 跳转页面
*
* @param activity
*/
public void gotoActivity(Class activity, Bundle bundle) {
Intent intent = new Intent(getContext(), activity);
intent.putExtras(bundle);
startActivity(intent);
}
public void showToast(String msg) {
ToastUtil.show(getContext(), msg);
}
@Override
public void onDestroyView() {
dismssLoadingDialog();
super.onDestroyView();
}
}
package com.mayi.fastdevelop.base;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import com.mayi.fastdevelop.comnon.AppCallback;
import com.mayi.fastdevelop.comnon.Constant;
import com.mayi.fastdevelop.util.ToastUtil;
import com.mayi.fastdevelop.view.LoadingDialog;
import java.util.List;
public abstract class BaseFragmentActivity extends FragmentActivity {
private List<BaseFragment> list;//list不能为null,否则无法传递数据,传递数据是通过list找到Fragment的
/**
* 数量Fragment发送过来的数据
*
* @param data
* @param callback
*/
public abstract void handleInfo(String data, AppCallback<String> callback);
public void setList(List<BaseFragment> list) {
this.list = list;
}
public void viewPagerLoadFragments(final List<BaseFragment> list, ViewPager viewPager) {
this.list = list;
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
}
/**
* 把Fragment添加到页面中并显示第一个Fragment
*
* @param list
*/
public void addFragmentsAndShowFragment(List<BaseFragment> list, int viewId) {
this.list = list;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
BaseFragment fragment = list.get(i);
transaction.add(viewId, fragment).hide(fragment);
}
transaction.show(list.get(0));
transaction.commit();
}
}
/**
* 显示选中的Fragment
*
* @param index
*/
public void showFragment(int index) {
if (list != null && list.size() > 0 && index < list.size()) {
BaseFragment baseFragment = list.get(index);
if (!baseFragment.isHidden() && baseFragment.isVisible()) {
return;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
for (int i = 0; i < list.size(); i++) {
BaseFragment fragment = list.get(i);
transaction.hide(fragment);
}
transaction.show(baseFragment).commit();
}
}
/**
* 接收Fragment发送过来的数据,如果tag=Activity,在Activity中处理数据
*
* @param tag
* @param data
* @param callback
*/
public void receiveInfo(String tag, String data, AppCallback<String> callback) {
if (TextUtils.equals(Constant.ACTIVITY_TAG, tag)) {
handleInfo(data, callback);
} else {
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
BaseFragment fragment = list.get(i);
if (TextUtils.equals(tag, fragment.getFragmentTag())) {
fragment.receiveAndHandleInfo(data, callback);
}
}
}
}
}
private Dialog dialog;
public void showLoadingDialog() {
if (dialog == null) {
dialog = LoadingDialog.init(this).show();
} else {
dialog.show();
}
}
public void dismssLoadingDialog() {
if (dialog != null) {
dialog.dismiss();
}
}
/**
* 跳转页面并结束当前页面
*
* @param activity
*/
public void gotoActivityAndFinish(Class activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
finish();
}
/**
* 跳转页面
*
* @param activity
*/
public void gotoActivity(Class activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
}
/**
* 跳转页面并结束当前页面
*
* @param activity
*/
public void gotoActivityAndFinish(Class activity, Bundle bundle) {
Intent intent = new Intent(this, activity);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
/**
* 跳转页面
*
* @param activity
*/
public void gotoActivity(Class activity, Bundle bundle) {
Intent intent = new Intent(this, activity);
intent.putExtras(bundle);
startActivity(intent);
}
public void showToast(String msg) {
ToastUtil.show(this, msg);
}
@Override
protected void onDestroy() {
dismssLoadingDialog();
super.onDestroy();
}
}
......@@ -13,8 +13,8 @@ import android.support.annotation.Nullable;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.bean.AppConfig;
import com.mayi.fastdevelop.constant.Key;
import com.mayi.fastdevelop.constant.RequestCode;
import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.comnon.RequestCode;
import com.mayi.fastdevelop.util.DownLoadUtil;
import com.mayi.fastdevelop.util.PermissionUtil;
import com.mayi.fastdevelop.util.SpUtil;
......
package com.mayi.fastdevelop.comnon;
public interface AppCallback<T> {
public void callBack(T t);
}
package com.mayi.fastdevelop.comnon;
public class Constant {
public static final String ACTIVITY_TAG="Activity";
}
package com.mayi.fastdevelop.constant;
package com.mayi.fastdevelop.comnon;
public class Key {
public static final String ANT_DOWNLOAD_ID = "ant_download_id";
......
package com.mayi.fastdevelop.constant;
package com.mayi.fastdevelop.comnon;
public class RequestCode {
public static final int REQUEST_CODE_APP_INSTALL = 0x00;
......
......@@ -9,7 +9,7 @@ import android.os.Environment;
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import com.mayi.fastdevelop.constant.Key;
import com.mayi.fastdevelop.comnon.Key;
/**
......
......@@ -27,8 +27,11 @@ public class ToastUtil {
if (sToast == null) {
sToast = Toast.makeText(context, msg, duration);
} else {
sToast.setDuration(duration);
sToast.setText(msg);
/**
*适配8.0及以上版本,8.0不会更新Toast中的文字,要创建一个新的Toast
*/
sToast.cancel();
sToast = Toast.makeText(context, msg, duration);
}
sToast.show();
return sToast;
......
......@@ -3,25 +3,16 @@ package com.mayi.fastdevelop.view.installapk;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.bean.AppConfig;
import com.mayi.fastdevelop.constant.Key;
import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.util.ActivitysManager;
import com.mayi.fastdevelop.util.DownLoadUtil;
import com.mayi.fastdevelop.util.PermissionUtil;
......@@ -29,8 +20,6 @@ import com.mayi.fastdevelop.util.SpUtil;
import com.mayi.fastdevelop.util.ToastUtil;
import com.mayi.fastdevelop.view.dialog.BaseDialog;
import com.mayi.fastdevelop.view.dialog.DialogViewHolder;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import java.util.List;
......
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment