如何在活动中显示购物车图标?

时间:2017-09-25 05:46:08

标签: java android android-layout android-fragments

我制作了购物车应用程序并使用了导航抽屉,在此导航栏中我使用了购物车图标。

Check this shopping cart icon

现在,当我点击特定项目时。它在新的活动中开放,但它没有显示购物车图标,那么它将如何显示?那么,我看到购物车中的商品了吗?

Unable to see shopping cart icon

ItemDetailsActivity。 java(这是这个图像的.java文件,我无法看到购物车图标,所以我无法看到购物车中添加了多少商品)

public class ItemDetailsActivity extends AppCompatActivity {
    int imagePosition;
    String stringImageUri;
    TextView textViewshare, textViewmap;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_details);
        SimpleDraweeView mImageView = (SimpleDraweeView)findViewById(R.id.image1);
        TextView textViewAddToCart = (TextView)findViewById(R.id.text_action_bottom1);
        TextView textViewBuyNow = (TextView)findViewById(R.id.text_action_bottom2);
        textViewshare = (TextView) findViewById(R.id.text_action1);
        textViewmap = (TextView) findViewById(R.id.text_action3);

        TextView textViewBuyNowwithpayment = (TextView) findViewById(R.id.text_action_bottom2);



        //Getting image uri from previous screen
        if (getIntent() != null) {
            stringImageUri = getIntent().getStringExtra(ImageListFragment.STRING_IMAGE_URI);
            imagePosition = getIntent().getIntExtra(ImageListFragment.STRING_IMAGE_URI,0);
        }

        Uri uri = Uri.parse(stringImageUri);
        mImageView.setImageURI(uri);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(ItemDetailsActivity.this, ViewPagerActivity.class);
                    intent.putExtra("position", imagePosition);
                    startActivity(intent);

            }
        });

        textViewAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
                imageUrlUtils.addCartListImageUri(stringImageUri);
                Toast.makeText(ItemDetailsActivity.this,"Item added to cart.",Toast.LENGTH_SHORT).show();
                MainActivity.notificationCountCart++;
                NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
            }
        });


        textViewBuyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageUrlUtils imageUrlUtils = new ImageUrlUtils();
                imageUrlUtils.addCartListImageUri(stringImageUri);
                MainActivity.notificationCountCart++;
                NotificationCountSetClass.setNotifyCount(MainActivity.notificationCountCart);
                startActivity(new Intent(ItemDetailsActivity.this, CartListActivity.class));


            }
        });
//         payment.setOnClickListener(new View.OnClickListener() {
//             @Override
//             public void onClick(View v) {
//                 Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
//                 startActivity(i);
//             }
//         });

        textViewBuyNowwithpayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ItemDetailsActivity.this, PayPalCheckoutActivity.class);
                startActivity(i);
            }
        });
        textViewshare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
                intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
                startActivity(intent);

            }

        });
        textViewmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(ItemDetailsActivity.this, Placepicker.class);
                startActivity(in);
            }
        });








    }


    }

