假设我在viewpager中有两个片段,FragmentA和FragmentB。当我点击fragmentA中的按钮时,它应该能够在另一个fragmentB.so中添加textview,怎么可能....请帮帮我
class Myadpter extends FragmentPagerAdapter {
Fragment fragment =null;
public Myadpter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position==0){
fragment = new Post();
}
if(position==1){
fragment = new ActiveChat();
}
if(position==2){
fragment = new LastUsers();
}
if(position==3){
fragment = new Noname();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
答案 0 :(得分:1)
实现一个接口以在两个片段之间进行通信,视图寻呼机所在的类将是中间人
答案 1 :(得分:0)
正如其他用户已经说过的那样,实现接口是最佳选择。此链接Communicating with Other Fragments将更详细地解释如何实现您尝试执行的操作。希望这能解决你的问题。
答案 2 :(得分:0)
请执行以下操作:
片段A
public class FragmentB extends Fragment{
TextView textViewFragB;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.your_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textViewFragB= findViewById(R.id.textViewFragB);
}
public TextView getTextViewFragB(){
return textViewFragB;
}
<强> FragmentB 强>
public class TabControllerActivity extends AppCompatActivity implements FragmentA.OnButtonPressed{
MyAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
// Your Stuff
}
// Everytime the user clicks on the Button in FragmentA, this interface method gets triggered
@Override
public void onButtonPressed(TextView textViewFragA) {
FragmentB fragmentB = (FragmentB) adapter.getItem(1)/* Be careful here and get the right fragment,
otherwise the App will crash*/
// Since you got the TextView and not only the text inside of it,
// you can do whatever you want. Here for example we set the text like the textViewFragA.
//In a few words you turn the textViewFragB to the other one
fragmentB.getTextViewFragB().setText(textViewFragA.getText().toString());
}
}
<强>活动强>
var secheltLoc = new google.maps.LatLng(lat, lang);
myMapOptions = {
zoom: 13,
center: secheltLoc,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
icon: 'images/mpicon.png',
zoomControlOptions: {position:
google.maps.ControlPosition.LEFT_CENTER},mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']}
},
map = new google.maps.Map(document.getElementById("js_bigMap"), myMapOptions);
marker = new google.maps.Marker({ map: map, draggable: true,position: new google.maps.LatLng(lat, lang),visible: true,icon: 'images/mpicon.png',});
marker.setMap(map);
/*markerData contains latlang of locations*/
for(var i = 0 ; i < markerData.length; i++)
{
marker_ico = 'images/mpicon.png';
marker = new google.maps.Marker({map: map, draggable: true,position: markerData[i].latLng,visible: true,icon: marker_ico,});
overlay = new google.maps.OverlayView();
overlay.draw = function () {};
overlay.setMap(map);
google.maps.event.addListener(marker, 'click', function (e)
{
var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition());
console.log(point);
});
google.maps.event.addListener(marker, 'mouseover', function (e)
{
var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition());
console.log(point);
});
}/*loop*/
希望它会有所帮助