2016-12-16 52 views
0

我有一個導航抽屜和一個生成產品列表的片段。問題是,當我點擊某個產品時,它會嘗試在基本活動中找到onClick方法,而不是在片段的java類中。我應該在基本活動中實現onClick方法還是有任何方法在fragment類中實現它?我應該在NavigationDrawer活動中實現onClick方法?

這是產品的佈局 「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; 
    } 
+1

請粘貼您的完整代碼。 –

+2

如果只有一個xml對象正在使用onClick方法,請避免使用xml中的onclick,並使用.java一側的setOnClickListener。原因是:onClick搜索鏈接到xml的java中的方法,而不是遍佈整個應用程序。順便說一句,發佈完整的代碼@BhavinShah說 –

+0

請確保您使用正確的LayoutInflater。 – Qamar

回答

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

你應該叫活動通過在你的片段類中調用它並通過已調用的item的itemIndex來採取行動的方法。

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

這不是關於片段之間的導航。在一個片段中,我有一個產品列表,當我點擊其中一個片段時,它看不到onClick方法,在片段的類中實現。 –

+0

爲此,您必須在該片段中使用listview.setOnItemClickListener,該片段具有產品列表 – Nainal

相關問題