当从recyclerview中选中一个复选框时,我想删除一个分片,并且它是从tablayout中查看的。我已经跳过了初始化代码,将仅共享适配器代码(这就是工作所在),如下所示:
ViewpagerAdapter代码:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG = ViewPagerAdapter.class.getSimpleName();
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titlesList = new ArrayList<>();
private Context context;
private TabLayout tabLayout;
private ViewPager viewPager;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public ViewPagerAdapter(FragmentManager fm, Context ctx, ViewPager viewPager, TabLayout tabLayout) {
super(fm);
this.context = ctx;
this.viewPager = viewPager;
this.tabLayout = tabLayout;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
public void addFragTab(Fragment fragment, String name){
fragmentList.add(fragment);
titlesList.add(name);
}
public void addFragLastPosition(Fragment fragment, String name){
fragmentList.add(fragmentList.size(), fragment);
titlesList.add(name);
}
public void removeFrag(int position){
removeTab(position);
Fragment fragment = fragmentList.get(position);
fragmentList.remove(fragment);
titlesList.remove(position);
destroyFragmentView(viewPager, position, fragment);
notifyDataSetChanged();
}
private void destroyFragmentView(ViewGroup container, int position, Object obj) {
FragmentManager manager = ((Fragment) obj).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove((Fragment) obj).commit();
}
private void removeTab(int position) {
if (tabLayout.getChildCount() > 2){
tabLayout.removeTabAt(position);
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titlesList.get(position);
}
}
和带有复选框的recyclerview适配器以删除和添加片段:
public class PrefsAdapter extends RecyclerView.Adapter<PrefsCustomizeViewHolder> {
private static final String TAG = PrefsAdapter.class.getSimpleName();
private final Context context;
private List<Customize> itemsList;
private long id_nature, id_space;
private Customize cust;
private String nature, space;
private ViewPagerAdapter pagerAdapter;
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new ViewPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
@Override
public PrefsCustomizeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prefs_customize_items_layout, parent, false);
return new PrefsCustomizeViewHolder(view);
}
@Override
public void onBindViewHolder(final PrefsCustomizeViewHolder viewholder, final int position) {
final Customize customize = itemsList.get(position);
Picasso.with(context)
.load(customize.getIcon())
.fit()
.into(viewholder.prefsSelImg);
switch (position) {
case 0:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.NATURE)
.executeSingle();
if (cust != null) {
nature = cust.tabName;
Log.d(TAG, "Nature Tab From DB:\t" + nature);
id_nature = cust.getId();
Log.d(TAG, "Nature Tab Id from DB:\t" + id_nature);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
case 1:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.SPACE)
.executeSingle();
if (cust != null) {
space = cust.tabName;
Log.d(TAG, "Space Tab From DB:\t" + space);
id_space = cust.getId();
Log.d(TAG, "Space Tab Id from DB:\t" + id_space);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
}
viewholder.prefsSelImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position) {
case 0:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
//Customize.delete(Customize.class, id_nature);
pagerAdapter.removeFrag(1); // not working
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addFragTab(new NatureFrag(), Constants.NATURE); // not working too
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
case 1:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
Customize.delete(Customize.class, id_space);
Log.d(TAG, "Deleted Space id:\t" + id_space); // prints right log detail
pagerAdapter.removeTab(new SpaceFragment(), Constants.SPACE);
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addTab(new SpaceFragment(), Constants.SPACE);
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
}
}
});
}
@Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
删除标签的方法无效。任何人都可以帮助实现此实现或其他方式吗?谢谢。