1. 程式人生 > >點選圖片切換另一張圖片

點選圖片切換另一張圖片

chang_msg:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/abx"></item>
    <item android:state_selected="false" android:drawable="@drawable/abw"></item>
</selector>

Main_layout

<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/fram_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/img_ac0"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/chang_msg"
                />

            <ImageView
                android:id="@+id/img_abw"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:src="@drawable/chang_clazz"/>

            <ImageView
                android:id="@+id/img_ac2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:src="@drawable/chang_my"/>

        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="#fff">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:src="@drawable/headbg"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawer_icon_client"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:text="商品"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawer_icon_recommend"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:text="瞎推薦"/>
        </LinearLayout>
    </LinearLayout>


</android.support.v4.widget.DrawerLayout>

MainActivity

public class ShowActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawerLayout dl_root;
    private FrameLayout fram_layout;
    private List<Fragment> 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);
    }
}