0
我已將一個片段附加到一個活動。在片段中,我正在監聽對seekbar所做的更改,但「onProgressChanged」方法從未被訪問過。此外,當試圖修改同樣位於片段中的TextView變量時,它不會修改其值(TextView變量應顯示搜索欄的值。)在片段中使用seekbar android
這裏是我使用的代碼:
主要業務
public class NearbyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby);
// add map fragment
NearbyFragment nearbyFragment = (NearbyFragment) getSupportFragmentManager().findFragmentById(R.id.nearby_fragment);
if (nearbyFragment == null) {
nearbyFragment = NearbyFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container_bar, nearbyFragment)
.commit();
}
}
片段
public class NearbyFragment extends Fragment implements OnSeekBarChangeListener{
private TextView distance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_nearby, container, false);
final SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.seekBar);
distance = (TextView) rootView.findViewById(R.id.distance);
seekBar.setProgress(50);
distance.setText("ass");
seekBar.setOnSeekBarChangeListener(this);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_nearby, container, false);
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
distance.setText("" + i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
片段XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="241dp"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_marginStart="60dp"
android:layout_marginEnd="60dp" />
<TextView
android:text="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/distance"
android:gravity="center"/>
</LinearLayout>