点击图片切换另一张图片

chang_msg:


    
    

Main_layout


    

        

        
            

            

            

        
    
    
        
        
            
            
        
        
            
            
        
    


MainActivity

public class ShowActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawerLayout dl_root;
    private FrameLayout fram_layout;
    private List list;
    private ActionBarDrawerToggle toggle;
    private FragmentManager manager;
    private ImageView img_ac0;
    private ImageView img_abw;
    private ImageView img_ac2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        dl_root = findViewById(R.id.dl_root);
        fram_layout = findViewById(R.id.fram_layout);
        img_ac0 = findViewById(R.id.img_ac0);
        img_abw = findViewById(R.id.img_abw);
        img_ac2 = findViewById(R.id.img_ac2);

        dl_root.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

        InitActionBar();

        list = new ArrayList<>();
        list.add(new MsgFragment());
        list.add(new ClazzFragment());
        list.add(new MyFragment());

        manager = getSupportFragmentManager();
        img_ac0.setSelected(true);
        manager.beginTransaction().replace(R.id.fram_layout,new MsgFragment()).commit();
        img_ac0.setOnClickListener(this);
        img_ac2.setOnClickListener(this);
        img_abw.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_ac0:
                img_ac0.setSelected(true);
                img_abw.setSelected(false);
                img_ac2.setSelected(false);
                manager.beginTransaction().replace(R.id.fram_layout,new MsgFragment()).commit();
                break;
            case R.id.img_abw:
                img_ac0.setSelected(false);
                img_abw.setSelected(true);
                img_ac2.setSelected(false);
                manager.beginTransaction().replace(R.id.fram_layout,new ClazzFragment()).commit();
                break;
            case R.id.img_ac2:
                img_ac0.setSelected(false);
                img_abw.setSelected(false);
                img_ac2.setSelected(true);
                manager.beginTransaction().replace(R.id.fram_layout,new MyFragment()).commit();
                break;
        }
    }

    private void InitActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        toggle = new ActionBarDrawerToggle(this, dl_root, R.string.open, R.string.close);
        toggle.syncState();
        dl_root.addDrawerListener(toggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

你可能感兴趣的:(点击图片切换另一张图片)