我在选项卡式布局的选项卡中有一个recyclerview,所以在一个片段中。从recyclerview中添加和删除项目效果很好,但是当我刷新应用程序(打开手机上的最新应用程序并关闭它)时,recyclerview为空,即应用程序重新启动时。请让我知道是否需要查看任何代码。
这是我的片段的代码:
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static rongorai.wakeup.MainActivity.alarms;
public class Tab1 extends Fragment
{
/**Fragment stuff-------------------------------------------*/
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**Actual stuff--------------------------------------------*/
private FloatingActionButton fab;
private Button inserttest;
private Button removetest;
private RecyclerView rv;
public static MainAdapter rva;
public static TextView txtNoAlarms;
/**Methods-------------------------------------------------*/
public Tab1()
{
// Required empty public constructor
}
public static Tab1 newInstance(String param1, String param2)
{
Tab1 fragment = new Tab1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
fab = view.findViewById(R.id.fab);
rv = view.findViewById(R.id.rv);
inserttest = view.findViewById(R.id.inserttest);
removetest = view.findViewById(R.id.removettest);
txtNoAlarms = view.findViewById(R.id.txtNoAlarm);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(getActivity(), NewAlarm.class);
startActivity(intent);
}
});
removetest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*if (alarms.size() > 0) {
alarms.remove(0);
}
rva.notifyItemRemoved(0);
if (alarms.size() == 0 & txtNoAlarms.getVisibility() == View.INVISIBLE) {
txtNoAlarms.setVisibility(View.VISIBLE);
}*/
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.putExtra(AlarmClock.EXTRA_HOUR, 21);
intent.putExtra(AlarmClock.EXTRA_MINUTES, 6);
startActivity(intent);
}
});
inserttest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alarms.add(new Alarm("alarm name", true, true, true, true, new boolean[] {true, false}, 15, 27));
rva.notifyItemInserted(0);
if (alarms.size() != 0 & txtNoAlarms.getVisibility() == View.VISIBLE) {
txtNoAlarms.setVisibility(View.INVISIBLE);
}
updateAlarmVals();
}
});
//Recycler View-----------------------------------------------------------------------------
rv.setHasFixedSize(true);
rva = new MainAdapter(alarms, getActivity());
rv.setAdapter(rva);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
/*rva.setOnItemClickListener(new MainAdapter.OnItemClickedListener() {
@Override
public void onItemClick(int position) {
//Toast.makeText(getActivity(), "clicked " + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), EditAlarm.class);
startActivity(intent);
}
});*/
return view;
}
public static void updateAlarmVals() {
Tab2.alarmvals.setText("" + alarms.size() + "\n");
for (Alarm i: alarms) {
Tab2.alarmvals.setText(Tab2.alarmvals.getText().toString() +
i.toString() + "\n");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@Override
public void onAttach(Context context)
{
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener)
{
mListener = (OnFragmentInteractionListener) context;
} else
{
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach()
{
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener
{
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:0)
您可能需要在片段中处理onResume()
并更新recyclerview。
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}