在我的应用程序中,我有一个SlidingTabLayout,每个选项卡都包含一个列表视图。当我多次浏览选项卡时,每次都会在列表视图中添加项目。我该如何防止这种行为?
第二个问题
在每个选项卡上我想添加第二个片段,它将包含一些控件,每个片段必须与列表视图中的列表项链接,当我切换选项卡时,必须选择第一个列表项来显示相应的片段。我怎样才能实现这种行为?
我的主要活动
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);
}
}
我的查看寻呼机适配器
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;
}
}
我的listview创建者
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();
}
}
}
和我的模式适配器 - 用于我的数据结构
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;
}
}
答案 0 :(得分:3)
使用
items.clear()
或
items = new ArrayList<NameImg>();
在开关()语句之前。您正在向项目 arraylist添加数据而不清除列表。所以,你的arraylist越来越大。