繁体   English   中英

我如何将适配器传递给片段,就像意图一样

[英]How can i pass adapter to fragment like in intent

我是Android的新手,我试图从点击后点击使用意图调用我的MapFragment是我的代码

以下是适配器代码:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  final BusInfo info = getItem(position);
  View view = LayoutInflater.from(context).inflate(R.layout.bus_only_list,null);
  TextView busname;
  busname = (TextView) view.findViewById(R.id.busname);
  busname.setText(info.name);
  view.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       pref = context.getSharedPreferences("busInfo",Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = pref.edit();
       editor.putString("bus_name",info.name);
       editor.commit();

       Intent intent = new Intent(context, MapsFragment.class);
       intent.putExtra("name",info.name);
       context.startActivity(intent);>     

     }
  });

  return view;
}

我想使用intent传递给mapfragment,但它重定向到MainActivity而不是MapFragment 如何停止转移到MainActivity?

谢谢。

将值传递给Fragment的常见模式是使用newInstance方法。 在此方法中,您可以将Argument设置为fragment作为发送值的方法。

首先,创建newInstance方法:

public class YourFragment extends Fragment {
  ...

  // Creates a new fragment with bus_name
  public static YourFragment newInstance(String busName) {
    YourFragment yourFragment = new YourFragment();
    Bundle args = new Bundle();
    args.putString("bus_name", busName);
    yourFragment.setArguments(args);
    return yourFragment;
  }

  ...
}

然后你可以获得onCreate的值:

public class YourFragment extends Fragment {
  ...

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the value from arguments
    String busName = getArguments().getString("bus_name", "");  
  }

  ...
}

您可以使用以下内容将活动的值设置为片段:

FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
YourFragment yourFragment = YourFragment.newInstance("bus_name_value");
fragTransaction.replace(R.id.fragment_place_holder, yourFragment);
fragTransaction.commit();

您可以使用上面的代码在Fragment初始化中发送值。

如果要将值设置为已实例化的片段,可以创建方法然后调用方法来设置值:

public class YourFragment extends Fragment {
  ...

  public setBusName(String busName) {
    // set the bus name to your fragment.
  }

  ...
}

现在,在活动中,您可以使用以下命令调用它:

// R.id.yourFragment is the id of fragment in xml
YourFragment yourFragment = (YourFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.yourFragment);
yourFragment.setBusName("bus_name_value");

您无法将意图传递给Fragment。 请尝试使用Bundle。

Bundle bundle = new Bundle();

bundle.putString("name", info.name);

mapFragment.setArguments(bundle)

在你的片段(MapsFragment)中获取Bundle,如下所示:

Bundle bundle = this.getArguments();

if(bundle != null){
   String infoName = bundle.getString("name");
}

正如之前提到的那样:1。使用回调或只是对你的上下文进行强制转换(你的活动必须自己处理更改的碎片)。 2.要更改片段,请使用活动的FragmentManager - intent用于启动另一个活动。

碎片文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM