三級聯(lián)動
原來的樣式好難看,發(fā)現(xiàn)沒有設(shè)置樣式的地方猖任,只有更改源碼一途了
Drawable資源
- wheel_bg.xml 將面的內(nèi)容注釋
這個用來設(shè)置背景色的
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#333"
android:centerColor="#DDD"
android:endColor="#333"
android:angle="90" />
<stroke android:width="1dp" android:color="#FF333333" />
</shape>
</item>
<item android:left="4dp" android:right="4dp" android:top="1dp" android:bottom="1dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#AAA"
android:centerColor="#FFF"
android:endColor="#AAA"
android:angle="90" />
</shape>
</item>
</layer-list>
- wheel_val.xml
控件中間選擇中部分
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#70222222"
android:centerColor="#70222222"
android:endColor="#70EEEEEE"
android:angle="90" />
<stroke android:width="1dp" android:color="#70333333" />
漸變
<gradient
android:startColor="#22ffffff"
android:centerColor="#00ffffff"
android:endColor="#22ffffff"
android:angle="90" />
</shape>
WheelView類更改陰影色
/**
* Top and bottom shadows colors
*/
// private static final int[] SHADOWS_COLORS = new int[]{0xeeFF0000, 0xee00FF00, 0xee0000FF};
/**
* 更改陰影色
*/
private static final int[] SHADOWS_COLORS = new int[]{ 0xeeffffff, 0xeaffffff, 0x33ffffff };
這樣就有漸變的效果了
中間選擇部分加2條線
- 在drawCenterRect方法 中
private void drawCenterRect(Canvas canvas) {
int center = getHeight() / 2;
//條止的偏移量
int offset = (int) (getItemHeight() / 2 * 1.2);
centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);
centerDrawable.draw(canvas);
/**
* 中間選中的畫2條線
*
*/
//上方的線
canvas.drawLine(0,center-offset,getWidth(),center-offset,mPaint);
//下方的線
canvas.drawLine(0,center+offset,getWidth(),center+offset,mPaint);
}
前面要初始化畫筆
/**
* 初始化畫筆
*/
private void initPaint() {
mPaint = new Paint();
//鋸齒
mPaint.setAntiAlias(true);
//畫筆顏色
// mPaint.setColor(0xffebebeb);
//為了看出效果
mPaint.setColor(0xffEA2000);
//寬度
mPaint.setStrokeWidth(1f);
}
選擇中字體變大啦租,不選擇時變小
字體更改要更改Adapter中的設(shè)置--AbstractWheelTextAdapter
- 設(shè)置屬性
/**
* 當(dāng)前的索引
*/
private int currentIndex = 0;
/**
* 最大字體
*/
private static int maxsize = 24;
/**
* 最小字體
*/
private static int minsize = 14;
- 更改構(gòu)造方法
protected AbstractWheelTextAdapter(Context context) {
this(context, TEXT_VIEW_ITEM_RESOURCE);
}
/**
* Constructor
* @param context the current context
* @param itemResource the resource ID for a layout file containing a TextView to use when instantiating items views
*/
protected AbstractWheelTextAdapter(Context context, int itemResource) {
this(context, itemResource, NO_RESOURCE,0,maxsize,minsize);
}
/**
* Constructor
* @param context the current context
* @param itemResource the resource ID for a layout file containing a TextView to use when instantiating items views
* @param itemTextResource the resource ID for a text view in the item layout
*/
protected AbstractWheelTextAdapter(Context context, int itemResource, int itemTextResource,int currentIndex,int maxsize,int minsize) {
this.context = context;
itemResourceId = itemResource;
itemTextResourceId = itemTextResource;
/**
* 為屬性賦值
*/
this.currentIndex=currentIndex;
this.maxsize = maxsize;
this.minsize = minsize;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
這樣還是沒有效果的
更改AbstractWheelTextAdapter 中
public View getItem(int index, View convertView, ViewGroup parent) {
方法:
@Override
public View getItem(int index, View convertView, ViewGroup parent) {
if (index >= 0 && index < getItemsCount()) {
if (convertView == null) {
convertView = getView(itemResourceId, parent);
}
TextView textView = getTextView(convertView, itemTextResourceId);
if (textView != null) {
CharSequence text = getItemText(index);
if (text == null) {
text = "";
}
textView.setText(text);
/**
* 設(shè)置字體設(shè)置
*
*/
if(index==currentIndex){
textView.setTextSize(maxsize);
}else {
textView.setTextSize(minsize);
}
if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE) {
configureTextView(textView);
}
}
return convertView;
}
return null;
}
貌似只有第一個會變
- Adapter中把所有Item都存放起來
/**
* 存放Item容器
*/
private ArrayList<View> arrayList = new ArrayList<View>();
/**
* getter 方法對外使用
* @return
*/
public ArrayList<View> getTextViews() {
return arrayList;
}
- 在getItem方法中添加
@Override
public View getItem(int index, View convertView, ViewGroup parent) {
if (index >= 0 && index < getItemsCount()) {
if (convertView == null) {
convertView = getView(itemResourceId, parent);
}
TextView textView = getTextView(convertView, itemTextResourceId);
/**
* 添加到容器中
*/
if (!arrayList.contains(textView)) {
arrayList.add(textView);
}
if (textView != null) {
CharSequence text = getItemText(index);
if (text == null) {
text = "";
}
textView.setText(text);
/**
* 設(shè)置字體設(shè)置
*
*/
if(index==currentIndex){
textView.setTextSize(maxsize);
}else {
textView.setTextSize(minsize);
}
if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE) {
configureTextView(textView);
}
}
return convertView;
}
return null;
}
這樣貌似還是不行
- 對控件添加監(jiān)聽并設(shè)置字體
private void initListener() {
/**
* 滾動中改變
*/
mWheelView.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
//獲取當(dāng)前的索引
int currentItem = wheel.getCurrentItem();
/**
* 獲取內(nèi)容
*/
String text = (String) mBirthYearAdapter.getItemText(currentItem);
/**
* 設(shè)置字體
*
*/
setItemTextSize(text,mBirthYearAdapter);
// Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
/**
* 滾動完成改變
*/
mWheelView.addScrollingListener(new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) {
}
@Override
public void onScrollingFinished(WheelView wheel) {
//獲取當(dāng)前的索引
int currentItem = wheel.getCurrentItem();
/**
* 獲取內(nèi)容
*/
String text = (String) mBirthYearAdapter.getItemText(currentItem);
/**
* 設(shè)置字體
*
*/
setItemTextSize(text,mBirthYearAdapter);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
- 設(shè)置字體大小方法
/**
* 設(shè)置字體大小
* @param currentItemText
* @param birthYearAdapter
*/
private void setItemTextSize(String currentItemText,BirthYearAdapter birthYearAdapter){
//獲取所有的View
ArrayList<View> arrayLists = birthYearAdapter.getTextViews();
int size = arrayLists.size();
//當(dāng)前條目的內(nèi)容
String currentText;
for(int i = 0; i < size; i++) {
TextView textview = (TextView) arrayLists.get(i);
currentText = textview.getText().toString().trim();
if (currentItemText.equals(currentText)) {
textview.setTextSize(maxsize);
} else {
textview.setTextSize(minsize);
}
}
}
效果