最近在使用 DataBinding ,顺便弄下ViewPage + Fragment
直接上代码,先看DockerItem的代码
public class DockerItem {
public static enum ItemType{
Home,Classfy,Personal
}
ItemType type;
int iconRes;
int textRes;
public DockerItem(int textRes,int iconRes,ItemType type){
this.textRes = textRes;
this.iconRes = iconRes;
this.type = type;
}
public ItemType getType() {
return type;
}
public void setType(ItemType type) {
this.type = type;
}
public int getIconRes() {
return iconRes;
}
public void setIconRes(int iconRes) {
this.iconRes = iconRes;
}
public int getTextRes() {
return textRes;
}
public void setTextRes(int textRes) {
this.textRes = textRes;
}
}
Image就是底部docker的图片,textView是文字说明
下面到dockerBar dockerBar的代码有点多,比较适用
public class DockerBar extends LinearLayout{
DockerBarBinding mDockerBarBinding;
private OnItemClickListener mOnItemClickListener;
List mItems = new ArrayList<>();
public interface OnItemClickListener{
void onItemClick(DockerBar dockerBar,DockerItem item);
}
public DockerBar(Context context) {
this(context,null);
}
public DockerBar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DockerBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initViews();
}
public void setmOnItemClickListener(OnItemClickListener l){
mOnItemClickListener = l;
}
private void init(){
DockerItem item = new DockerItem(R.string.home,R.drawable.dockbar_home_selector,DockerItem.ItemType.Home);
mItems.add(item);
item = new DockerItem(R.string.classify,R.drawable.dockbar_classify_selector,DockerItem.ItemType.Classfy);
mItems.add(item);
item = new DockerItem(R.string.personal,R.drawable.dockbar_personal_selector,DockerItem.ItemType.Personal);
mItems.add(item);
}
private void initViews(){
init();
LayoutInflater infaInflater = LayoutInflater.from(getContext());
System.out.print(infaInflater);
mDockerBarBinding = DataBindingUtil.inflate(infaInflater,R.layout.docker_bar,this,true);
mDockerBarBinding.dockerList.removeAllViews();
for (int i=0;i
dockerBar的XML布局文件如下:
public class FragAdapter extends FragmentPagerAdapter {
List mFragments = new ArrayList<>();
public FragAdapter(FragmentManager fm){
super(fm);
mFragments.add(new HomeFragment());
mFragments.add(new ClassifyFragment());
mFragments.add(new PersonalFragment());
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
}
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mBinding;
FragAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
setupViews();
}
private void setupViews() {
mAdapter = new FragAdapter(getSupportFragmentManager());
mBinding.viewPager.setAdapter(mAdapter);
mBinding.viewPager.setOnPageChangeListener(mOnPageChangeListener);
mBinding.dockerBar.setmOnItemClickListener(mOnItemClickListener);
}
private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mBinding.dockerBar.setSelection(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
private DockerBar.OnItemClickListener mOnItemClickListener = new DockerBar.OnItemClickListener() {
@Override
public void onItemClick(DockerBar dockerBar, DockerItem item) {
switch (item.getType()){
case Home:
mBinding.viewPager.setCurrentItem(0,false);
break;
case Classfy:
mBinding.viewPager.setCurrentItem(1,false);
break;
case Personal:
mBinding.viewPager.setCurrentItem(2,false);
break;
}
}
};
}
public class BaseFragment extends Fragment {
protected ViewGroup mViewGroup;
protected Context mContext;
protected Activity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
mContext = getActivity().getApplicationContext();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return mViewGroup;
}
public void onResume() {
super.onResume();
}
public void onPause() {
super.onPause();
}
}
最后就是一个简单的Fragment 了继承Base
public class HomeFragment extends BaseFragment {
private static final String TAG = "HomeFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mViewGroup == null) {
HomeFragmentBinding binding = HomeFragmentBinding.inflate(inflater, null, false);
mViewGroup = (ViewGroup) binding.getRoot();
}
return super.onCreateView(inflater, container, savedInstanceState);
}
}
源码下载:http://download.csdn.net/detail/zmadmad/9500247