2015-10-14 47 views
0

我遇到問題。 我正在開發,使用抽屜佈局參照這個例子https://developer.android.com/training/implementing-navigation/nav-drawer.htmlTabHost中ListView的Click事件的問題

橫向菜單正常工作的應用程序,但我的片段(內容)負載3周的選項卡和每個都具有一個列表視圖,問題,我已是,該方法onItemClick(setOnItemClickListener)不起作用,即選擇一個項目沒有解決任何問題。任何想法?。謝謝。!!

PS:對不起我的英文不好

PrincipalActivity.java

public class PrincipalActivity extends AppCompatActivity { 

private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private String[] mLeftPanel; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_principal); 
    initUI(); 
} 

private void initUI(){ 
    mLeftPanel = getResources().getStringArray(R.array.menu_array); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.dwrMain); 
    mDrawerList = (ListView) findViewById(R.id.leftPanel); 

    ArrayList<DrawerOption> items = new ArrayList<DrawerOption>(); 
    items.add(new DrawerOption(R.drawable.ic_plan_visit,mLeftPanel[0])); 
    items.add(new DrawerOption(R.drawable.ic_clients,mLeftPanel[1])); 
    items.add(new DrawerOption(R.drawable.ic_search,mLeftPanel[2])); 
    items.add(new DrawerOption(R.drawable.ic_sinchronize,mLeftPanel[3])); 
    items.add(new DrawerOption(R.drawable.ic_configure,mLeftPanel[4])); 
    items.add(new DrawerOption(R.drawable.ic_logout, mLeftPanel[5])); 
    mDrawerLayout.setDrawerShadow(R.color.listViewBackground, GravityCompat.START); 
    mDrawerList.setAdapter(new DrawerOptionAdapter(this, items)); 

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      if (position == 0) { 
       fragmentPlanVisit(position); 
       getSupportActionBar().setTitle(mLeftPanel[0]); 
      } 
      ...... 

      mDrawerLayout.closeDrawers(); 
     } 
    }); 

    ...... 

}  

public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

public boolean onOptionsItemSelected(MenuItem menuItem){ 
    int id = menuItem.getItemId(); 
    if (id == android.R.id.home){ 
     if (mDrawerLayout.isDrawerOpen(mDrawerList)){ 
      mDrawerLayout.closeDrawers(); 
     }else{ 
      mDrawerLayout.openDrawer(mDrawerList); 
     } 
    } 
    return super.onOptionsItemSelected(menuItem); 
} 

private void fragmentPlanVisit(int position){ 
    Fragment fragment = new PlanVisitActivity(); 
    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_fragment, fragment) 
       .commit(); 
     mDrawerList.setItemChecked(position, true); 
    } 
} 
} 

activity_principal.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/dwrMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<FrameLayout 
    android:id="@+id/content_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/ContentBackground"/> 

<ListView 
    android:id="@+id/leftPanel" 
    android:layout_width="250dp" 
    android:layout_height="match_parent" 
    android:choiceMode="singleChoice" 
    android:layout_gravity="start" 
    android:divider="@color/divideColorMenuDrawer" 
    android:dividerHeight="1dp" 
    android:background="@color/listViewBackground" 
    android:layout_weight="1"/> 

</android.support.v4.widget.DrawerLayout> 

PlanVisitActivity.java

public class PlanVisitActivity extends Fragment { 

private TabHost tabHost; 
private ListView lstPlanVisitCurse; 
private ListView lstPlanVisitDay; 
private ListView lstPlanVisitFuture; 
private ArrayList<PlanVisits> outLstPlanVisits = new ArrayList<PlanVisits>(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.activity_planvisit, container, false); 

    lstPlanVisitCurse = (ListView)(view.findViewById(R.id.lstPlanVisit_Tab1)); 
    lstPlanVisitCurse.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Log.i("PlanVisitActivity", String.valueOf(i)); 
      ..... 
      ..... 
     } 
    }); 

    ...... 

    tabHost = (TabHost)(view.findViewById(R.id.tabHost)); 
    tabHost.setup(); 

    TabHost.TabSpec spec = tabHost.newTabSpec(getResources().getString(R.string.tab1)); 
    spec.setIndicator(getResources().getString(R.string.tab1)); 
    spec.setContent(R.id.tab1); 
    tabHost.addTab(spec); 

    spec = tabHost.newTabSpec(getResources().getString(R.string.tab2)); 
    spec.setIndicator(getResources().getString(R.string.tab2)); 
    spec.setContent(R.id.tab2); 
    tabHost.addTab(spec); 

    spec = tabHost.newTabSpec(getResources().getString(R.string.tab3)); 
    spec.setIndicator(getResources().getString(R.string.tab3)); 
    spec.setContent(R.id.tab3); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(0); 
    getListPlanVisit(); 

    return view; 
} 

private void getListPlanVisit() { 

    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String s) { 
      int tab = tabHost.getCurrentTab(); 
      try { 
       if (tab == 0) { 
        final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitCurse, tab); 
        tk.execute(); 
        outLstPlanVisits = (ArrayList<PlanVisits>) tk.get(); 
       } else if (tab == 1) { 
        final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitDay, tab); 
        tk.execute(); 
        outLstPlanVisits = (ArrayList<PlanVisits>) tk.get(); 
       } else if (tab == 2) { 
        final PlanVisitsTask tk = new PlanVisitsTask(getActivity().getWindow().getContext(), lstPlanVisitFuture, tab); 
        tk.execute(); 
        outLstPlanVisits = (ArrayList<PlanVisits>) tk.get(); 
       } 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
} 

activity_planvisit.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/ContentBackground" 
    android:focusable="false" 
    android:focusableInTouchMode="false"> 

<TabHost 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tabHost" 
    android:layout_gravity="center_horizontal"> 

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary"></TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

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

       <ListView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/lstPlanVisit_Tab1" 
        android:divider="@color/divideColorlistView" 
        android:dividerHeight="1dp" 
        android:background="@color/listViewBackground" 
        android:choiceMode="singleChoice"/> 
      </LinearLayout> 

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

       <ListView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/lstPlanVisit_Tab2" 
        android:divider="@color/divideColorlistView" 
        android:dividerHeight="1dp" 
        android:background="@color/listViewBackground" 
        android:choiceMode="singleChoice" /> 
      </LinearLayout> 

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

       <ListView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/lstPlanVisit_Tab3" 
        android:divider="@color/divideColorlistView" 
        android:dividerHeight="1dp" 
        android:background="@color/listViewBackground" 
        android:choiceMode="singleChoice" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 
</LinearLayout> 

回答