RecyclerView初步尝试

早就听说RecyclerView有多少多少XX
但是直到今天才开始尝试使用它,惭愧惭愧

RecyclerView初步尝试_第1张图片

包括:
1,分割线装饰(DividerGridItemDecoration & DividerItemDecoration)
2,自定义动画(ScaleInOutItemAnimator等)
3,Item点击和长按事件
4,动态添加删除Item
5,自定义ActionBar风格

注意:瀑布流效果需要Item的高度都不一致,这样才能看出来。
动态添加删除Item时分割线会有点小问题,如果很介意,建议不要使用装饰(Decoration)来实现分割线,而是使用Item的padding或margin

很简单直接上代码
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.widget.effect.DividerGridItemDecoration;


/**
 * http://blog.csdn.net/lmj623565791/article/details/45059587
 * @author Administrator
 *
 */
public class MainActivity extends Activity {

	private Context context;
	private MyAdapter adapter;
	private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        initViews();
    }

    public void initViews(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());//动画effect
        
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        
        final ArrayList<KeyValue> list=new ArrayList<KeyValue>();
        for (int i = 0; i < 100; i++){
        	list.add(new KeyValue("key"+i, "value"+i));
        }
        adapter = new MyAdapter(list);
        adapter.setOnItemClickLitener(new MyAdapter.OnItemClickLitener() {
			
			@Override
			public void onItemClick(View view, int position) {
				// TODO Auto-generated method stub
				adapter.removeData(position);
			}
		});
        // 设置Adapter
        recyclerView.setAdapter(adapter);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.action_add:
            adapter.addData(1);
            break;
        case R.id.action_delete:
        	adapter.removeData(1);
            break;
        case R.id.action_0:
        	LinearLayoutManager manager0 = new LinearLayoutManager(this);
	        manager0.setOrientation(LinearLayoutManager.HORIZONTAL);
	        recyclerView.setLayoutManager(manager0);
        	break;
        case R.id.action_1:
        	LinearLayoutManager manager1 = new LinearLayoutManager(this);
        	manager1.setOrientation(LinearLayoutManager.VERTICAL);
        	recyclerView.setLayoutManager(manager1);
        	break;
        case R.id.action_2:
        	GridLayoutManager manager2 = new GridLayoutManager(this,4);
        	recyclerView.setLayoutManager(manager2);
        	break;
        case R.id.action_3:
        	GridLayoutManager manager3 = new GridLayoutManager(this,3);
        	manager3.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        		@Override
        		public int getSpanSize(int position) {
        			return (3 - position % 3);
        		}
        	});
        	recyclerView.setLayoutManager(manager3);
        	break;
        case R.id.action_4:
        	StaggeredGridLayoutManager manager4 = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        	recyclerView.setLayoutManager(manager4);
        	break;
        case R.id.action_5:
        	StaggeredGridLayoutManager manager5 = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL);
        	recyclerView.setLayoutManager(manager5);
        	break;
        }
        return true;
    }
    
}


具体看附件

你可能感兴趣的:(android)