我有三种类别,我想从左到右滑动选择每种类型,如滑动选择器。任何帮助都会被赞赏
答案 0 :(得分:2)
使用搜索栏来满足此要求。
xml文件中的创建搜索栏。
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
使用setOnSeekBarChangeListener监听搜索中的更改,
SeekBar skbarVisibleWithin = (SeekBar) findViewById(R.id.seekbar);
skbarVisibleWithin.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
/**
* if the progress is less than 25 than we should set
* the progress to 0, else if the progress is less than
* 75 then we should set the progress as 50 else set the
* progress as 100;
**/
int progress = seekBar.getProgress();
if (progress < 25) {
seekBar.setProgress(PROGRESS_STAGE_1);
}
else if (progress < 75) {
seekBar.setProgress(PROGRESS_STAGE_2);
}
else {
seekBar.setProgress(PROGRESS_STAGE_3);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress == PROGRESS_STAGE_1) {
//TODO:
}
else if (progress == PROGRESS_STAGE_2) {
//TODO:
}
else if (progress == PROGRESS_STAGE_3) {
//TODO:
}
}
});
在onProgessChanged中,手动设置progess。
答案 1 :(得分:0)
在Hardik4560代码的帮助下,我定制了他的代码并创建了一个搜索选择器 我正在分享这段代码可能会帮助别人知道更好的解决方案请更新.....
寻求栏的Xml
代码
mainSeekbar = (SeekBar) findViewById(R.id.mainSeekBar);
mainSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO something fancy here
int progress = seekBar.getProgress();
if (progress > 0 && progress < 40) {
if (seekBar.getProgress() > 20) {
progress = PROGRESS_STAGE_2;
seekBar.setProgress(PROGRESS_STAGE_2);
} else {
progress = PROGRESS_STAGE_1;
seekBar.setProgress(PROGRESS_STAGE_1);
}
} else if (progress > 40 && progress < 70) {
if (seekBar.getProgress() > 55) {
progress = PROGRESS_STAGE_3;
seekBar.setProgress(PROGRESS_STAGE_3);
} else {
progress = PROGRESS_STAGE_2;
seekBar.setProgress(PROGRESS_STAGE_2);
}
} else if (progress > 70 && progress < 100) {
if (seekBar.getProgress() > 86) {
progress = PROGRESS_STAGE_4;
seekBar.setProgress(PROGRESS_STAGE_4);
} else {
progress = PROGRESS_STAGE_3;
seekBar.setProgress(PROGRESS_STAGE_3);
}
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO something fancy here
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// progressTextView.setText(String.valueOf(progress) + '%');
if (progress == PROGRESS_STAGE_1) {
// TODO:
Toast.makeText(getApplicationContext(), "type 1",
Toast.LENGTH_SHORT).show();
} else if (progress == PROGRESS_STAGE_2) {
// TODO:
Toast.makeText(getApplicationContext(), "type 2",
Toast.LENGTH_SHORT).show();
} else if (progress == PROGRESS_STAGE_3) {
// TODO:
Toast.makeText(getApplicationContext(), "type 3",
Toast.LENGTH_SHORT).show();
} else if (progress == PROGRESS_STAGE_4) {
Toast.makeText(getApplicationContext(), "type 4",
Toast.LENGTH_SHORT).show();
}
}
});
SeekBar自定义背景
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="@drawable/back_seekbar" />
</item>
<!-- <item android:id="@android:id/progress">
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:filter="false"
android:dither="false"
android:gravity="left"
android:src="@drawable/back_progress"
android:tileMode="repeat" />
</item> -->
</layer-list>