Mahagney Saleh
Mahagney Saleh

Reputation: 117

Where should I implement onClick method in a NavigationDrawer Activity?

I have a navigation drawer and a fragment that generates a list of products. The problem is that when I click on a product it tryes to find onClick method in base activity not in fragment's java class. Should I implement onClick method in the base activity or is there any way to implement it in fragment's class.

This is product's layout "item_order.xml":

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="orderClick"
        android:id="@+id/tOrder"
        android:padding="4dp">
....
</RelativeLayout>

Here is the base class:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waiter);

        //set fragment

        OrdersFragment fragment=new OrdersFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        OrdersFragment.wa=this;
}

Here is the fragment:

public class OrdersFragment extends Fragment {
    private Database db;
    private ArrayList<Order> orders=new ArrayList<Order>();
    private OrderAdapter adapter;
    private ListView listOrders;
    public static WaiterActivity wa;
    public OrdersFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

        // Inflate the layout for this fragment
        db = new Database();
        db.createNewOrderListener(this);
        ListView listOrders = (ListView) view.findViewById(R.id.ordersList);
        adapter = new OrderAdapter(this.getContext(), R.layout.item_order, orders);

        listOrders.setAdapter(adapter);



        return view;
    }

Upvotes: 0

Views: 168

Answers (3)

Avinash kumawat
Avinash kumawat

Reputation: 75

I think you are talking about list view item selection. Firstly remove onClick from you xml. Create a function orderClick in you base activity and Then in you fragment add onItemClick listener to your list view.

listOrders.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (getActivity() instanceof "your base activity"){
                    (("your base activity")getActivity()).orderClick();
                }
            }
        });

Upvotes: 1

Nainal
Nainal

Reputation: 1748

use setOnItemClickListener in your activity, and call the fragments from there only, as:-

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fragment fragment = null;
    FragmentManager fragmentManager = getSupportFragmentManager();
    switch (position) {
        case 0:
            fragment = new ProgramFragment();

            break;

        case 1:
            fragment = new FeedbackFragment();

            break;
        case 2:

             fragment = new ShareFragment();


            break;
        case 3:
            fragment = new LogOutFragment();

            break;

        default:
          fragment =null;
    }


    if (fragment != null) {

            fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit();


    }


    mDrawerList.setItemChecked(position, true);
    drawerLayout.closeDrawer(mDrawerList);
            }
        });

Upvotes: 0

Rez
Rez

Reputation: 500

You should call activity method for taking action by calling it in your fragment class and pass the itemIndex of item that has been called.

Upvotes: 0

Related Questions