如何从另一个不是父项的活动中调用片段?

时间:2015-12-06 08:23:18

标签: java android android-activity fragment call

我有一个非父母的活动,我想在点击图片视图时调用该活动中的片段。所以我在我的图像视图中添加了onclickListner。我点击了图片视图,但是片段没有被调用。可能是什么问题?任何人都可以帮忙..

GoSend活动布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:id="@+id/LinearLayoutGoSend">

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/LinearContainer">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="15dp">

    <TextView
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/DriversNear"
        android:id="@+id/textView10"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp" />


    <fragment android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/map"
        tools:context=".GoSend"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_marginTop="10dp" />

    </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_weight="1.00"
                android:background="@android:color/white"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/From"
                    android:id="@+id/textView11"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="10dp" />

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="08dp"
                    android:id="@+id/imageView11"
                    android:background="@drawable/line2"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView10"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_place_black_48dp"
                        android:layout_marginTop="05dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText_from"
                        android:text="@string/Loc"
                        android:visibility="visible"
                        android:drawableEnd="@drawable/ic_chevron_right_black_24dp"
                        android:layout_marginEnd="10dp"
                        android:cursorVisible="false"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView12"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_description_black_48dp"
                        android:layout_marginTop="05dp" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/editText_from_details"
                        android:layout_marginEnd="10dp"
                        android:hint="Location details"
                        android:cursorVisible="true"/>


                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textView12"
                    android:layout_marginStart="48dp"
                    android:hint="House no./Floor/Landmark" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/LinearLayoutAdditionalContactFrom">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView13"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_person_black_48dp"
                        android:layout_marginTop="05dp"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/contactDetailsFrom"
                        android:layout_marginEnd="10dp"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                        android:text="@string/additionalContact"
                        android:layout_gravity="center_vertical"
                        android:layout_marginTop="05dp"
                        android:layout_marginStart="05dp" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/textView15"
                    android:layout_marginStart="48dp"
                    android:hint="House no./Floor/Landmark" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/LinearLayoutAdditionalContactTo">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:id="@+id/imageView18"
                        android:layout_marginStart="20dp"
                        android:background="@drawable/ic_person_black_48dp"
                        android:layout_marginTop="05dp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/contactDetailsTo"
                        android:layout_marginEnd="10dp"
                        android:text="@string/additionalContact"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:drawableEnd="@drawable/ic_expand_more_black_24dp"
                        android:layout_marginTop="05dp"
                        android:layout_marginStart="05dp" />
                </LinearLayout>
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="60dp"
                        android:layout_marginEnd="50dp"
                        android:layout_gravity="center"
                        android:visibility="gone"
                        android:id="@+id/LinearLayoutTo">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/editTex"
                            android:layout_weight="1"
                            android:hint="Name"
                            android:layout_gravity="center"
                            />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/editTe"
                            android:layout_weight="1"
                            android:hint="Phone"
                            android:layout_gravity="center"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="OR"
                            android:id="@+id/textView"
                            android:layout_gravity="center" />


        </LinearLayout>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView_next"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.42"
                android:background="@drawable/ic_chevron_right_black_48dp"
                />
        </LinearLayout>

    </ScrollView>
</LinearLayout>
</LinearLayout>

GoSend活动

public class GoSend extends AppCompatActivity {

private GoogleMap mMap;
private MarkerOptions markerOptions;
private LinearLayout ll;
private TextView additionalContactFrom;
private TextView additionalContactTo;
private LinearLayout linearLayoutFrom;
private LinearLayout linearLayoutTo;
private ImageView next;

private Toolbar toolbar;
private EditText editTextLocation;
private EditText edtxt_from;
private EditText edtxt_to;
private String stringAddress;
String stringAddress1;
String stringAddressTo;
EditText locdetailsFrom;
EditText locdetailsTo;
private Intent i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gosendlayout);

    setUI();

    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    }

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public void setUI() {

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("COURIER");
    setSupportActionBar(toolbar);
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    edtxt_from=(EditText)findViewById(R.id.editText_from);
    edtxt_to=(EditText)findViewById(R.id.editText_to);
    locdetailsFrom = (EditText) findViewById(R.id.editText_from_details);
    locdetailsTo = (EditText) findViewById(R.id.editText_to_details);
    additionalContactFrom = (TextView)findViewById(R.id.contactDetailsFrom);
    additionalContactTo = (TextView)findViewById(R.id.contactDetailsTo);
    linearLayoutFrom = (LinearLayout)findViewById(R.id.LinearLayoutFrom);
    linearLayoutTo = (LinearLayout)findViewById(R.id.LinearLayoutTo);
    next = (ImageView)findViewById(R.id.imageView_next);

    try {
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().
                    findFragmentById(R.id.map)).getMap();
        }
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMyLocationEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }



    edtxt_from.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             i=new Intent(getApplicationContext(),PickLocationActivity.class);
            startActivity(i);
        }
    });

    edtxt_to.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            i=new Intent(getApplicationContext(),PickLocationActivity.class);
            startActivity(i);
        }
    });


    additionalContactFrom.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(linearLayoutFrom.getVisibility() == View.GONE){

                linearLayoutFrom.setVisibility(View.VISIBLE);

            }else{
              linearLayoutFrom.setVisibility(View.GONE);

            }
        }
    });


    additionalContactTo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(linearLayoutTo.getVisibility() == View.GONE){

                linearLayoutTo.setVisibility(View.VISIBLE);

            }else{
              linearLayoutTo.setVisibility(View.GONE);

            }
        }
    });

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DetailsFragment fragment = new DetailsFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.LinearContainer, fragment).commit();

            Toast toast = Toast.makeText(getApplicationContext(),"Done",Toast.LENGTH_LONG);
            toast.show();

        }
    });
}

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first

}

}

DetailsFragment layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|top"
    android:background="@android:color/transparent">
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/Details"
            android:id="@+id/textView22"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="15dp"
            android:layout_marginStart="15dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/payment"
            android:id="@+id/textView24"
            android:layout_marginStart="15dp"
            android:layout_marginTop="15dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="08dp"
            android:id="@+id/imageView23"
            android:background="@drawable/line2"
            android:layout_marginTop="10dp" />


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:weightSum="1">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/imageView24"
                android:background="@drawable/coins49"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/price"
                android:id="@+id/textView25"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView26"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="190dp" />

            </LinearLayout>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="08dp"
                android:id="@+id/imageView26"
                android:background="@drawable/line2"
                android:layout_marginTop="05dp" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/imageView25"
            android:layout_marginStart="10dp"
            android:background="@drawable/currency13" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/total"
            android:id="@+id/textView27"
            android:layout_marginStart="10dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textView28"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="190dp" />

    </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="08dp"
            android:id="@+id/imageView27"
            android:background="@drawable/line2"
            android:layout_marginTop="05dp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@android:color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/imageView28"
                android:layout_marginStart="10dp"
                android:background="@drawable/credit101" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/paywith"
                android:id="@+id/textView29"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/textView30"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="180dp" />
        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView29"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/ic_chevron_right_black_48dp" />

</LinearLayout>

DetailsFragment

public class DetailsFragment extends Fragment {
private Toolbar toolbar;
public DetailsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_details, container, false);

    return view;
}

}

请帮助..

1 个答案:

答案 0 :(得分:0)

您的活动布局中没有android:id="@+id/LinearContainer,因此FragmentManager无法放置DetailsFragment