首页 > 解决方案 > EditText Data to be displayed in a recycleview

问题描述

I have a fragment which contains a EditText part and a send button I need to send the same text entered in EditText by pressing the send button to the RecycleView. The text which I sent should be displayed above the edit text.

The design is like this :

enter image description here

标签: javaandroidandroid-fragmentsstickyrecycleview

解决方案


RecyclerView recyclerView;
List<Object> list;
RecycleAdapter recycleAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView =  inflater.inflate(R.layout.fragment_main, container, false);
    recyclerView = rootView.findViewById(R.id.recyclerview);
    list = new ArrayList<>();
    Button send= rootView.findViewById(R.id.button);
    final EditText editText = rootView.findViewById(R.id.editText);
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            list.add(0,editText.getText().toString());
            recycleAdapter.notifyDataSetChanged();

        }
    });
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL,true));
    recycleAdapter = new RecycleAdapter(getActivity(), list);
    recyclerView.setAdapter(recycleAdapter);

    return  rootView;
}

推荐阅读