gpt4 book ai didi

android - 避免在 ListView 中重新添加项目

转载 作者:行者123 更新时间:2023-11-29 17:33:29 26 4
gpt4 key购买 nike

在我的应用程序中,我有一个 SlidingTabLayout,每个选项卡都包含一个 ListView 。当我多次浏览选项卡时,每次都会将项目添加到 ListView 中。我怎样才能防止这种行为?

第二个问题

在每个选项卡上我想添加第二个 fragment 来保存一些控件,每个 fragment 都必须与 ListView 中的列表项链接,当我切换选项卡时必须选择第一个列表项以显示相应的 fragment 。我怎样才能实现这种行为?

我的主要 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);
}

我的 View 寻呼机适配器

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;
}

最佳答案

使用

items.clear() 

items = new ArrayList<NameImg>();

switch() 语句之前。您正在向 items arraylist 添加数据而不清除列表。所以,你的 arraylist 越来越大。

关于android - 避免在 ListView 中重新添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com