Hello all,

In my application i have a SlidingTabLayout, each tab containing a 
listview. when i go through the tabs several times the items are added in 
the listview each time. how can i prevent this behaviour?

the second question

On each tab i want to add a second fragment that will hold some controls, 
each fragment has to be linked with a list item from the list view, when i 
switch the tab the first list item has to be selected to display the 
corresponding fragment. How can i achieve this behavior?

my main activity
public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    SlidingTabLayout slidingTabLayout;
    CharSequence Titles[] = {"Basic", "Sensors", "USB"};
    int Numboftabs = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Creating The Toolbar and setting it as the Toolbar for the 
activity
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        // Creating The ViewPagerAdapter and Passing Fragment Manager, 
Titles for the Tabs and Number Of Tabs.
        viewPagerAdapter = new 
ViewPagerAdapter(getSupportFragmentManager(), Titles);
        // Assigning ViewPager View and setting the adapter
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(viewPagerAdapter);
        // Assiging the Sliding Tab Layout View
        slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
        slidingTabLayout.setDistributeEvenly(true); // To make the Tabs 
Fixed set this true, This makes the tabs Space Evenly in Available width
        // Setting Custom Color for the Scroll bar indicator of the Tab View
        slidingTabLayout.setCustomTabColorizer(new 
SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });
        // Setting the ViewPager For the SlidingTabsLayout
        slidingTabLayout.setViewPager(viewPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

my view pager adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which 
are Going to be passed when ViewPagerAdapter is created
    int NumbOfTabs; // Store the number of tabs, this will also be passed 
when the ViewPagerAdapter is created
    FragmentManager fragmentManager;
    private static final int tabIcons[] = {R.drawable.hand, 
R.drawable.sensors, R.drawable.fastobjects, R.drawable.gears, 
R.drawable.usb};
    // Build a Constructor and assign the passed Values to appropriate 
values in the class
    public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[]) {
        super(fm);
        this.fragmentManager = fm;
        this.Titles = mTitles;
        this.NumbOfTabs = mTitles.length;
    }
    //This method return the fragment for the every position in the View 
Pager
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = 
fragmentManager.findFragmentByTag("android:switcher:" + R.id.pager + ":" + 
position);
        if (fragment == null) {
            Bundle arguments = new Bundle();
            arguments.putInt("position", position);
            arguments.putString("frag","android:switcher:" + R.id.pager + 
":" + position);
            fragment = new Tab_Modes();
            fragment.setArguments(arguments);
            return fragment;
         }
        else
            return null;

    }
    // This method return the titles for the Tabs in the Tab Strip
    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }
    // This method return the Number of tabs for the tabs Strip
    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}


and my listview creator

public class Tab_Modes extends ListFragment {
    private ArrayList<NameImg> items = new ArrayList<NameImg>();
    private int crtMode;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, 
savedInstanceState);
        Bundle arguments = getArguments();
        crtMode = arguments.getInt("position");
        Toast.makeText(getActivity(), "Crt Frag :" + 
arguments.getString("frag"), Toast.LENGTH_LONG).show();
        switch (crtMode) {
            case 0:
                items.add(new NameImg("Manual", "Simple camera Control", 
R.drawable.hand));
                items.add(new NameImg("Bulb", "Long Exposure", 
R.drawable.bulb));
                items.add(new NameImg("Time Lapse", "Frame by Frame Movie", 
R.drawable.timelapse));
                items.add(new NameImg("HDR", "High Dinamic Range", 
R.drawable.hdr));
                items.add(new NameImg("IR", "Infra RED Control", 
R.drawable.ir));
                break;
            case 1:
                items.add(new NameImg("Triggered", "Trigger camera ", 
R.drawable.triggrered));
                items.add(new NameImg("Dark Room", "Long Exposure", 
R.drawable.darkroom));
                items.add(new NameImg("Lightning", "Frame by Frame Movie", 
R.drawable.lightning));
                break;
            case 3:
                items.add(new NameImg("1", "USB1", R.drawable.usb));
                items.add(new NameImg("2", "USB2", R.drawable.waterdrops));
                break;
        }
        setListAdapter(new ModesItemAdapter(getActivity(), items));
        //ListView myLV  = null;
        //myLV = (ListView) myLV.findViewById(R.id.customListView);
        //myLV.setItemChecked(0, true);
        return v;
    }

        @Override
        public void onListItemClick(ListView l, View v, int position, long 
id) {
            super.onListItemClick(l, v, position, id);
            //getListView().setSelector(android.R.color.holo_blue_bright);
            //Toast.makeText(getActivity(), "selected item :" + position + 
" ID :" + id, Toast.LENGTH_LONG).show();
            FragmentManager fm = getFragmentManager();
            ModeControls myControls;
            myControls = (ModeControls) fm.findFragmentByTag(position + 
"ModesContent" );
            if (myControls == null) {//if the fragment dosen't exists we 
create it
                Bundle arguments = new Bundle();
                arguments.putString("myTxt", "selected item :" + position + 
" ID :" + id);
                FragmentTransaction ft = fm.beginTransaction();
                myControls = new ModeControls();
                myControls.setArguments(arguments);
                ft.add(R.id.main_activity, myControls).commit();
            }
        }
}

and my modes adapter - for my data structure

public class ModesItemAdapter extends ArrayAdapter<NameImg> {

    public ModesItemAdapter(Context c, List<NameImg> items) {
        super(c, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemView itemView = (ItemView)convertView;
        if (null == itemView)
            itemView = ItemView.inflate(parent);
        itemView.setItem(getItem(position));
        return itemView;
    }
}


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to