Android学习之分段选择器的实现
2016-05-15 19:59
344 查看
周末在github上浏览了一个分段选择器的实现,其实就是一个自定义view,写了一个小demo实验了一下,感觉很不错,遂记之!
效果图:
实现很简单,主要是在fragment的填充
布局:
segmentcontrol:texts=”我的好友|同学|同事|老师”。用来设置分段内容。
MainActivity
setDefaultFragment()加载默认Fragment,setOnSegmentControlClickListener监听分段选择器的点击事件。然后填充Fragment。
Fragment实例化用Fragment自带的静态工程方法newInstance(),便于传值和调用。
Fragment的实现
这里没什么讲的,主要是静态工厂方法newInstance,和利用setArgument传值。
segmentcontrol代码会在项目中给出,这个demo可以直接用于项目中。
下载地址:
http://pan.baidu.com/s/1mietbvi
效果图:
实现很简单,主要是在fragment的填充
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity"> <com.example.wangchang.segmentcontrol.segmentcontrol.SegmentControl xmlns:segmentcontrol="http://schemas.android.com/apk/res-auto" android:id="@+id/segment_control" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:textSize="13sp" segmentcontrol:colors="@color/colorPrimary" segmentcontrol:cornerRadius="5dp" segmentcontrol:direction="horizon" segmentcontrol:horizonGap="8dp" segmentcontrol:texts="我的好友|同学|同事|老师" segmentcontrol:verticalGap="8dp" /> <FrameLayout android:id="@+id/layFragme" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/segment_control" /> </RelativeLayout>
segmentcontrol:texts=”我的好友|同学|同事|老师”。用来设置分段内容。
MainActivity
package com.example.wangchang.segmentcontrol.activity; import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.example.wangchang.segmentcontrol.R; import com.example.wangchang.segmentcontrol.fragment.BookFragment; import com.example.wangchang.segmentcontrol.fragment.FavoritesFragment; import com.example.wangchang.segmentcontrol.fragment.FindFragment; import com.example.wangchang.segmentcontrol.fragment.LocationFragment; import com.example.wangchang.segmentcontrol.segmentcontrol.SegmentControl; public class MainActivity extends AppCompatActivity { private LocationFragment mLocationFragment; private FindFragment mFindFragment; private FavoritesFragment mFavoritesFragment; private BookFragment mBookFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SegmentControl show = (SegmentControl) findViewById(R.id.segment_control); setDefaultFragment(); show.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() { @Override public void onSegmentControlClick(int index) { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); switch (index) { case 0: if (mLocationFragment == null) { mLocationFragment = LocationFragment.newInstance("我的好友"); } ft.replace(R.id.layFragme, mLocationFragment).commitAllowingStateLoss(); Snackbar.make(show, " 好友", Snackbar.LENGTH_SHORT).show(); break; case 1: if (mFindFragment == null) { mFindFragment = FindFragment.newInstance("同学"); } ft.replace(R.id.layFragme, mFindFragment).commitAllowingStateLoss(); Snackbar.make(show, " 同学", Snackbar.LENGTH_SHORT).show(); break; case 2: if (mFavoritesFragment == null) { mFavoritesFragment = FavoritesFragment.newInstance("同事"); } ft.replace(R.id.layFragme, mFavoritesFragment).commitAllowingStateLoss(); Snackbar.make(show, " 同事", Snackbar.LENGTH_SHORT).show(); break; case 3: if (mBookFragment == null) { mBookFragment = BookFragment.newInstance("老师"); } ft.replace(R.id.layFragme, mBookFragment).commitAllowingStateLoss(); Snackbar.make(show, " 老师", Snackbar.LENGTH_SHORT).show(); break; } } }); } private void setDefaultFragment() { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (mLocationFragment == null) { mLocationFragment = LocationFragment.newInstance("我的好友"); ft.replace(R.id.layFragme, mLocationFragment).commitAllowingStateLoss(); } } }
setDefaultFragment()加载默认Fragment,setOnSegmentControlClickListener监听分段选择器的点击事件。然后填充Fragment。
Fragment实例化用Fragment自带的静态工程方法newInstance(),便于传值和调用。
Fragment的实现
package com.example.wangchang.segmentcontrol.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.wangchang.segmentcontrol.R; /** * Created by WangChang on 2016/5/15. */ public class BookFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_index, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); TextView tv = (TextView) getActivity().findViewById(R.id.tv); Bundle bundle = getArguments(); if (bundle != null) { tv.setText(bundle.getString("ARGS")); } } public static BookFragment newInstance(String content) { Bundle args = new Bundle(); args.putString("ARGS", content); BookFragment fragment = new BookFragment(); fragment.setArguments(args); return fragment; } }
这里没什么讲的,主要是静态工厂方法newInstance,和利用setArgument传值。
segmentcontrol代码会在项目中给出,这个demo可以直接用于项目中。
下载地址:
http://pan.baidu.com/s/1mietbvi
相关文章推荐
- android开发人员获取sqlite
- Android NDK下载链接
- 走进Android天才少年-1999年的Lody(罗迪)
- Android Intent ArrayList
- Android ZXing竖屏解决
- Android Timer(计时器)
- Android 跳转到市场给好评
- Android Notification 通知栏
- 各种Android插件
- Android应用资源---字符串资源类型(String)
- android 滚动条
- Android SQLite 操作
- Android文件上传,PHP端接收
- Android学习路线指南
- Android TextView drawableLeft 在代码中实现
- Android APK反编译详解(附图)【转】
- Android再按一次退出软件
- Android 如何让EditText不自动获取焦点
- Android Socket 的使用
- Android Dialog 对话框