1 SteppersView.Config steppersViewConfig = new SteppersView.Config();
在自定义view的里面的内部类
private OnFinishAction onFinishAction;
private OnCancelAction onCancelAction;
private FragmentManager fragmentManager;取消和完成时候需要做的事情
配置类主要有三个内部属性。一个是管理fragment的,另外两个是接口
====================================================================
public void build() {
if(config != null) {
setOrientation(LinearLayout.HORIZONTAL);
//LayoutInflater inflater = (LayoutInflater) getContext
().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater.inflate(R.layout.steppers_recycle, this, true);
//recyclerView = (RecyclerView) getChildAt(0);
recyclerView = new RecyclerView(getContext());
RecyclerView.LayoutParams layoutParams = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
recyclerView.setLayoutParams(layoutParams);
addView(recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager (getContext
()));
steppersAdapter = new SteppersAdapter(this, config, items);
//steppersAdapter.setPossitiveButtonEnable
(possitiveButtonEnable);
recyclerView.setAdapter(steppersAdapter);
} else {
throw new RuntimeException("SteppersView need config, read
documentation to get more info");
}
}
recyclerView.setHasFixedSize(true);//size固定的话,用这个方法性能会提升
recyclerView.setLayoutManager(new LinearLayoutManager (getContext
()));
====================================================================
看一下他的adapter
@Override
public int getItemViewType(int position) {
return (position == currentStep ? VIEW_EXPANDED : VIEW_COLLAPSED);
}
判断是否是当前步骤,是的话就展开,不是就不 展开,确定布局类型
====================================================================
@Override
public SteppersViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View v = null;
if (viewType == VIEW_COLLAPSED) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.steppers_item, parent, false);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.steppers_item_expanded, parent,
false);
}
SteppersViewHolder vh = new SteppersViewHolder(v);
return vh;
}
确定这个holder是从哪个layout获取到view的
然后再另外一个类,实例holder
public class SteppersViewHolder extends RecyclerView.ViewHolder {
private boolean isChecked;
protected View itemView;
protected RoundedView roundedView;
protected TextView textViewLabel;
protected TextView textViewSubLabel;
protected LinearLayout linearLayoutContent;
protected FrameLayout frameLayout;
protected LinearLayout frameLayoutsContainer;
protected Button buttonContinue;
protected Button buttonCancel;
protected Fragment fragment;
public SteppersViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
this.roundedView = (RoundedView) itemView.findViewById
(R.id.roundedView);
this.textViewLabel = (TextView) itemView.findViewById
(R.id.textViewLabel);
this.textViewSubLabel = (TextView) itemView.findViewById
(R.id.textViewSubLabel);
this.linearLayoutContent = (LinearLayout) itemView.findViewById
(R.id.linearLayoutContent);
this.frameLayout = (FrameLayout) itemView.findViewById
(R.id.frameLayout);
//this.frameLayoutsContainer = (LinearLayout) itemView.findViewById
(R.id.frameLayoutsContainer);
this.buttonContinue = (Button) itemView.findViewById
(R.id.buttonContinue);
this.buttonCancel = (Button) itemView.findViewById
(R.id.buttonCancel);
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public Fragment getFragment() {
return fragment;
}
/**
* @return true if step is done, false if not
*/
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
========================================================================
@Override
public void onBindViewHolder(final SteppersViewHolder holder, final int
position) {
}
主要是具体的布局了
首先判断 if(holder.isChecked())roundedView的状态
roundedView
onDraw
paint.setAntiAlias(true);抗锯齿
private void drawText(Canvas canvas) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(getResources().getDimension
(R.dimen.item_circle_text_size));
Rect areaRect = new Rect(0, 0, canvas.getWidth(), canvas.getHeight
());
RectF bounds = new RectF(areaRect);
bounds.right = paint.measureText(text, 0, text.length());
bounds.bottom = paint.descent() - paint.ascent();
bounds.left += (areaRect.width() - bounds.right) / 2.0f;
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f;
paint.setColor(Color.WHITE);
canvas.drawText(text, bounds.left, bounds.top - paint.ascent(),
paint);
}
设置字体。不断的draw
========================================================
if(position == currentStep || holder.isChecked())
holder.roundedView.setCircleAccentColor();
else holder.roundedView.setCircleGrayColor();
设置颜色值
holder.linearLayoutContent.setVisibility(position == currentStep ||
position == beforeStep ? View.VISIBLE : View.GONE);
========================================================
steppersItem.addObserver(new Observer() {
@Override
public void update(Observable observable, Object data) {
if(observable != null) {
SteppersItem item = (SteppersItem) observable;
holder.buttonContinue.setEnabled
(item.isPositiveButtonEnable());
}
}
});
给其中一个item设置
在下面
private void nextStep() {
this.removeStep = currentStep - 1 > -1 ? currentStep - 1 :
currentStep;
this.beforeStep = currentStep;
this.currentStep = this.currentStep + 1;
notifyItemRangeChanged(removeStep, currentStep);
}
最后一句话告诉他要改变
===================================================
if (position == getItemCount() - 1) holder.buttonContinue.setText
(context.getResources().getString(R.string.step_finish));
else holder.buttonContinue.setText(context.getResources().getString
(R.string.step_continue));
holder.buttonContinue.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(position == getItemCount() - 1) config.getOnFinishAction
().onFinish();
else nextStep();
}
});
结束的时候的处理
======================================
fragment的处理
if(position < beforeStep) {
if (fragment != null) {
if(BuildConfig.DEBUG) Log.v(TAG, "Removing item #" +
position + ": f=" + fragment);
fragmentTransaction.detach(fragment);
}
} else if(position == beforeStep || position == currentStep) {
if (fragment != null) {
if(BuildConfig.DEBUG) Log.v(TAG, "Attaching item #" +
position + ": f=" + fragment + " d=" + fragment.isDetached());
fragmentTransaction.attach(fragment);
} else {
fragment = steppersItem.getFragment();
if(BuildConfig.DEBUG) Log.v(TAG, "Adding item #" +
position + ": f=" + fragment + " n=" + name);
fragmentTransaction.add(steppersView.getId(), fragment,
name);
}
}
fragmentTransaction.detach(fragment);之前的就解绑
======================================
if (fragmentTransaction != null) {
fragmentTransaction.commitAllowingStateLoss();
fragmentTransaction = null;
fragmentManager.executePendingTransactions();
}
if(fragmentManager.findFragmentByTag(name) != null &&
fragmentManager.findFragmentByTag(name).getView() !=
null) {
View fragmentView = fragmentManager.findFragmentByTag
(name).getView();
if(fragmentView.getParent() != null && frameLayoutName() !=
((View) fragmentView.getParent()).getTag()) {
steppersView.removeViewInLayout(fragmentView);
holder.frameLayout.removeAllViews();
holder.frameLayout.addView(fragmentView);
}
}
}
添加view