我创建了两个嵌套的片段选项卡。在两个片段选项卡之间选择的页面中添加了一个listitem重复项。只有在快速滑动片段时才会添加重复项。如果我顺利选择了页面,则意味着listview不会创建duplicates。什么是在android中删除listview副本的解决方案?`
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.activity_explore_next, container, false);
FontChangeCrawler fontChanger = new FontChangeCrawler(getContext().getAssets(), "arial.ttf");
fontChanger.replaceFonts((ViewGroup) rootview);
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
hashSet.addAll(movieList);
movieList.clear();
movieList.addAll(hashSet);
peoplelist = (ListView) rootview.findViewById(R.id.peoplelist);
taglist = (ListView) rootview.findViewById(R.id.taglist);
usermsg = (TextView) rootview.findViewById(R.id.usernotfound);
search = (EditText) rootview.findViewById(R.id.searchbar);
adapter = new CustomListAdapter2(getActivity(), movieList);
peoplelist.setItemsCanFocus(true);
peoplelist.setAdapter(null);
peoplelist.setAdapter(adapter);
peoplelist.setVisibility(View.GONE);
adapter1 = new CustomListAdapter21(getActivity(), movieList1);
taglist.setItemsCanFocus(false);
taglist.setAdapter(adapter1);
viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
setupViewPager(viewPager);
viewPager.setOffscreenPageLimit(2-1);//Caching Fragment view
PageListener pageListener = new PageListener();
viewPager.setOnPageChangeListener(pageListener);
search = (EditText) rootview.findViewById(R.id.searchbar);
peoplelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
System.out.println("item clicked");
}
});
movieList.clear();
getPeople();
// adapter.notifyDataSetChanged();
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new PeopleFragment(), "PEOPLE");
adapter.addFragment(new TagFragment(), "TAGS");
viewPager.setAdapter(adapter);
viewPager.requestTransparentRegion(viewPager);
}
public class PageListener extends ViewPager.SimpleOnPageChangeListener {
public void onPageSelected(int position) {
System.out.println("position in view pager" + position);
movieList.clear();
if (position == 0) {
text = "people";
search.setHint("Search people");
usermsg.setText("No Username found");
search.setText("");
movieList.clear();
getPeople();
} else {
text = "tag";
search.setHint("Search hashtags");
search.setText("");
usermsg.setText("# No Hash tag found");
movieList1.clear();
getHashTag();
}
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onDestroyView() {
//mContainer.removeAllViews();
ViewGroup mContainer = (ViewGroup) getActivity().findViewById(R.id.editprofilenext);
mContainer.removeAllViews();
mContainer.clearFocus();
mContainer.removeAllViewsInLayout();
((View) movieList).clearFocus();
adapter.notifyDataSetChanged();
super.onDestroyView();
}
}
CustomListAdapter2`
class CustomListAdapter2 extends BaseAdapter implements Filterable {
private Activity activity;
private Context mContext;
private LayoutInflater inflater;
private List<Movie> movieItems;
private List<FeedItem> feedlist;
private List<Movie> mStringFilterList;
ValueFilter valueFilter;
public static final String MY_PREFS_NAME = "MyPrefsFile";
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter2(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
this.mStringFilterList = movieItems;
this.mStringFilterList.addAll(movieItems);
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
final Movie m = movieItems.get(position);
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
mHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_people, null);
FontChangeCrawler fontChanger = new FontChangeCrawler(getContext().getAssets(), "arial.ttf");
fontChanger.replaceFonts((ViewGroup) convertView);
mHolder.thumbNail = (ImageView) convertView
.findViewById(R.id.image5);
mHolder.Username = (TextView) convertView.findViewById(R.id.contact_name);
mHolder.Fullname = (TextView) convertView.findViewById(R.id.phone_number);
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.Username.setText(m.getUsername());
mHolder.Fullname.setText(m.getFullname());
Glide.with(convertView.getContext()).load(m.getThumbnailUrl()).asBitmap().override(100, 100).placeholder(R.drawable.profileicon).centerCrop().into(new BitmapImageViewTarget(mHolder.thumbNail) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(view.getContext().getResources(), resource);
circularBitmapDrawable.setCircular(true);
mHolder.thumbNail.setImageDrawable(circularBitmapDrawable);
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//tabLayout.setVisibility(View.GONE);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.editprofilenext, ParallaxProfileActivity.newInstance(m.getid()), ParallaxProfileActivity.TAG)
.addToBackStack(null)
.commit();
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
return convertView;
}
private class ViewHolder {
private TextView Username, Fullname;
private ImageView thumbNail;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<Movie> filterList = new ArrayList<Movie>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getUsername().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Movie country = new Movie();
Movie m = mStringFilterList.get(i);
filterList.add(m);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
movieItems = (List<Movie>) results.values;
notifyDataSetChanged();
}
}
}
`
答案 0 :(得分:0)
在pagelistener类中尝试此操作。清除这两个列表并调用notifyDataSetChanged(),
if (position == 0) {
text = "people";
search.setHint("Search people");
usermsg.setText("No Username found");
search.setText("");
movieList.clear();
movieList1.clear();
getPeople();
adapter.notifyDataSetChanged();
} else {
text = "tag";
search.setHint("Search hashtags");
search.setText("");
usermsg.setText("# No Hash tag found");
movieList.clear();
movieList1.clear();
getHashTag();
adapter1.notifyDataSetChanged();
}
如果它不起作用,请告诉我,我会尝试另一种解决方案..