Проблемы с событием Click ListView в TabHost

У меня проблема. Я разрабатываю приложение, которое использует макет ящика со ссылкой на этот пример https://developer.android.com/training/implementing-navigation/nav-drawer.html

Боковое меню работает корректно, но мой фрагмент (контент) загружает 3 вкладки и в каждой есть listview, проблема у меня в том, что метод 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>

person Brando T.    schedule 14.10.2015    source источник


Ответы (1)


В поисках ответов я нашел эту ссылку onListItemClick не работает для просмотра списка?, внести коррективы и теперь работает правильно.

person Brando T.    schedule 16.10.2015