android recycle 的简单用法

转载请标明出处:
http://blog.csdn.net/lmj623565791/article/details/45059587
本文出自:【张鸿洋的博客】

开始—>

这是我的整体布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycle">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

这是item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="#5abfed"
        android:textColor="#ffffff"
        android:text="1"
        android:id="@+id/text_char"/>
</LinearLayout>

我的activity

public class Recycler extends Activity {

    private List<String> mDatas;
    private RecyclerView recyclerView = null;
    private HomeAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler);
        recyclerView = (RecyclerView) findViewById(R.id.recycle);

        //初始化数据
        initData();
        /*
         *设置布局管理器
         *1.linearlayout管理器
         *2.gridlayout管理器
         *3.横向的gridlayout管理器
         *4.纵向的gridlayout管理器
         */

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        //设置adapter
        recyclerView.setAdapter(adapter = new HomeAdapter());
    }

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


    //adapter的写法
    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>
    {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(
                    LayoutInflater.from(Recycler.this).inflate(R.layout.recycle_item,
                    parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText(mDatas.get(position));
            ViewGroup.LayoutParams layoutParams = holder.tv.getLayoutParams();
            layoutParams.height = (int) (Math.random()*100 + 100);
            Log.i("xjxu", layoutParams.height + "");
            holder.tv.setLayoutParams(layoutParams);
        }

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

        public void addData(int position) {
            mDatas.add(position, "Insert One");
            notifyItemInserted(position);
        }

        public void removeData(int position) {
            mDatas.remove(position);
            notifyItemRemoved(position);
        }
        class MyViewHolder extends RecyclerView.ViewHolder
        {
            TextView tv;
            public MyViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.text_char);
            }
        }
    }
}

添加和删除数据的时候只需要在你用的地方
适当的调用adapter.addData(position); 或者 adapter.removeData(position);

good 完成 我只有简单的用法,如果想深入了解,请去开始的标注的博客

阅读更多
换一批

没有更多推荐了,返回首页