第三方SegmentControl+fragment实现选择切换

先看一下效果图,是否是需要的效果


这个是引入第三方的一个库,(感谢大神的第三方,很好用)

compile 'com.7heaven.widgets:segmentcontrol:1.16'

直接看代码,很简单,看得懂

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.yangzhuokeji.myapplication.MainActivity">

    <com.sevenheaven.segmentcontrol.SegmentControl
        android:id="@+id/segment_control"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:textSize="13sp"
        app:cornerRadius="5dip"
        app:horizonGap="8dip"
        app:normalColor="#FFFFFF"
        app:selectedColor="#0099CC"
        app:texts="策划|开发|企业|导航"
        app:verticalGap="8dip"
        tools:layout_editor_absoluteX="17dp"
        tools:layout_editor_absoluteY="0dp" />

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#0099CC">

    </FrameLayout>
</LinearLayout>
MainActivity.java

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.sevenheaven.segmentcontrol.SegmentControl;
import com.yangzhuokeji.zhifubaozhifu.shangbiao.Fragmentas;
import com.yangzhuokeji.zhifubaozhifu.shangbiao.Fragmentas2;
import com.yangzhuokeji.zhifubaozhifu.shangbiao.Fragmentas4;
import com.yangzhuokeji.zhifubaozhifu.shangbiao.fragment3;


public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private FrameLayout framelayout;
    private SegmentControl mSegmentHorzontal;
    private SegmentControl mSegmentVertical;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        mSegmentHorzontal = (SegmentControl) findViewById(R.id.segment_control);
        // mSegmentVertical = (SegmentControl) findViewById(R.id.segment_control2);
        FragmentManager fm1 = getFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm1.beginTransaction();
        transaction.replace(R.id.framelayout, new Fragmentas());
        transaction.commit();
        mSegmentHorzontal.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onSegmentControlClick(int index) {
                /*Log.i(TAG, "onSegmentControlClick: index = " + index);
                if (index == 2) {
                    FragmentManager fm = getFragmentManager();
                    // 开启Fragment事务
                    FragmentTransaction transaction = fm.beginTransaction();
                    transaction.replace(R.id.framelayout, new Fragmentas());
                    transaction.commit();
                }else{
                    Toast.makeText(MainActivity.this, "666", Toast.LENGTH_SHORT).show();
                }*/
                FragmentManager fm = getFragmentManager();
                // 开启Fragment事务
                FragmentTransaction transaction = fm.beginTransaction();

                switch (index) {

                    case 0:
                        transaction.replace(R.id.framelayout, new Fragmentas());
                        break;

                    case 1:
                        transaction.replace(R.id.framelayout, new Fragmentas2());
                        break;
                    case 2:
                        transaction.replace(R.id.framelayout, new fragment3());
                        break;
                    case 3:
                        transaction.replace(R.id.framelayout, new Fragmentas4());
                        break;

                    default:

                        break;
                }
                transaction.commit();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

BaseFragment.java

import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * Created by Administrator on 2016/9/21.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public abstract class BaseFragment extends Fragment {

    boolean isTranslucentNavigation = false;

    /*
    * 1.onCreateView是创建的时候调用,onViewCreated是onCreateView后被触发的事件,前后关系
    * */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentViewId(), container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    public static boolean hasKitKat() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    public static boolean hasLollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    public boolean isTranslucentNavigation() {
        return isTranslucentNavigation;
    }

    public void setTranslucentNavigation(boolean translucentNavigation) {
        isTranslucentNavigation = translucentNavigation;
    }
    //沉浸式状态栏结束

    public abstract int getContentViewId();

    protected abstract void initData();

    public abstract void TitleBar(boolean isImmersive);

}
下面的几个fragment都是一样的,自己修改布局,在所在的fragment做操作就可以了,只展示一个

fragmet3.java

import android.annotation.TargetApi;
import android.os.Build;

/**
 * Created by Android-01 on 2017/5/10.
 */

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class fragment3 extends BaseFragment {

    @Override
    public int getContentViewId() {
        return R.layout.fragment3;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void TitleBar(boolean isImmersive) {

    }
}

fragment3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        />
</LinearLayout>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值