Fragment嵌套使用的一些问题
因为项目需要在Fragment里面嵌套Fragment,只有第一次进去的时候能看嵌套的Fragment,在切换FragmentActivity 就看不见嵌套的了。
第一次TAB切换的时可以看见viewpager
--------------------编程问答--------------------
第二次就看不见了 --------------------编程问答-------------------- 下面是代码
第一个Fragment代码
package com.EIS.Fragments;
import java.util.ArrayList;
import java.util.List;
import com.EIS.R;
import com.EIS.Adapter.QueryFragmentPagerAdapter;
import com.EIS.Adapter.TabFragmentPagerAdapter;
import com.EIS.Listener.MenuButtonOnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class InterActionFragment extends Fragment {
private View view;
private ViewPager mViewPager;
private TabFragmentPagerAdapter fragmentPagerAdapter;
private RadioGroup group;
private static List<Fragment> fragments;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.interactionview, container, false);
mViewPager = (ViewPager) view.findViewById(R.id.query_viewpager);
group = (RadioGroup) view.findViewById(R.id.group);
fragments = new ArrayList<Fragment>();
fragments.add(new EnterpriseQueryframgent());
fragments.add(new EnterpriseQueryframgent());
mViewPager.setAdapter(fragmentPagerAdapter);
fragmentPagerAdapter = new TabFragmentPagerAdapter(
getFragmentManager(), fragments);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.bt_query) {
mViewPager.setCurrentItem(0);
} else {
mViewPager.setCurrentItem(1);
}
}
});
return view;
}
}
FragmentPagerAdapter代码
package com.EIS.Adapter;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
public static final String ARGUMENTS_NAME = "EXPERTQUERY";
private List<Fragment> fragments;
public TabFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
Log.i("ss", "查看调构造器 listsize():" + fragments.size());
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
Log.i("ss", "查看调用 fragments: " + fragments.get(arg0));
return fragments.get(arg0);
}
@Override
public int getCount() {
return fragments.size();
}
}
嵌套 Fragment代码
package com.EIS.Fragments;
import com.EIS.R;
import com.EIS.Listener.MenuButtonOnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class EnterpriseQueryframgent extends Fragment implements
MenuButtonOnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.enterprisequeryframgent, container, false);
//tv_tabName.setText(bundle.getString(MainActivity.ARGUMENTS_NAME, ""));
return rootView;
}
@Override
public void OnClick(View v) {
// TODO Auto-generated method stub
}
}
--------------------编程问答-------------------- 有沒有高手看完了指导一下 --------------------编程问答-------------------- 布局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:background="@color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/title_background"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/buttom_interactive"
android:textColor="@color/white"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/bt_query"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/selector_interaction_query"
android:button="@null"
android:checked="true"
android:clickable="true"
android:gravity="center"
android:text="@string/enterprise_query" />
<RadioButton
android:id="@+id/bt_entrust"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/selector_interaction_entrust"
android:button="@null"
android:clickable="true"
android:gravity="center"
android:text="@string/my_entrust" />
</RadioGroup>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/query_viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@id/rl_tab"
android:layout_gravity="center"
android:background="#ffffff"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
</LinearLayout>
这个是第一个fragment的布局 --------------------编程问答--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res/com.EIS"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/interaction_retrieval_text"
android:hint="@string/input_keyword" />
<RelativeLayout
android:layout_width="95dp"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@drawable/selector_interaction_retrieval"
android:scaleType="fitStart"
android:src="@drawable/icon_interactive_retrieval" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/bt_query"
android:textColor="@color/white"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</LinearLayout>
这是viewpager的布局
补充:移动开发 , Android