在片段中使用搜索栏 android
Using seekbar in a fragment android
我已将片段附加到 activity。在该片段中,我正在监听对搜索栏所做的更改,但从未访问 'onProgressChanged' 方法。此外,当尝试修改也在片段中的 TextView 变量时,它不会修改它的值(TextView 变量应该显示搜索栏的值。
这是我使用的代码:
主要activity
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>
更改此行
return inflater.inflate(R.layout.fragment_nearby, container, false);
至
return rootView;
在你的 onCreateView
我已将片段附加到 activity。在该片段中,我正在监听对搜索栏所做的更改,但从未访问 'onProgressChanged' 方法。此外,当尝试修改也在片段中的 TextView 变量时,它不会修改它的值(TextView 变量应该显示搜索栏的值。
这是我使用的代码:
主要activity
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>
更改此行
return inflater.inflate(R.layout.fragment_nearby, container, false);
至
return rootView;
在你的 onCreateView