您的位置:首页 > 其它

fragment页面切换(隐藏,显示)

2018-03-05 13:05 239 查看


MainActivity

public class MainActivity extends Baseactivity{
@BindView(R.id.frag)
FrameLayout frag;
@BindView(R.id.rg)
RadioGroup rg;
@BindView(R.id.shouye)
RadioButton shouye;
@BindView(R.id.shangpin)
RadioButton shangpin;
@BindView(R.id.faxian)
RadioButton faxian;
private Shopfragment shopfragment;
private Shouyefragment shouyefragment;
private Faxianfragment faxianfragment;
@Override
protected void btonclick() {

}

@Override
protected int getContentView() {
return R.layout.activity_main;
}

@Override
protected void initdata() {
shouyefragment = new Shouyefragment();
getSupportFragmentManager().beginTransaction().add(R.id.frag, shouyefragment).commit();
//点击按钮 显示的方法
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
hidefragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch (i)
{
case R.id.shouye:
fragmentTransaction.show(shouyefragment).commit();
break;
case R.id.shangpin:
if(shopfragment==null)
{
shopfragment = new Shopfragment();
fragmentTransaction.add(R.id.frag,shopfragment).commit();
}
else
{
fragmentTransaction.show(shopfragment).commit();
}

break;
case R.id.faxian:
if(faxianfragment==null)
{
faxianfragment = new Faxianfragment();
fragmentTransaction.add(R.id.frag,faxianfragment).commit();
}else
{
fragmentTransaction.show(faxianfragment).commit();
}

break;
}

}

private void hidefragment() {
if(shouyefragment!=null&&shouyefragment.isAdded())
{
getSupportFragmentManager().beginTransaction().hide(shouyefragment).commit();
}
if(shopfragment!=null&&shopfragment.isAdded()){
getSupportFragmentManager().beginTransaction().hide(shopfragment).commit();
}
if(faxianfragment!=null&&faxianfragment.isAdded())
{
getSupportFragmentManager().beginTransaction().hide(faxianfragment).commit();
}
}

});
}

@Override
public void onClick(View view) {

}

}


BaseActivity

public abstract class Baseactivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
ButterKnife.bind(this);
getSupportActionBar().hide();
initdata();
btonclick();
}

protected abstract void btonclick();
protected abstract int getContentView();
protected abstract void initdata();

public void mytost(String s){
Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
}
public void jump(Class<?> c){
startActivity(new Intent(this,c));
}
}


布局

<LinearLayout
android:orientation="vertical"
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="liu.bwei.cow.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:id="@+id/frag"
></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/shouye"
android:checked="true"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/bg"
android:gravity="center"
android:layout_width="0px"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="首页"
/>
<RadioButton
android:id="@+id/shangpin"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/bg"
android:gravity="center"
android:layout_width="0px"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="商品"
/>
<RadioButton
android:id="@+id/faxian"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/bg"
android:gravity="center"
android:layout_width="0px"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="发现"
/>
</RadioGroup>
</LinearLayout>


drawable/bg布局

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@android:color/holo_red_dark"></item>
<item android:drawable="@android:color/white"></item>
</selector>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: