本文介绍了如何在项目点击的活动中调用多个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个应用程序,其中我需要在一个活动上调用两个片段来替换另一个片段,其中一个片段将包含一个日期时间选择器,另一个片段将包含所选日期的相关项列表,我"不知道如何执行此操作,因此,请提前感谢您的任何建议....
i am developing an application where i need to call two fragments on an activity replacing another fragment,one of the fragment will contain a date time picker and another fragment will contain the list of the related items of the selected date,i don''t have any idea how to do this,so any suggestion will be cordially appreciated,thanks in advance....
推荐答案
package balaji.fragment_static;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment>
android:name="balaji.fragment_static.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment>
android:name="balaji.fragment_static.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</fragment></fragment></linearlayout>
package balaji.fragment_static;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false);
}
}
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:background="#7B68EE"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<textview>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30dp"
android:text="Fragment 1" />
</textview></relativelayout>
package balaji.fragment_static;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2, container, false);
}
}
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:background="#00FF00"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<textview>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30dp"
android:text="Fragment 2" />
</textview></relativelayout>
这篇关于如何在项目点击的活动中调用多个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!