概述
周末在github上浏览了一个分段选择器的实现,其实就是一个自定义view,写了一个小demo实验了一下,感觉很不错,遂记之!
效果图:
实现很简单,主要是在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学习之分段选择器的实现的全部内容,希望文章能够帮你解决Android学习之分段选择器的实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复