我应该在NavigationDrawer活动中的哪个位置实现onClick方法?

时间:2016-12-16 10:40:16

标签: android android-fragments navigation-drawer

我有一个导航抽屉和一个生成产品列表的片段。问题是,当我点击产品时,它试图在基本活动中找到onClick方法,而不是在fragment的java类中。我应该在基本活动中实现onClick方法,还是有任何方法可以在fragment的类中实现它。

这是产品的布局“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>

这是基类:

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;
}

这是片段:

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;
    }

3 个答案:

答案 0 :(得分:1)

我认为你在谈论列表视图项目选择。 首先从你的xml中删除onClick。创建一个函数orderClick在你的基本活动中然后在你的片段中添加onItemClick监听器到你的列表视图。

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();
                }
            }
        });

答案 1 :(得分:0)

您应该通过在片段类中调用它来调用activity方法来执行操作,并传递已调用的itemIndex。

答案 2 :(得分:0)

在您的活动中使用setOnItemClickListener,并仅从那里调用片段,如下所示: -

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);
            }
        });