activity_item_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_item_details"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10"
    tools:context="com.codeexpertise.eshop.product.ItemDetailsActivity">


    <ScrollView android:id="@+id/scrollbar"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="9.5"
              android:scrollbars="none"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin">
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <com.facebook.drawee.view.SimpleDraweeView xmlns:fresco="http://schemas.android.com/apk/res-auto"
                                                   android:id="@+id/image1"
                                                   android:layout_width="match_parent"
                                                   android:layout_height="200.0dp"
                                                   fresco:placeholderImage="@color/stay_color" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:orientation="vertical">
                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Denim Shirt"
                          android:textSize="16dp"
                          android:textColor="@color/gen_black"/>
                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginTop="8dp"
                          android:text="Rs. 1,979"
                          android:textSize="20dp"
                          android:textColor="@color/gen_black"
                          />
                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="FREE Delivery"
                          android:textSize="12dp"
                          android:layout_marginTop="4dp"/>
            </LinearLayout>


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

                <TextView android:id="@+id/text_ratings"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:background="@color/green_light"
                          android:paddingTop="2dp"
                          android:paddingBottom="2dp"
                          android:paddingLeft="6dp"
                          android:paddingRight="6dp"
                          android:text="4.3 *"
                          android:textSize="12dp"
                          android:textColor="@color/gen_white"
                          android:textStyle="bold"/>

                <TextView android:id="@+id/text_ratings_reviews"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="12dp"
                          android:text="50 ratings \u0026 15 reviews"
                          android:textSize="12dp"/>

            </LinearLayout>/
        <View android:layout_width="match_parent"
              android:layout_height="@dimen/view_width_small"
              android:background="@color/grey_light"
              android:layout_marginTop="8dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:orientation="horizontal"
            android:layout_marginTop="8dp"
            android:weightSum="3">

            <LinearLayout android:id="@+id/layout_action1"
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="1"
                          android:gravity="center"
                          android:orientation="horizontal"
                          android:weightSum="2">
                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:src="@drawable/ic_share_black_18dp"/>
                <TextView android:id="@+id/text_action1"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:layout_marginLeft="8dp"
                          android:text="Share"
                     android:showAsAction="ifRoom"
                          android:textSize="12dp"
                          android:textColor="@color/gen_black"
                          android:gravity="left"
                    android:actionProviderClass=
                        "android.widget.ShareActionProvider"/>
            </LinearLayout>

            <View android:layout_width="@dimen/view_width_small"
                  android:layout_height="match_parent"
                  android:background="@color/grey_light"/>

            <LinearLayout android:id="@+id/layout_action2"
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="1"
                          android:gravity="center"
                          android:orientation="horizontal"
                          android:weightSum="2">
                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:src="@drawable/ic_filter_none_black_18dp"/>
                <TextView android:id="@+id/text_action2"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:layout_marginLeft="8dp"
                          android:text="Similar"
                          android:textSize="12dp"
                          android:textColor="@color/gen_black"
                          android:gravity="left"/>
            </LinearLayout>

            <View android:layout_width="@dimen/view_width_small"
                  android:layout_height="match_parent"
                  android:background="@color/grey_light"/>

            <LinearLayout android:id="@+id/layout_action3"
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="1"
                          android:gravity="center"
                          android:orientation="horizontal"
                          android:weightSum="2">
                <ImageView android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:src="@drawable/ic_favorite_border_black_18dp"/>
                <TextView android:id="@+id/text_action3"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:name="com.google.android.gms.maps.MapFragment"
                          android:layout_marginLeft="8dp"
                          android:text="Store Locator"
                          android:textSize="12dp"
                          android:textColor="@color/gen_black"
                          android:gravity="left"/>
            </LinearLayout>
        </LinearLayout>
        <View android:layout_width="match_parent"
              android:layout_height="@dimen/view_width_small"
              android:background="@color/grey_light"
              android:layout_marginTop="8dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Details"
                      android:textSize="16dp"
                      android:textColor="@color/gen_black"/>
            <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="8dp"
                      android:text="\u2022 Regular fit, full sleeve"
                      android:textSize="12dp"
                      android:textColor="@color/gen_black"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="\u2022 Fabric: Cotton"
                android:textSize="12dp"
                android:textColor="@color/gen_black"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="\u2022 Pattern: printed"
                android:textSize="12dp"
                android:textColor="@color/gen_black"/>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:orientation="horizontal"
        android:weightSum="2"
        android:elevation="30dp"
        android:background="@color/gen_black">
        <TextView android:id="@+id/text_action_bottom1"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:background="@color/gen_white"
                  android:text="ADD TO CART"
                  android:textSize="14dp"
                  android:textColor="@color/gen_black"
                  android:textStyle="bold"
                  android:gravity="center"/>

        <TextView android:id="@+id/text_action_bottom2"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_weight="1"
                  android:background="#4dc3ff"
                  android:text="BUY NOW"
                  android:textSize="14dp"
                  android:textColor="@color/gen_white"
                  android:textStyle="bold"
                  android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

Main.xml(在这个xml中我使用导航抽屉栏中显示的菜单项)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_search_white_24dp"
          app:showAsAction="collapseActionView|ifRoom"
          app:actionViewClass="android.support.v7.widget.SearchView"/>
    <item android:id="@+id/action_notifications"
          android:title="@string/action_notifications"
          app:showAsAction="always"
          android:icon="@drawable/ic_notifications_white_24dp"/>
    <item android:id="@+id/action_cart"
          android:title="@string/action_cart"
          app:showAsAction="always"
          android:icon="@drawable/ic_menu_notifications"/>
</menu>

1 个答案:

答案 0 :(得分:3)

请在 ItemDetailsActivity 中覆盖此方法以显示菜单

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

ItemDetailsActivity 中使用此方法来处理点击事件

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.action_cart:
        dosomething();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
相关问题