package com.example.administrator.zonghe;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import com.example.administrator.fragment.Frag1;
import com.example.administrator.fragment.Frag2;
import com.example.administrator.fragment.Frag3;
import com.example.administrator.fragment.Frag4;
public class MainActivity extends AppCompatActivity {
private FrameLayout frag_layout;
private RadioGroup radio_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout d1 = (DrawerLayout)findViewById(R.id.d1);
ImageView iv1 = (ImageView)findViewById(R.id.iv1);
ImageView iv2 = (ImageView)findViewById(R.id.iv2);
frag_layout = (FrameLayout)findViewById(R.id.frag_layout);
radio_group = (RadioGroup)findViewById(R.id.radio_group);
iv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d1.openDrawer(Gravity.LEFT);
}
});
iv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d1.closeDrawer(Gravity.LEFT);
}
});
d1.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
View content = d1.getChildAt(0);
float scale = 1 - slideOffset;
content.setTranslationX(drawerView.getMeasuredWidth()*(1-scale));
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId){
case R.id.rb1:
changePager(new Frag1());
break;
case R.id.rb2:
changePager(new Frag2());
break;
case R.id.rb3:
changePager(new Frag3());
break;
case R.id.rb4:
changePager(new Frag4());
break;
}
}
});
}
private void changePager(Fragment fragment){
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,fragment).commit();
}
}