我有带碎片的导航抽屉。
其中一个片段是WatchListFragment,它包含一个填充了剧集列表的RecyclerView。
列表与 RecyclerAdapter 相关联。 在RecyclerAdapter中,我有一个 onLongClickListener ,其结果应该是更改列表中的一些剧集,并将 WatchListFragment 中的列表替换为它。
但我不知道如何将数据传递给我的WatchListFragment。 在c ++中,id只是继续通过引用传递列表,但这不会在这里工作。 有任何想法吗?
片段代码:
public class WatchListFragment extends Fragment {
private EpisodeRecycleAdapter mAdapter;
private List<Episode> releasedEpisodeList; //contains all unseen & released episodes
private RecyclerView mRecyclerView;
private Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.watch_list_fragment, container, false);
context = view.getContext();
mRecyclerView = (RecyclerView) view.findViewById(R.id.watchlistRV);
/* improved performance if size of the layout doesnt change */
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
mRecyclerView.setLayoutManager(mLayoutManager);
//load watchlist from saved file
ejson = new EpisodeJSON(context);
watchList = ejson.readFromFile();
wlFunc = new WatchListFunctions();
//load released episodes of watchlist in releasedEpisodeList
releasedEpisodeList = wlFunc.getReleasedEpisodeList(watchList);
mAdapter = new EpisodeRecycleAdapter(context, releasedEpisodeList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.watchlist_options, menu);
}
RecyclerAdapter代码:
class EpisodeRecycleAdapter extends RecyclerView.Adapter<EpisodeRecycleAdapter.ViewHolder> {
private List<Episode> episodes = new ArrayList<>();
private Context mContext;
EpisodeRecycleAdapter(Context context, List<Episode> myDataset) {
super();
this.episodes = myDataset;
mContext = context;
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
TextView textViewName, textViewNumbers, textViewDate;
ImageView imageViewEye;
ViewHolder(View v) {
super(v);
textViewName = (TextView)v.findViewById(R.id.showname);
textViewNumbers = (TextView)v.findViewById(R.id.seasonepisodenumbers);
textViewDate = (TextView)v.findViewById(R.id.date);
imageViewEye = (ImageView) v.findViewById(R.id.eye_image);
}
}
// Create new views (invoked by the layout manager)
@Override
public EpisodeRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem_row, parent, false);
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
RecyclerView mRecyclerView = (RecyclerView) view.getRootView().findViewById(R.id.watchlistRV);
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
final Episode episodeItem = episodes.get(itemPosition);
final int interval = episodeItem.getInterval();
final LocalDate episodeDate = episodeItem.getDate();
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("title");
alert.setMessage("msg");
String dateStringArray[] = new String[52];
for (int i = 0; i < 52; i++) {
dateStringArray[i] = episodeDate.plusDays(interval * (i+1)).toString("dd.MM.yy");
}
final Spinner picker = new Spinner(mContext);
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, dateStringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
picker.setAdapter(adapter);
alert.setView(picker);
alert.show();
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int i = 1;
int eId = episodeItem.getSeasonID();
for(Episode e: episodes){
if(e.getSeasonID() == eId && !e.getDate().isBefore(episodeItem.getDate())){
e.setDate(e.getDate().plusDays(interval * i));
i++;
}
}
}
});
//TODO get episodes in watchlistfragment
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
return true; //return true so no onClick event happens
}
});
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int pos) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textViewName.setText(episodes.get(pos).getShowName());
holder.textViewNumbers.setText(episodes.get(pos).getSeasonEpisodeAsString());
holder.textViewDate.setText(episodes.get(pos).getDateAsString());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return episodes.size();
}
答案 0 :(得分:1)
在你的适配器中创建一个像这样的方法
public static List<Episode> getList(){
return episodes ;
}
并在您的片段中调用此方法
mAdapter.getList(); // List<Episode> after changes
您可以使用的另一种方法
您可以在releasedEpisodeList = episodes;
之后执行此r(Episode e: episodes){ if(e.getSeasonID() == eId && !e.getDate().isBefore(episodeItem.getDate())){ e.setDate(e.getDate().plusDays(interval * i)); i++; } }releasedEpisodeList = episodes;
,并将private List<Episode> releasedEpisodeList;
改为public static List<Episode> releasedEpisodeList