我有一个带碎片的导航抽屉。
其中一个片段是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);
}
回收适配器代码:
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();
}
最佳答案
在适配器中创建如下方法
public static List<Episode> getList(){
return episodes ;
}
像这样在片段中调用这个方法
mAdapter.getList(); // List<Episode> after changes
您可以使用的另一种方法
在fo
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
关于android - 从RecyclerAdapter中获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39957304/