import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
WindowManager wm= getWindowManager();
Display d=wm.getDefaultDisplay();
if (d.getWidth()>d.getHeight())
{
Fragment1 f1 = new Fragment1();
ft.replace(android.R.id.content, f1);
}
else
{
Fragment2 f2 = new Fragment2();
ft.replace(android.R.id.content, f2);
}
ft.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图使用Android的碎片和希望显示fragment1在显示器的宽度大于显示器的高度和更大fragment2时显示的高度比显示器的宽度,但使用时更大getWidth()和getHeight()机器人工作室是说,这些方法depricated。那么如何知道什么时候宽度更大,什么时候高度更大呢?
在 ScrollView 中,我在两个不同高度的片段之间动态切换。不幸的是,这会导致跳跃。人们可以在以下动画中看到它:
当切换到黄色片段时,我希望两个按钮都保持在相同的位置。那怎么办呢?
源代码分别位于https://github.com/wondering639/stack-dynamiccontent https://github.com/wondering639/stack-dynamiccontent.git
相关代码片段:
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="800dp"
android:background="@color/colorAccent"
android:text="@string/long_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_fragment1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="show blue"
app:layout_constraintEnd_toStartOf="@+id/button_fragment2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button_fragment2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="show yellow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button_fragment1"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/button_fragment2">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
主活动.kt
package com.example.dynamiccontent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-scrollview fragmentmanager
我有一个奇怪的问题,当我用另一个片段替换它时,我的片段没有调用任何结束生命周期方法,如onPause和onStop.我像这样替换片段
public static void replaceFragment(Activity activity, int layoutId, Fragment fragment, String title, String shortTitle) {
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction().replace(layoutId, fragment);
transaction.addToBackStack(title);
transaction.setBreadCrumbTitle(title);
transaction.setBreadCrumbShortTitle(shortTitle);
transaction.commit();
activity.getFragmentManager().executePendingTransactions();
}
Run Code Online (Sandbox Code Playgroud)
我认为它是以某种方式保持我的片段活着,即使我在popBackStack之后再次替换相同的片段之后它会被显示出来然后它也会在onStart之前调用onStop吗?
背景:
我有一个主要的Activity,它包裹了一个Fragment可以改变的主要部分,并且为了保持一个后备箱,我使用FragmentManager的是后备箱.
保持活动堆栈的主要区别在于,当一个片段被推到后台并被替换时,它会调用它onDestroyView()而不是它onDestroy(),当它返回时它的视图将被重新创建onCreateView().(onCreate()但未调用碎片对象未被调用)
在活动堆栈中,它不会发生,并且视图仍然存在.
这对低端设备有积极影响,因为Android操作系统可以释放一些内存而你不必保持正确的视图(在我的应用程序中,来自服务器的消息可能随时改变视图),这样可以节省宝贵的内存带宽也是如此.
实际问题:
假设我有一个片段,用户点击某些内容并更改了视图,例如扩展了列表.
如果用户然后转到另一个屏幕(即片段),则前一个片段将被推送到后台,并且它的视图将被销毁.
当用户返回时,片段将被重新创建,并且不会"记住"用户所做的更改,例如列表不会被扩展,因为它应该
那么如何在不为每个视图制作特殊情况的情况下保存状态并恢复它?
不受欢迎的答案:
onSaveInstanceState():当片段被推送到后台时,它不会被调用,因为活动没有被破坏而且不是配置改变.android savestate android-fragments back-stack fragmentmanager
目前我有一个"RELATIVE_LAYOUT"容器,我用它来添加我的片段.我在按钮上使用OnClickListener将片段XML布局加载到RelativeLayout容器中.
我想要实现的是,当我按下按钮一次时,片段应该加载......当我再次按下它时,应该删除片段.我已经尝试使用整数来识别是否加载了片段,但是失败了.任何帮助赞赏......
码:
public class MainActivity extends Activity {
Button B1,B2;
int boolb1=0, boolb2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B1 = (Button)findViewById(R.id.btn1);
B2 = (Button)findViewById(R.id.btn2);
B1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentOne f1 = new FragmentOne();
if(boolb1==0)
{ft.add(R.id.frg1, f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
boolb1=1;}
else
{ft.remove(f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
boolb1=0;}
//ft.addToBackStack("f1");
ft.commit();
}
});
B2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); …Run Code Online (Sandbox Code Playgroud) java android android-fragments fragmenttransaction fragmentmanager
所以我在这里有这段代码,我正在创建一个新的Fragment并用另一个片段替换它.这很好用.不过,我已经注意到,在第一行的构造函数被调用但是onAttach(),onCreate()等等都没有.如果我要取消注释第二行,它将不起作用,因为updateItem(URL)需要在onCreate()函数中启动的webView.
DetailViewFragment detailFragment = new DetailViewFragment();
//detailFragment.updateItem(URL);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.displayList, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
Run Code Online (Sandbox Code Playgroud)
非常感谢任何可以使用第二行未使用的帮助.
我需要一些帮助.以这种方式将片段添加到活动中.问题是每次调用openFragment时都会创建片段并添加.这很明显.问题:我做了什么修改,所以它只能添加片段一次.在具有相同片段标签的下一次调用中,它将无效.
case:第一次按下按钮添加片段并显示.我再次按下相同的按钮它没有任何反应.
public static void openFragment(Activity activity, Fragment fragment) {
FragmentManager fragmentManager = ((ActionBarActivity) activity)
.getSupportFragmentManager();
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.addToBackStack(fragment.getClass().getSimpleName());
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
ftx.add(R.id.main_content, fragment);
ftx.commit();
}
Run Code Online (Sandbox Code Playgroud) stack android android-fragments android-fragmentactivity fragmentmanager
使用DrawerLayout具有NavigationView和FrameLayout我想改用Fragment秒.这很好用.但是,如果我切换太快,那么Fragments重叠......
这就像executePendingTransactions()不起作用.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="@240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/transparent"
android:dividerHeight="0dp"
app:menu="@menu/navigationdrawer" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
如果我Fragment快速切换s(太快)(手动或通过我的Nexus 5延迟750ms的代码),我得到两个Fragments重叠,第二个Fragment启用触摸但是第一个Fragment在顶部...
第一个Fragment包含an ImageView和TextViews.第二个Fragment包含a TabLayout和a ViewPager(如果我的问题可以解决这个问题).是的我正在使用AppCompat 22.2.0和Design 22.2.0库.
如果我为两者设置背景颜色,那么我只能看到第一个颜色Fragment,它永远不会改变.
我试过popBackStackImmediate(),executePendingTransactions(),remove(fragment), …
android android-fragments fragmentmanager android-design-library
我有一个特定的Android原生库来集成相机功能.该库为Fragment提供了它的所有功能.我只是想将它与React Native app中的Mainactivity集成.
我也经历了很多教程.但我被困在我们将片段添加到React本机应用程序的Mainactivity容器布局的地方.
更新:
React Component(JS文件):
export default class MyCustomView extends Component {
render() {
return (
<View >
{
MyLayout.openBlankFragment(12345)
}
</View>
);
}
}
const MyLayout = NativeModules.MyModule;
Run Code Online (Sandbox Code Playgroud)
MyModule(Java代码):
@ReactMethod
private void openBlankFragment(final int viewId) {
// Log.v("View Tag", "View ID: "+viewId); it prints tag 12345
UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
@Override
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
View view = nativeViewHierarchyManager.resolveView(viewId);
final Activity activity = getCurrentActivity();
BlankFragment fragment = new BlankFragment(); …Run Code Online (Sandbox Code Playgroud) android android-layout fragmentmanager react-native react-native-android
我使用的是导航中MainActivity,然后我开始SecondActivity(对结果).完成后SecondActivity我想继续导航MainActivity,但FragmentManager已经保存了他的状态.
在Navigation.findNavController(view).navigate(R.id.action_next, bundle)我收到的日志消息:
Ignoring navigate() call: FragmentManager has already saved its state
Run Code Online (Sandbox Code Playgroud)
我如何继续导航?
android fragmentmanager android-architecture-components android-jetpack android-architecture-navigation
android ×10
fragmentmanager ×10
back-stack ×2
fragment ×2
java ×2
android-architecture-components ×1
android-architecture-navigation ×1
display ×1
react-native ×1
savestate ×1
stack ×1