fragment

Android Jetpack -- Navigation 篇

╄→гoц情女王★ 提交于 2020-01-19 14:10:46
Navigation是指允许用户在应用程序中浏览、进入和退出不同内容的Fragment 。Navigation能够实现从简单的按钮单击到更复杂的模式,如应用程序栏和导航抽屉。导航组件还通过遵循一套既定的原则来确保一致和可预测的用户体验。 Navigation的原则: 固定的起始位置:除去登陆等一次性界面,用户在启动App最先看到的界面需要为一个固定的起始界面。 Navigation的表现形式为堆栈形式:Navigation类似于栈,栈顶为用户所看到的界面,界面的切换总是在栈顶进行,导航到目标后,目标位于栈顶。 标题栏的回退应该和返回键的功能一样,但是标题栏不能退出应用:这个也很容易理解,因为两个的定义就存在本质的区别。 Deep Link:比如从浏览器跳转到其他App时,用户首先看到的应该还是浏览器的那个页面,没有其他界面的跳转过程。 Navigation有三个主要的部分: Navigation graph:就是Navigatation的xml文件,包含所有的需要跳转的目标 NavHost:一个容器,用于显示 NavController:控制跳转流程 具体实现(简单例子) 首先新建新建两个Fragment,HomeFragment和DetailFragment 在其对应生成的xml文件中添加一些控件 frament_home.xml 1 <?xml version="1.0"

FragmentPagerAdapter 刷新数据

旧时模样 提交于 2020-01-18 02:03:14
public class HomeFragmentPagerAdapter extends FragmentPagerAdapter { private final FragmentManager fm; public List<Fragment> fragmentList = new ArrayList<>(); public HomeFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.fm = fm; this.fragmentList = fragmentList; } @Override public Fragment getItem(int arg0) { return fragmentList.get(arg0); } @Override public int getCount() { return fragmentList.size(); } public void refresh(List<Fragment> fragmentList){ if (this.fragmentList!=null){ FragmentTransaction ft = fm.beginTransaction(); for (Fragment fragment :

Calling a method in a fragment from a separate class

做~自己de王妃 提交于 2020-01-17 02:41:24
问题 What's the best way to call a method inside a fragment from a different class? I passed the context via getActivity() into the class. Something along the lines of ((Fragment) ((Activity) context).getMainFragment()).Method(); This just doesn't look right.... 回答1: I think you can do something like this: Create interface OnMyDialogClickListener and class MyDialogFragment , which will call methods of created interface public class MyDialogFragment extends DialogFragment { private

CropActivity is not starting in onActivityResult inside Fragment

青春壹個敷衍的年華 提交于 2020-01-16 20:56:08
问题 I am calling an intent to select an image and later to crop the image into aspect ratio(1,1), but when i run the app the gallery is opening but when i select the image it closes and gets back to the Fragment. Below is the code of my Button's on click listener mImageBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent galleryIntent = new Intent(); galleryIntent.setType("image/*"); galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

Problem putting a fragment into another fragment

女生的网名这么多〃 提交于 2020-01-16 19:11:29
问题 I Tried putting a Fragment into a FrameLayout inside another Fragment via FragmentManager and FragmentTransaction (from android.support.v4.app). The container fragment has a button and a TextView on top and a FrameLayout at the bottom (I create the layout programmatically and i don't want to hurt your eyes with all of that). The CreateView() works just fine and i cann access the FrameLayout at the bottom of the container and add or remove View dynamically as I please via @Override public void

Fragment传递参数

不问归期 提交于 2020-01-16 07:15:20
实例化自定义Fragment时,为什么官方推荐Fragment.setArguments(Bundle bundle)这种方式来传递参数,而不推荐通过构造方法直接来传递参数呢。 在使用构造函数时候遇到这样错误提示:Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static 所以这边就记录下使用Fragment.setArguments(Bundle bundle)这种方式来传递参数的用法 创建newInstance方法 public static final BeautyOrderItemFragment newInstance(int orderId, String title) { BeautyOrderItemFragment fragment = new BeautyOrderItemFragment(); Bundle bundle = new Bundle(); bundle.putInt("orderId",orderId); bundle.putString("title", title); fragment.setArguments(bundle); return fragment ; }

Android: Not sure why my listview is getting all jumbled up?

一曲冷凌霜 提交于 2020-01-14 03:23:22
问题 I implemented a ListView that worked correctly until I added more than 5 items and 2 headers. Im not entirely sure why some items are not appearing and others are appearing multiple times. Any assistance in fixing this will be much appreciated. Code is include below. Toolbox.java public class Toolbox extends Fragment { private ListView lstView; private View rootView; List<Tools> tools; public static Toolbox newInstance(Context context) { Toolbox fragment = new Toolbox(); return fragment; }

Android组件化探索与实践

我们两清 提交于 2020-01-14 02:36:06
什么是组件化 不用去纠结组件和模块语义上的区别,如果模块间不存在强依赖且模块间可以任意组合,我们就说这些模块是组件化的。 组件化的好处 实现组件化本身就是一个解耦的过程,同时也在不断对你的项目代码进行提炼。对于已有的老项目,实现组件化刚开始是很难受的,但是一旦组件的框架初步完成,对于后期开发效率是会有很大提升的。 组件间间相互独立,可以减少团队间的沟通成本。 每一个组件的代码量不会特别巨大,团队的新人也能快速接手项目。 如何实现组件化 这是本文所主要讲述的内容,本篇文章同时适用于新老项目,文中会逐渐带领大家实现如下目标: 各个组件不存在强依赖 组件间支持通信 缺少某些组件不能对项目主体产生破坏性影响 组件化-理论篇 理论篇不会讲述实际项目,先从技术上实现上面的三个目标。 组件间不存在强依赖 组件间不存在强依赖从理论上来说其实很简单,我不引用你任何东西,你也不要引用我任何东西就行了。但在实际项目中,需要清楚明白那些业务模块应该定义为组件,另外在已有项目中,拆分代码也需要大量的工作。 组件间如何通信 组件间通过接口通信。为每一个组件定义一个或者多个接口,简单起见,我们假定只为每一个组件定义接口(多个接口是类似的)。 便于理解,还是要举实例。假设当前存在两个组件UserManagement(用户管理)和OrderCenter(订单中心)

Assign data to activity variable from fragment, and get variable in another fragment

£可爱£侵袭症+ 提交于 2020-01-13 19:23:07
问题 I have an android application. Which have one activity with two fragments. When my application starts it assigns data to a variable in his activity. Then on the click of a button second fragment opens and it takes data from his activity which is assigned in activity. But it returns null. What I have tried so far is... I made variable in my activity class and assign data from my fragment like this. ((MainActivity)getActivity()).resultObj = gson.fromJson(HomeCardString.toString(),