导航抽屉里面的按钮 - Android应用程序

时间:2015-08-15 20:15:54

标签: android navigation-drawer

我只是使用导航抽屉编写应用程序。我没有在导航抽屉里面使用listview,我使用了LinearLayout和一个按钮。但是当我点击它时,它没有做任何事情。

XML:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/order_summary"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@color/background_material_light"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:background="@color/base">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="30dp">

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

                    <ImageView
                        android:id="@+id/imgSummary"
                        android:layout_width="75dp"
                        android:layout_height="75dp"
                        android:background="@color/actionbar_text"
                        android:paddingBottom="3px"
                        android:paddingLeft="3px"
                        android:paddingRight="3px"
                        android:paddingTop="3px"
                        android:scaleType="fitXY"
                        android:src="@drawable/order_summary" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="10dp"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/tvLabelTotal"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Total Pesanan"
                            android:textColor="@color/actionbar_text"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/tvTotalHarga"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Rp125.000"
                            android:textColor="@color/actionbar_text"
                            android:textSize="30dp" />

                        <TextView
                            android:id="@+id/tvTotalItem"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="14 Item"
                            android:textColor="@color/actionbar_text"
                            android:textSize="16dp" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="-25dp"
            android:gravity="right">

            <Button
                android:id="@+id/btnCloseOrder"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/circle_button"
                android:gravity="center_vertical|center_horizontal"
                android:text="OK"
                android:textColor="#fff" />
        </RelativeLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true">

            <LinearLayout
                android:id="@+id/listMenu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></LinearLayout>
        </ScrollView>


    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v4.view.ViewPager>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

Java代码:

package com.jajanankampoeng.order;

import android.content.res.Configuration;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.jajanankampoeng.db.helper.DatabaseHelper;
import com.jajanankampoeng.db.helper.DummyData;
import com.jajanankampoeng.db.model.Item;
import com.jajanankampoeng.db.model.ItemCategory;
import com.jajanankampoeng.db.model.SelectedItem;
import com.jajanankampoeng.order.adapter.TabsPagerAdapter;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements
        ActionBar.TabListener {

    private DrawerLayout mDrawerLayout;
    private LinearLayout mOrderSummary;
    private ActionBarDrawerToggle mDrawerToggle;
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;

    // Database Helper
    private DatabaseHelper db;
    private DummyData dummyData;

    //Selected Item
    private ArrayList<SelectedItem> selectedItems = new ArrayList<SelectedItem>();

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

        if(savedInstanceState != null){
            selectedItems = savedInstanceState.getParcelableArrayList("SelectedItems");
        }
        summarizeOrders();

        actionBar = getSupportActionBar();
        db = new DatabaseHelper(getApplicationContext());
        //dummyData = new DummyData(getApplicationContext());
        //dummyData.generateData();

        List<ItemCategory> allItemCategories = db.getAllItemCategories();

        for (ItemCategory ic : allItemCategories) {
            actionBar.addTab(actionBar.newTab().setText(ic.getItem_category_name())
                    .setTabListener(this));
        }
        db.closeDB();

        viewPager = (ViewPager) findViewById(R.id.pager);
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), allItemCategories.size());
        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mOrderSummary = (LinearLayout) findViewById(R.id.order_summary);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        Button btnCloseOrder = (Button)findViewById(R.id.btnCloseOrder);
        btnCloseOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Click", "You click me....");
            }
        });

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

    }

    public void summarizeOrders() {
        if (selectedItems.size() > 0) {
            LinearLayout listOrders = (LinearLayout) findViewById(R.id.listMenu);
            listOrders.removeAllViews();
            for (SelectedItem item : selectedItems) {
                final TableLayout itemContainer = new TableLayout(this.getApplicationContext());
                itemContainer.setPadding(50, 30, 10, 30);
                itemContainer.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                itemContainer.setStretchAllColumns(true);
                final TableRow trItemContainer = new TableRow(this.getApplicationContext());
                trItemContainer.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT));

                TextView tvItem = new TextView(this.getApplicationContext());
                tvItem.setText(item.getItem_name());
                tvItem.setTag(item.getItem_id());
                tvItem.setWidth(500);
                tvItem.setTextSize(14);
                tvItem.setTextColor(Color.parseColor("#81ee95"));
                final TextView tvItemQuantity = new TextView(this.getApplicationContext());
                tvItemQuantity.setText(String.valueOf(item.getQuantity()));
                tvItemQuantity.setWidth(50);
                tvItemQuantity.setGravity(Gravity.RIGHT);
                tvItemQuantity.setPadding(0, 0, 20, 0);
                tvItemQuantity.setTextColor(Color.parseColor("#1c803a"));
                ImageView btnMinus = new ImageView(this.getApplicationContext());
                btnMinus.setLayoutParams(new TableRow.LayoutParams(60, 60));
                btnMinus.setImageResource(R.drawable.btn_down);
                btnMinus.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                btnMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Integer.parseInt(tvItemQuantity.getText().toString()) > 0) {
                            int value = Integer.parseInt(tvItemQuantity.getText().toString()) - 1;
                            if (value == 0) {
                                itemContainer.removeView(trItemContainer);
                                return;
                            }
                            tvItemQuantity.setText(value);
                        }
                    }
                });
                ImageView btnPlus = new ImageView(this.getApplicationContext());
                btnPlus.setLayoutParams(new TableRow.LayoutParams(60, 60));
                btnPlus.setImageResource(R.drawable.btn_up);
                btnPlus.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                btnPlus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int value = Integer.parseInt(tvItemQuantity.getText().toString()) + 1;
                        tvItemQuantity.setText(value);
                    }
                });
                trItemContainer.addView(tvItem);
                trItemContainer.addView(btnMinus);
                trItemContainer.addView(tvItemQuantity);
                trItemContainer.addView(btnPlus);
                itemContainer.addView(trItemContainer);
                listOrders.addView(itemContainer);
            }
        }
    }

    public List<SelectedItem> getSelectedItems() {
        return this.selectedItems;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.icon) {
//            Log.d("ini", "gila");
//            return true;
//        }

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(Gravity.LEFT);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    private class DrawerItemClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            onDrawerItemClick(v);
        }
    }

    private void onDrawerItemClick(View v){
        switch (v.getId()){
            case R.id.btnCloseOrder:
                Toast.makeText(v.getContext(), "Helo Close", Toast.LENGTH_LONG).show();
                break;
        }
    }

    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelableArrayList("SelectedItems", selectedItems);
    }
}

我试图点击btnCloseOrder ...但它没有显示日志....

0 个答案:

没有答案