如何在RecyclerView

时间:2017-07-26 08:07:07

标签: android android-recyclerview fragment savestate

我是android开发的新手,所以我还没有完成所有的基础知识 几天后我就遇到了问题。
我的问题如下:
我收到了一些带有一些物品的Recyclerview。我创建了一个按钮来添加列表中的新项目。添加功能工作正常,我可以看到任何新项目,但如果我刷到第三个片段(我的应用程序中有3个片段,并且左边的一个是recyclerView)或者如果我只是杀了我的应用程序并重新启动它,每个新的物品被销毁。
所以基本上我不知道如何真正在Android应用程序中保存这种东西。我需要你的帮助来指明我的方​​式 这是我的代码:

使用recyclerview进行分段

public class CameraFragment extends Fragment  {

//matchselector reclycler view
private ArrayList<Item> items = new ArrayList<>();
private RecyclerView recyclerView;
private SnapRecyclerAdapter adapter;
// add
private View addMatch;
private AlertDialog.Builder alertDialog;
private EditText team1_add;
private View view;
private boolean add = false;
private int edit_position;
private Item newitem;


public static CameraFragment create() {
    return new CameraFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_camera, container, false);

    initDialog();

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    //match selector vertical recycler view
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    addMatch = (View) view.findViewById(R.id.add_match);


    /**
     * Center snapping
     */
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    final CustomLinearLayoutManager mCustomLinearLayoutManager = new CustomLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

    recyclerView.setLayoutManager(mCustomLinearLayoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new SnapRecyclerAdapter(getContext(), items);
    recyclerView.setAdapter(adapter);

    MatchList();
    adapter.notifyDataSetChanged();

    //Button addmatch click
    addMatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.add_match:
                removeView();
                add = true;
                alertDialog.setTitle("Ajout");
                team1_add.setText("");
                alertDialog.show();
                break;
        }
    }
    });

}

// dialog ajout
private void initDialog(){
    alertDialog = new AlertDialog.Builder(getContext());
    view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout_addmatch,null);
    alertDialog.setView(view);
    alertDialog.setPositiveButton("Ajouter", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(add){
                add =false;
                newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                adapter.addItem(newitem);
                dialog.dismiss();
            } else {
                items.set(edit_position, newitem);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }

        }
    });
    team1_add = (EditText)view.findViewById(R.id.team1_add);
}


private void removeView(){
    if(view.getParent()!=null) {
        ((ViewGroup) view.getParent()).removeView(view);
    }
}


public void MatchList() {
    items.add(new Item(1, "a", "b", "c","d" , "12:30" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(4, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(2, "a", "b", "c",  "d" , "12:30" , R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(3, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    adapter.notifyDataSetChanged();
}

}

SnapRecyclerAdapter:

public class SnapRecyclerAdapter extends RecyclerView.Adapter<SnapRecyclerAdapter.RecyclerViewHolder> {

private LayoutInflater layoutInflater;
private Context context;
private ArrayList<Item> items;

public SnapRecyclerAdapter(Context context, ArrayList<Item> items) {
    this.layoutInflater = LayoutInflater.from(context);
    this.context = context;
    this.items = items;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View item = layoutInflater.inflate(R.layout.item_recycler_view, parent, false);
    return new RecyclerViewHolder(item);
}

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
    Item item = items.get(position);

    holder.Logoteam1.setImageResource(item.getLogoteam1());
    holder.Logoteam2.setImageResource(item.getLogoteam2());
    holder.team1.setText(item.getTeam1());
    holder.team2.setText(item.getTeam2());
    holder.categorie.setText(item.getCategorie());
    holder.location.setText(item.getLocation());
    holder.date.setText(item.getDate());


}

public void addItem(Item newitem) {
    items.add(newitem);
    notifyItemInserted(items.size());
    notifyDataSetChanged();
}


public void removeItem(int position) {
    items.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, items.size());
}


@Override
public int getItemCount() {
    return items.size();
}

class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private ImageView Logoteam1;
    private ImageView Logoteam2;
    private TextView team1;
    private TextView team2;
    private TextView categorie;
    private TextView location;
    private TextView date;

    private RecyclerViewHolder(final View v) {
        super(v);

        Logoteam1 = (ImageView) v.findViewById(R.id.Logoteam1);
        Logoteam2 = (ImageView) v.findViewById(R.id.Logoteam2);
        team1 = (TextView) v.findViewById(R.id.team1);
        team2 = (TextView) v.findViewById(R.id.team2);
        categorie = (TextView) v.findViewById(R.id.categorie);
        location = (TextView) v.findViewById(R.id.location);
        date = (TextView) v.findViewById(R.id.date);

    }
}
}

2 个答案:

答案 0 :(得分:1)

将项目添加到回收站视图顶部的简单方法如下:

public class CameraFragment extends Fragment  {

//matchselector reclycler view
private ArrayList<Item> items = new ArrayList<>();
private RecyclerView recyclerView;
private SnapRecyclerAdapter adapter;
// add
private View addMatch;
private AlertDialog.Builder alertDialog;
private EditText team1_add;
private View view;
private boolean add = false;
private int edit_position;
private Item newitem;


public static CameraFragment create() {
    return new CameraFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_camera, container, false);

    initDialog();

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    //match selector vertical recycler view
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    addMatch = (View) view.findViewById(R.id.add_match);


    /**
     * Center snapping
     */
    SnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    final CustomLinearLayoutManager mCustomLinearLayoutManager = new CustomLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

    recyclerView.setLayoutManager(mCustomLinearLayoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new SnapRecyclerAdapter(getContext(), items);
    recyclerView.setAdapter(adapter);

    MatchList();
    adapter.notifyDataSetChanged();

    //Button addmatch click
    addMatch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.add_match:
        int position = 0

               newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                items.add(position,"" + newitem)
 adapter.notifyItemInserted(position);
               // removeView();
              //  add = true;
              //  alertDialog.setTitle("Ajout");
              //  team1_add.setText("");
              //  alertDialog.show();
                break;
        }
    }
    });

}

// dialog ajout
private void initDialog(){
    alertDialog = new AlertDialog.Builder(getContext());
    view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout_addmatch,null);
    alertDialog.setView(view);
    alertDialog.setPositiveButton("Ajouter", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(add){
                add =false;
                newitem = new Item(5, team1_add.getText().toString(), "0", "0","0" , "0" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge);
                adapter.addItem(newitem);
                dialog.dismiss();
            } else {
                items.set(edit_position, newitem);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }

        }
    });
    team1_add = (EditText)view.findViewById(R.id.team1_add);
}


private void removeView(){
    if(view.getParent()!=null) {
        ((ViewGroup) view.getParent()).removeView(view);
    }
}


public void MatchList() {
    items.add(new Item(1, "a", "b", "c","d" , "12:30" ,   R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(4, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(2, "a", "b", "c",  "d" , "12:30" , R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    items.add(new Item(3, "a", "b", "c","d" , "12:30" ,  R.mipmap.ic_blasonvierge, R.mipmap.ic_blasonvierge));
    adapter.notifyDataSetChanged();
}

}

答案 1 :(得分:0)

只要加载了特定片段,就会创建Recycleview列表中的对象。因此,每次杀死应用程序或移动到第三个片段时,都会重新加载第一个片段,并再次创建包含您放置的默认值的对象。添加到列表仅在该对象保留在内存中之前有效,否则它将被销毁。根据您的要求,您可以使用任何存储介质,例如Sharedpreferences(使用Serailization)甚至是sqlite(虽然小任务不是很多)。