我有一些片段,我想通过按钮在一个片段中控制两个不同的布局。默认情况下,我希望看到第一个布局,然后单击按钮,将视图更改为第二个布局,然后单击相同或另一个按钮将视图再次更改为第一个布局。 这是我的片段:
public class HistoryActivity extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_history,
container, false);
Button btn1 = (Button) rootView.findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//what to place here
}
});
Button btn2 = (Button) rootView.findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//what to place here
}
});
return rootView;
}
}
这是我的主要:
public class MainActivity extends FragmentActivity implements TabListener {
private ActionBar actionbar;
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Tab1
ActionBar.Tab first = actionbar.newTab();
first.setText(getString(R.string.first));
first.setTabListener(this);
//Tab2
ActionBar.Tab second = actionbar.newTab();
second.setText(getString(R.string.second));
second.setTabListener(this);
//Tab3
ActionBar.Tab third = actionbar.newTab();
third.setText(getString(R.string.third));
third.setTabListener(this);
//Tab4
ActionBar.Tab fourth = actionbar.newTab();
fourth.setText(getString(R.string.fourth));
fourth.setTabListener(this);
actionbar.addTab(first);
actionbar.addTab(second);
actionbar.addTab(third);
actionbar.addTab(fourth);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
//Tab Adapter
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
//TODO Auto-generated method stub
Fragment fragment = null;
if(arg0==0){
fragment = new FirstTab();
}
if(arg0==1){
fragment = new SecondTab();
}
if(arg0==2){
fragment = new ThirdTab();
}
if(arg0==3){
fragment = new FourthTab();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
答案 0 :(得分:3)
在Fragment的布局中包含两种布局,并根据需要将可见性设置为View.VISIBLE
或View.GONE
。
编辑:添加一些伪代码
在您的布局中,您需要一个包含要在
之间切换的布局的FrameLayout<LinearLayout>
<FrameLayout>
<LinearLayout android:id="@+id/layout1" />
<LinearLayout android:id="@+id/layout2" />
</FrameLayout>
<LinearLayout> <!-- put buttons here --> </LinearLayout>
</LinearLayout>
在onCreateView中,为按钮分配单击侦听器以切换视图并进行渲染。
private View mLayout1, mLayout2;
private int mScreen;
private static final int SCREEN1 = 0;
private static final int SCREEN2 = 1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
// assign buttons
// assign layouts to class variables
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mScreen = SCREEN1;
renderScreen();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mScreen = SCREEN2;
renderScreen();
}
});
mScreen = SCREEN1; // default screen
renderScreen();
return rootView;
}
并显示正确的屏幕:
private void renderScreen() {
mLayout1.setVisibility(mScreen == SCREEN1 ? View.VISIBLE : View.GONE);
mLayout2.setVisibility(mScreen == SCREEN2 ? View.VISIBLE : View.GONE);
}