如何启动片段Insted打开新的Activity,OnClick它打开新的Activity,有什么方法可以打开片段

时间:2017-02-18 05:02:31

标签: android

如何启动片段Insted打开新的Activity,OnClick它打开新的Activity,有什么方法可以打开片段。

@Override
protected void onPostExecute(List<MovieModel> result) {
    super.onPostExecute(result);

  final MovieAdapter adapter = new MovieAdapter( getActivity().getApplicationContext(), R.layout.rownew, result );
        ////  getApplicationContext()   // getActivity is added by me
        lvMovies.setAdapter(adapter);

        //set data to list
        lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              //  Toast.makeText(getActivity().getBaseContext(),parent.getItemIdAtPosition(position)+" is selected",Toast.LENGTH_LONG).show();

                MovieModel movieModel = (MovieModel) adapter.getItem(position);

                Intent intent = new Intent("sanjay.apackage.torcente.com.torcentemotors.productdesc");
                intent.putExtra("productimage", movieModel.getProduct_image());
                //sanjay //
                intent.putExtra("productname",movieModel.getProduct_name());
                intent.putExtra("productprice", movieModel.getProduct_price());
                intent.putExtra("productcolor", movieModel.getProduct_color());
                intent.putExtra("originalprice", movieModel.getOriginal_price());
                intent.putExtra("appdesc", movieModel.getApp_desc());

                startActivity(intent);

                /*
                Fragment fragment_productdesc = new fragment_productdesc();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.main_container, fragment_productdesc ); // give your fragment container id in first parameter
                transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
                transaction.commit();  */
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

我认为您希望将数据从活动发送到片段,因此请尝试使用此方法将活动中的数据发送到方法。

创建一个自定义片段,扩展片段put参数并初始化。

public class MyFragment extends Fragment {
private View rootView;
private int type;

public MyFragment() {
    // Required empty public constructor
}

public static MyFragment newInstance(int type, Object object) {
    MyFragment fragment = new MyFragment();
    Bundle args = new Bundle();
    args.putInt("type", type);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        type = getArguments().getInt(Key.TYPE);
    }
}

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_slider, container,false);
    return rootView;
}