i have multi Tab with ListView in application and i have an DrawerLayout inside that. but i can not do along that with other layouts. i'm try to use this solution for resolve problems but i can not do that.

I think My appliaction have multi Tab with ListView and that solution does not work for me. enter image description here

My Activity.(create Tab with ListView):

    public class DashboardActivity extends ActionBarActivity {

        private String[] mPlanetTitles;
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
        private Context context;
        public ReceivedSMS dataReceived;
        private ActionBarDrawerToggle mDrawerToggle;
        String button_text= "Application";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            context = getBaseContext();

            final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);

            /** Creating ANDROID Tab */
            ActionBar.Tab tab = actionBar.newTab()
                    .setText(R.string.sent_sms)
                    .setTabListener(new CustomTabListener<AppleFragment>(this, "apple", AppleFragment.class));

            actionBar.addTab(tab);

            /** Creating ReceivedSMSFragment Tab */
            tab = actionBar.newTab()
                    .setText(R.string.received_sms)
                    .setTabListener(new CustomTabListener<ReceivedSMSFragment>(this, "received_sms", ReceivedSMSFragment.class));

            actionBar.addTab(tab,true);

            mPlanetTitles = getResources().getStringArray(R.array.rightMenuItems);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            // Set the adapter for the list view
            mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, mPlanetTitles));
            // Set the list's click listener
            mDrawerList.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int post, long arg3) {
                }
            });

            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.drawable.ic_drawer, R.string.drawer_open,
                    R.string.drawer_close) {

                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(button_text);
                    supportInvalidateOptionsMenu(); // creates call to
                }

                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getSupportActionBar().setTitle("Opend");
                    supportInvalidateOptionsMenu(); // creates call to
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {

            boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);

            mDrawerToggle.syncState();
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

    }

TabListener class:

public class CustomTabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public CustomTabListener(Activity activity, String tag, Class<T> clz){
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
        if(mFragment==null){
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content,mFragment, mTag);
        }else{
            ft.attach(mFragment);
        }            }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
        if(mFragment!=null)
            ft.detach(mFragment);
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {

    }
}

AppleListViewItems Fragment class:

public class AppleFragment extends ListFragment implements AbsListView.OnScrollListener {

    /** An array of items to display in ArrayList */
    String android_versions[] = new String[]{
            "Jelly Bean",
            "IceCream Sandwich",
            "HoneyComb",
            "Ginger Bread",
            "Jelly Bean",
            "IceCream Sandwich",
            "HoneyComb",
            "Ginger Bread",
            "Jelly Bean",
            "IceCream Sandwich",
            "HoneyComb",
            "Ginger Bread",
            "Froyo"
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, android_versions);

        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long thisID) {
        super.onListItemClick(l, v, position, thisID);
    }
}

Related posts

Recent Viewed