
我想在“网格和列表”项的项目单击上用fragment-1上的fragment-1和fragment-2上的fragment添加和替换Frame布局.
我已经创建了main.xml类
<?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="horizontal" ><linearLayout androID:layout_wIDth="0dp" androID:layout_weight="1" androID:orIEntation="vertical" androID:layout_height="fill_parent" > <fragment androID:ID="@+ID/fragment1" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" /> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="1dp" androID:background="#000000" /> <fragment androID:ID="@+ID/fragment2" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" /></linearLayout><VIEw androID:layout_wIDth="1dp" androID:layout_height="fill_parent" androID:background="#000000" /><FrameLayout androID:ID="@+ID/fragment3" androID:layout_wIDth="0dp" androID:layout_height="fill_parent" androID:layout_weight="2"/>这是我的fragment_1.xml
<?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"androID:paddingleft="8dp"androID:paddingRight="8dp" ><GrIDVIEw androID:ID="@+ID/GrID1" androID:layout_wIDth="match_parent" androID:numColumns="auto_fit" androID:columnWIDth="100dp" androID:layout_height="0dp" androID:layout_weight="1" androID:drawSelectorOntop="false" />这是我的row_fragment1_List.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:layout_margin="15dp"androID:orIEntation="vertical" ><ImageVIEw androID:ID="@+ID/img_vIEw" androID:layout_wIDth="50dp" androID:layout_height="50dp" androID:background="#CCCCCC" androID:textSize="16sp" androID:textStyle="bold" /><TextVIEw androID:ID="@+ID/text2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="16sp" />和fragment_3.xml
<?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:gravity="center"androID:orIEntation="vertical" ><TextVIEw androID:ID="@+ID/tv_fragment3" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textSize="22sp" androID:gravity="center" androID:text="This is 3rd Fragment" />这是我的Main.xml
public class Main extends Activity{ @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); }}这是Fragment1.class
public class Fragment1 extends Fragment implements OnItemClickListener{ Activity myActivity; GrIDVIEw mGrIDVIEw; private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7"}; private int imgID[] = {R.drawable.admin_access_rule, R.drawable.admin_backup,R.drawable.admin_browsesite, R.drawable.admin_comment_post,R.drawable.admin_content, R.drawable.admin_content_type,R.drawable.admin_logout,}; @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show(); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create VIEw", Toast.LENGTH_SHORT).show(); VIEw vIEw = inflater.inflate(R.layout.fragment_1,container, false); mGrIDVIEw = (GrIDVIEw)vIEw.findVIEwByID(R.ID.GrID1); return vIEw; } @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState); } @OverrIDe public voID onStart() { super.onStart(); mGrIDVIEw.setAdapter(new GrIDBaseAdapter(myActivity)); mGrIDVIEw.setonItemClickListener(this); } private class GrIDBaseAdapter extends BaseAdapter { LayoutInflater mLayoutInflater = null; public GrIDBaseAdapter(Context mContext) { mContext = myActivity; mLayoutInflater = LayoutInflater.from(mContext); } @OverrIDe public int getCount() { return ListItem.length; } @OverrIDe public Object getItem(int position) { return null; } @OverrIDe public long getItemID(int position) { return ListItem.length; } @OverrIDe public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) { if(convertVIEw == null) { convertVIEw = mLayoutInflater.inflate(R.layout.row_fragment_List, null); } ImageVIEw mImageVIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.img_vIEw); mImageVIEw.setimageResource(imgID[position]); TextVIEw tvUserEmail = (TextVIEw) convertVIEw.findVIEwByID(R.ID.text2); tvUserEmail.setText("Sub " +ListItem[position]); return convertVIEw; }}@OverrIDepublic voID onItemClick(AdapterVIEw<?> arg0, VIEw vIEw, int position, long ID) { TextVIEw txt3 = (TextVIEw)myActivity.findVIEwByID(R.ID.tv_fragment3); txt3.setText("1st Fragment's : " + position +" Item Clicked");}}我的Fragment2.class
public class Fragment2 extends ListFragment{ Activity myActivity; private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 6","Item 7"};@OverrIDepublic voID onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();}@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create VIEw", Toast.LENGTH_SHORT).show(); /** Creating an array adapter to store the List of countrIEs **/ ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), androID.R.layout.simple_List_item_1,ListItem); /** Setting the List adapter for the ListFragment */ setlistadapter(adapter); return super.onCreateVIEw(inflater, container, savedInstanceState);}@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState);}@OverrIDepublic voID onListItemClick(ListVIEw l, VIEw v, int position, long ID) { Toast.makeText(myActivity.getApplicationContext(), position +" Item Clicked", Toast.LENGTH_SHORT).show(); TextVIEw txt3 = (TextVIEw)myActivity.findVIEwByID(R.ID.tv_fragment3); txt3.setText("2nd Fragment's : " + position +" Item Clicked"); super.onListItemClick(l, v, position, ID);}}最后是我的Fragment3.class
public class Fragment3 extends Fragment{Activity myActivity;TextVIEw txt_vIEw; @OverrIDepublic voID onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();}@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create VIEw", Toast.LENGTH_SHORT).show(); VIEw vIEw = inflater.inflate(R.layout.fragment_3,container, false); txt_vIEw = (TextVIEw)vIEw.findVIEwByID(R.ID.tv_fragment3); return vIEw;}@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState);}} 解决方法:
使片段公开活动可以附加到的接口.该事件应在需要时发生(例如,单击列表项).然后,活动应将片段实例放置在应放置的位置,例如:
// fragment is an instance of the fragment you want to showfinal FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.replace(R.ID.fragment3, fragment);transaction.commit(); 总结 以上是内存溢出为你收集整理的java-如何替换片段运行时全部内容,希望文章能够帮你解决java-如何替换片段运行时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)