仿美团分段式选择控件

第一步:添加compile:

compile 'info.hoang8f:android-segmented:1.0.6'

第二步xml配置:

<info.hoang8f.android.segmented.SegmentedGroup
            xmlns:segmentedgroup="http://schemas.android.com/apk/res-auto"
            android:id="@+id/mv_segmented"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_margin="8dp"
            android:orientation="horizontal"
            android:layout_centerVertical="true"
            segmentedgroup:sc_border_width="1dp"
            segmentedgroup:sc_corner_radius="14dp"
            segmentedgroup:sc_tint_color="#AFAFAF">
            <RadioButton
                android:id="@+id/mv_film"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="电影"
                style="@style/RadioButton" />
            <RadioButton
                android:id="@+id/mv_show"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="演出"
                style="@style/RadioButton" />
        </info.hoang8f.android.segmented.SegmentedGroup>

代码中使用:

package com.mirrorflower.idolfans.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mirrorflower.idolfans.R;
import com.mirrorflower.idolfans.fragment.MovieFilmFragment;
import com.mirrorflower.idolfans.fragment.MovieShowFragment;

import info.hoang8f.android.segmented.SegmentedGroup;

/** * 电影 **/
public class MovieActivity extends BaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
    private SegmentedGroup mv_segmented;
    private RadioButton mv_film, mv_show;
    private MovieFilmFragment movieFilmFragment;
    private MovieShowFragment movieShowFragment;
    private FragmentManager manager = getSupportFragmentManager();
    private FragmentTransaction mTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        initView();
    }

    /** * 初始化View */
    private void initView() {
        findViewById(R.id.mv_back).setOnClickListener(this);
        mv_segmented = (SegmentedGroup) findViewById(R.id.mv_segmented);
        mv_segmented.setOnCheckedChangeListener(this);
        mv_film = (RadioButton) findViewById(R.id.mv_film);
        mv_show = (RadioButton) findViewById(R.id.mv_show);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mv_back:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        mTransaction = manager.beginTransaction();
        if(movieFilmFragment!=null){
            mTransaction.hide(movieFilmFragment);
        }
        if(movieShowFragment!=null){
            mTransaction.hide(movieShowFragment);
        }
        switch (checkedId){
            case R.id.mv_film:
                if(movieFilmFragment==null){
                    movieFilmFragment = new MovieFilmFragment();
                    mTransaction.add(R.id.mv_content,movieFilmFragment);
                }else {
                    mTransaction.show(movieFilmFragment);
                }
                break;
            case R.id.mv_show:
                if(movieShowFragment==null){
                    movieShowFragment = new MovieShowFragment();
                    mTransaction.add(R.id.mv_content,movieShowFragment);
                }else {
                    mTransaction.show(movieShowFragment);
                }
                break;
        }
        mTransaction.commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mv_film.setChecked(true);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
    }
}

你可能感兴趣的:(仿美团分段式选择控件)