RecyclerView添加项目

时间:2015-06-28 03:09:01

标签: android android-recyclerview

我正在制作笔记应用。当我点击R.id.delete_settingsfab12时,会打开一个名为 NewNote.java 的新活动。 当我点击EditTexts时,我想在 MainActivity.java 中添加2个R.id.send_new_note我的recyclelerview。我无法弄清楚如何添加项目(是的,我看了很多网站)。正如你所看到的,当我点击它时,我可以删除内容。我已经包含了很多代码,所以你可以帮助我更好。任何帮助深表感谢! :d

MainActivity.java

package zeta.bedi.gursimran.zeta;

public class MainActivity extends AppCompatActivity {

    //Toolbar
    private Toolbar toolbar;

    //RecyclerView
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "MainActivity";

    //FAB
    private FloatingActionButton fab12;
    private FloatingActionButton fab22;
    private List<FloatingActionMenu> menus = new ArrayList<>();
    private Handler mUiHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        //RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        //FAB
        final FloatingActionMenu menu2 = (FloatingActionMenu) findViewById(R.id.menu2);
        menus.add(menu2);
        menu2.hideMenuButton(true);
        int delay = 400;

        for (final FloatingActionMenu menu : menus) {
            mUiHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    menu.showMenuButton(true);
                }
            }, delay);
            delay += 150;
        }
        menu2.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
            @Override
            public void onMenuToggle(boolean opened) {
                String text;
                if (opened) {
                    text = "Menu opened";
                } else {
                    text = "Menu closed";
                }
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
        fab12 = (FloatingActionButton) findViewById(R.id.fab12);
        fab22 = (FloatingActionButton) findViewById(R.id.fab22);
        fab12.setOnClickListener(clickListener);
        fab22.setOnClickListener(clickListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ((MyAdapter) mAdapter).setOnItemClickListener(new MyAdapter.MyClickListener() {
            @Override
            public void onItemClick(int position, View v) {

                //Delete items
                Log.i(LOG_TAG, " Removed " + position);
                ((MyAdapter) mAdapter).deleteEntity(position);

             }
        });
    }

    public ArrayList<MyInformation> getDataSet() {
        ArrayList results = new ArrayList<MyInformation>();
        for (int i = 0; i < 5; i++) {
            MyInformation information = new MyInformation("Hi " + i, "Secondary " + i);
            results.add(i, information);
        }

        TextView newNote = (TextView) findViewById(R.id.edit_text_note);
        TextView newTitle = (TextView) findViewById(R.id.edit_text_title);            

        return results;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.delete_settings) {

            startActivity(new Intent(this, NewNote.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text;
            switch (v.getId()) {
                case R.id.fab12:
                    text = fab12.getLabelText();

                    startActivity(new Intent(MainActivity.this, NewNote.class));

                    break;
                case R.id.fab22:
                    text = fab22.getLabelText();

                    break;
            }
        }
    };
}

NewNote.java

package zeta.bedi.gursimran.zeta;

public class NewNote extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_note);
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_note, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == android.R.id.home) {
            NavUtils.navigateUpFromSameTask(this);
        }
        if (id == R.id.send_new_note) {


        }
        return super.onOptionsItemSelected(item);
    }       
}

MyAdapter.java

package zeta.bedi.gursimran.zeta;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<MyInformation> information;
    private static MyClickListener myClickListener;
    private static String LOG_TAG = "MyRecyclerViewAdapter";

    public static class ViewHolder
            extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        TextView title;
        TextView note;
        ImageView image;
        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            note = (TextView) itemView.findViewById(R.id.note);
            image = (ImageView) itemView.findViewById(R.id.imageView);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getPosition(), v);
            Log.i(LOG_TAG, "CLICK Listener" + getPosition());
        }
    }
    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(ArrayList<MyInformation> information) {
        this.information = information;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                         int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row, parent, false);

        // set the view's size, margins, paddings and layout parameters
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.title.setText(information.get(position).getTitle());
        holder.note.setText(information.get(position).getNote());
    }

    public void addItem(MyInformation myInformation, int index) {
        information.add(myInformation);
        notifyItemInserted(index);
    }
    public void deleteItem(int index) {
        information.remove(index);
        notifyItemRemoved(index);
    }
    public void setData(final List<MyInformation> data) {
        // Remove all deleted items.
        for (int i = information.size() - 1; i >= 0; --i) {
            if (getLocation(data, information.get(i)) < 0) {
                deleteEntity(i);
            }
        }
        // Add and move items.
        for (int i = 0; i < data.size(); ++i) {
            MyInformation entity = data.get(i);
            int loc = getLocation(information, entity);
            if (loc < 0) {
                addEntity(i, entity);
            } else if (loc != i) {
                moveEntity(i, loc);
            }
        }
    }
    public void addEntity(int i, MyInformation entity) {
        information.add(i, entity);
        notifyItemInserted(i);
    }
    public void moveEntity(int i, int loc) {
        move(information, i, loc);
        notifyItemMoved(i, loc);
    }

    public void deleteEntity(int i) {
        information.remove(i);
        notifyItemRemoved(i);
    }
    private void move(List<MyInformation> data, int a, int b) {
        MyInformation temp = data.remove(a);
        data.add(b, temp);
    }

    private int getLocation(List<MyInformation> data, MyInformation entity) {
        for (int j = 0; j < data.size(); ++j) {
            MyInformation newEntity = data.get(j);
            if (entity.equals(newEntity)) {
                return j;
            }
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        return information.size();
    }
    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

MyInformation.java

package zeta.bedi.gursimran.zeta;

public class MyInformation {
    String title;
    String note;

    public MyInformation(String title, String note) {
        this.title = title;
        this.note = note;
    }

    public String getNote() {
        return note;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setNote(String note) {
        this.note = note;
    }
}

activity_new_note.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="zeta.bedi.gursimran.zeta.NewNote">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"
        />

    <EditText
        android:layout_width="@dimen/edit_title_width"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text_title"
        android:hint="@string/hint_title"
        android:layout_below="@+id/app_bar"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:textSize="@dimen/edit_text_large_size"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text_note"
        android:hint="@string/hint_note"
        android:layout_below="@+id/edit_text_title"
        android:padding="5dp"
        android:inputType="textCapSentences"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:textSize="@dimen/edit_text_medium_size"
        android:imeOptions="actionSend"
        />

</RelativeLayout>

0 个答案:

没有答案