If you think the Android project sres listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package me.tatarka.sres;
//www.java2s.comimport android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by evan on 3/11/14.
*/publicclass ViewPager extends android.support.v4.view.ViewPager {
private LayoutSelectorHelper helper = new LayoutSelectorHelper();
public ViewPager(Context context) {
this(context, null);
}
public ViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SRes);
int layout = a.getResourceId(R.styleable.SRes_layout, 0);
if (layout > 0) setLayout(layout);
a.recycle();
}
}
public <T> void setLayoutSelector(LayoutSelector<T> layoutSelector) {
helper.setLayoutSelector(layoutSelector);
if (getAdapter() != null) {
ObservableList<T> model = (ObservableList<T>) getAdapter().getList();
setPages(model);
}
}
publicvoid setLayout(finalint layoutId) {
setLayoutSelector(LayoutSelectorHelper.fromLayout(layoutId));
}
@Override
public ObservablePagerAdapter<?> getAdapter() {
return (ObservablePagerAdapter<?>) super.getAdapter();
}
public <T> void setPages(final List<T> pages) {
setAdapter(new ObservablePagerAdapter<T>(pages) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
T item = getItem(position);
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(helper.getLayout(position, item), parent, false);
}
((Bindable<T>) view).bind(item);
return view;
}
@Override
publicint getItemViewType(int position) {
return helper.getItemViewType(position, getItem(position));
}
@Override
publicint getViewTypeCount() {
return helper.getItemTypeCount();
}
});
}
}