1. 程式人生 > >Android控制元件之RecyclerView的基本使用

Android控制元件之RecyclerView的基本使用

三、RecyclerView使用時要匯入android.suport.v7.wedget.RecyclerView包。

RecyclerView實現新增和刪除的簡單例子
<span style="font-size:18px;">public class MainActivity extends AppCompatActivity {

    private List<String> mDatas;
    private Context mContext;
    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter mAdapter;

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

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        mContext = MainActivity.this;

        initData();

        initView();

        //佈局管理
        LinearLayoutManager llManager = new LinearLayoutManager(mContext);
        //設定佈局管理器
        mRecyclerView.setLayoutManager(llManager);
        //設定分割線
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
        //設定刪除、新增的動畫
//        mRecyclerView.setItemAnimator(new SlideInOutRightItemAnimator(mRecyclerView));
//        mRecyclerView.setItemAnimator(new SlideInOutTopItemAnimator(mRecyclerView));
//        mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
//        mRecyclerView.setItemAnimator(new ScaleInOutItemAnimator(mRecyclerView));
        mRecyclerView.setItemAnimator(new SlideInOutBottomItemAnimator(mRecyclerView));

        mAdapter = new RecyclerViewAdapter(mContext, mDatas);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++){
            mDatas.add("" + (char)i);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_add) {
            mAdapter.add("New String",1);
            return true;
        }
        if(id == R.id.action_delete){
            mAdapter.remove(1);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}</span>

在menu中添加了增加和刪除的按鈕: