现在已经过去一周了,我非常需要一个解决方案,因为我需要在本周向我的客户呈现更新。所以,我有一个位于片段内的recyclerview,其中填充了使用volley库从网络服务中获取的数据。如果数据库中添加了新数据,我希望recyclerview重新加载/更新以实现更改或新添加的内容。现在,我尝试了许多方法,但未能实现所需的内容。我尝试了以下方法:
- 在处理程序内部,单击notifyDataSetChanged()适配器 - adapter.notifyItemRangeChanged(0, adapter.getItemCount()); - recyclerView.invalidate()也在处理程序内 - 分离和附加当前片段
另外,我还尝试了下面的代码。好吧,它可以工作,因为它会刷新整个活动并返回到最初的片段,即第一页。
片段类
- 在处理程序内部,单击notifyDataSetChanged()适配器 - adapter.notifyItemRangeChanged(0, adapter.getItemCount()); - recyclerView.invalidate()也在处理程序内 - 分离和附加当前片段
另外,我还尝试了下面的代码。好吧,它可以工作,因为它会刷新整个活动并返回到最初的片段,即第一页。
Intent intent = getActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();
getActivity().overridePendingTransition(0, 0);
startActivity(intent);
片段类
public class AddPlaylist extends Fragment {
RecyclerView recyclerView;
RecyclerAdapter adapter;
String[] id,title;
ArrayList<String> artist;
TextView text;
CreatePlaylist createPlaylist;
private CoordinatorLayout coordinatorLayout;
private FloatingActionButton fab;
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.addplaylist, container, false);
coordinatorLayout = (CoordinatorLayout) view.findViewById(R.id
.coordinatorLayout);
fab = (FloatingActionButton) view.findViewById(R.id.fab);
text = (TextView) view.findViewById(R.id.text);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInputDialog();
}
});
id = getArguments().getStringArray("id");
title = getArguments().getStringArray("title");
artist = new ArrayList<String>(Arrays.asList(title));
recyclerView= (RecyclerView) view.findViewById(R.id.my_recycler_view);
adapter = new RecyclerAdapter("addplaylist", id, artist, getActivity());
recyclerView.setAdapter(adapter);
recyclerView.invalidate();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
final Handler mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
recyclerView.invalidate();
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("playlist");
Bundle bundle = new Bundle();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
addPlaylist.setArguments(bundle);
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(addPlaylist);
ft.attach(addPlaylist);
ft.commit();
}
});
return view;
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//text.setText("Hello, " + editText.getText());
createPlaylist.createPlaylist(editText.getText().toString());
/*Intent intent = getActivity().getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
getActivity().finish();
getActivity().overridePendingTransition(0, 0);
startActivity(intent);*/
/*Bundle bundle = new Bundle();
Collections collections = new Collections();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
bundle.putString("fragment", "addfragment");
addPlaylist.setArguments(bundle);
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("playlist");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(collections);
ft.attach(frg);
ft.commit();*/
/*getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});*/
/*FragmentManager fragmentTransaction = getFragmentManager();
android.app.FragmentTransaction transaction = fragmentTransaction.beginTransaction();
Bundle bundle = new Bundle();
AddPlaylist addPlaylist = new AddPlaylist();
bundle.putStringArray("id", JsonArray.ids);
bundle.putStringArray("title", JsonArray.titles);
addPlaylist.setArguments(bundle);
transaction.addToBackStack("xyz");
transaction.add(R.id.containerView, addPlaylist);
//transaction.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
transaction.commit();*/
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
@UiThread
protected void dataSetChanged() {
adapter.notifyDataSetChanged();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
id = getArguments().getStringArray("id");
title = getArguments().getStringArray("title");
artist = new ArrayList<String>(Arrays.asList(title));
adapter = new RecyclerAdapter("addplaylist", id, artist, getActivity());
adapter.notifyDataSetChanged();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
createPlaylist = (CreatePlaylist) activity;
}catch(Exception e) {
}
}
public interface CreatePlaylist{
void createPlaylist(String name);
}
}
抱歉代码很乱。这是我尝试的全部内容。