我有一个包含这些ui小部件的对话框片段-具有两个选项卡和viewpager的选项卡布局,并在底部提供了一个用于提交结果的按钮。每个标签都包含一个recyclerview复选框。
选中项目并单击按钮后,我想根据选中的位置检索每个项目的ID。
这是我对一个片段/选项卡的实现,我已经填充了列表,并且在以下情况下效果很好:
public class MySessionsFragment extends Fragment {
private static final String TAG = MySessionsFragment.class.getSimpleName();
private RecyclerView mySessionsRV;
private MySessionsAdapter adapter;
private List<Sessions> list = new ArrayList<>();
public MySessionsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_sessions, container, false);
init(view);
return view;
}
private void init(View view) {
mySessionsRV = view.findViewById(R.id.mySessionsRV);
mySessionsRV.setHasFixedSize(true);
mySessionsRV.setLayoutManager(new GridLayoutManager(getActivity(), 3));
populate();
}
private void populate() {
SQLite.select()
.from(Sessions.class)
.async()
.queryListResultCallback(new QueryTransaction.QueryResultListCallback<Sessions>() {
@Override
public void onListQueryResult(QueryTransaction transaction, @NonNull List<Sessions> tResult) {
list = tResult;
adapter = new MySessionsAdapter(getActivity(), list);
mySessionsRV.setAdapter(adapter);
}
}).execute();
}
}
在对话框片段(标签持有人片段类)中,我试图从适配器中检索项目,如下所示:
public class ProgramsSessionsListFragment extends DialogFragment {
private static final String TAG = ProgramsSessionsListFragment.class.getSimpleName();
private Toolbar toolbar;
private ImageView closeIV;
private TabLayout tabLayout;
private FloatingActionButton addSessionFAB;
private ViewPager viewPager;
private ViewPagerAdapter pagerAdapter;
private Button doneBtn;
private List<String> myItemsList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sessions_list, container, false);
init(view);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
return view;
}
private void init(View view) {
toolbar = view.findViewById(R.id.toolbar);
closeIV = view.findViewById(R.id.closeIV);
tabLayout = view.findViewById(R.id.tabLayout);
addSessionFAB = view.findViewById(R.id.addSessionFAB);
viewPager = view.findViewById(R.id.viewPager);
doneBtn = view.findViewById(R.id.doneBtn);
closeIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
addSessionFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewSessionDialog sessionDialog = new NewSessionDialog();
sessionDialog.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Light_NoTitleBar_Fullscreen);
sessionDialog.show(getChildFragmentManager(), "NewSessionDialog");
}
});
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
retrieveSessionsSelected();
}
});
setUpTabs();
}
private void retrieveSessionsSelected() {
MySessionsAdapter adapter = new MySessionsAdapter(getActivity());
adapter.getItemsSelected();
if (adapter.getItemsSelected().size() > 0) {
Log.d(TAG, "My Sessions List Size:\t" + myItemsList.size());
myItemsList = adapter.getItemsSelected();
//get individual items from list
for (int i = 0; i < myItemsList.size(); i++) {
Log.d(TAG, "Selected Items Id:\t" + myItemsList.get(i));
}
} else {
Toast.makeText(getActivity(), "Ntn Selected Yet", Toast.LENGTH_SHORT).show();
}
}
private void setUpTabs() {
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
setUpViewPager(viewPager);
}
private void setUpViewPager(ViewPager viewPager) {
MySessionsFragment mf = new MySessionsFragment();
RecommFragment rf = new RecommFragment();
pagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
pagerAdapter.addFragTab(mf, "My Sessions");
pagerAdapter.addFragTab(rf, "Recommended");
viewPager.setAdapter(pagerAdapter);
}
}
这是我的适配器代码,用于显示项目并将所选项目的克隆列表返回到父片段:
public class MySessionsAdapter extends RecyclerView.Adapter<MySessionsViewHolder> {
private static final String TAG = MySessionsAdapter.class.getSimpleName();
private final Context context;
private List<Sessions> itemsList;
private List<String> selectedSessionsList = new ArrayList<>();
public MySessionsAdapter(Context context) {
this.context = context;
}
public MySessionsAdapter(Context context, List<Sessions> itemsList) {
this.context = context;
this.itemsList = itemsList;
}
@Override
public MySessionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_sessions_items_layout, parent, false);
return new MySessionsViewHolder(view);
}
@Override
public void onBindViewHolder(MySessionsViewHolder viewholder, int position) {
final Sessions sessions = itemsList.get(position);
viewholder.mySessionNameTV.setText(sessions.session_name);
Picasso.with(context)
.load(sessions.session_cover_image)
.placeholder(R.drawable.app_logo_resized)
.into(viewholder.mySessionIV);
viewholder.checkBoxSelect.setChecked(false);
viewholder.checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.d(TAG, "Session id:\t" + sessions.prog_sessionId);
selectedSessionsList.add(sessions.prog_sessionId);
for (int m = 0; m < selectedSessionsList.size(); m++) {
Log.d(TAG, "Session id in list:\t" + selectedSessionsList.get(m));
}
}
}
});
}
public List<String> getItemsSelected() {
return selectedSessionsList;
}
@Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
运行此应用程序时,我收到一条吐司消息,什么都没有选择,但在选项卡中,我可以看到所选的每个项目的日志,但父片段中的适配器方法调用未返回任何值。有人可以向我解释我在这里做错了什么吗?谢谢。