美文网首页RecyclerView
Recyclerview基本使用<1>

Recyclerview基本使用<1>

作者: 天空在微笑 | 来源:发表于2017-05-16 17:24 被阅读21次

想写几篇关于Recyclerview的文章,对Recyclerview做一个系统的总结。不过万事起于忽微,来先看看Recyclerview的最基本的使用。

1.看看最终效果

LinearLayoutManager.jpg
GridLayoutManager.jpg
StaggeredLayoutManager.jpg

2.LinearLayoutManagerRecyclerview.activity



package signin.company.com.recyclerviewdemo.simpleuse;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import signin.company.com.recyclerviewdemo.R;

public class LinearLayoutManagerRecyclerview extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private TestAdapter mAdapter;
    public static void newInstance(Context context) {
        context.startActivity(new Intent(context, LinearLayoutManagerRecyclerview.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_recyclerview);
        initData();
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
//mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
//mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter = new TestAdapter());
    }

    protected void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 1; i < 60; i++) {
            mDatas.add(String.valueOf(i));
        }
    }

    class TestAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                    LinearLayoutManagerRecyclerview.this).inflate(R.layout.item_text, parent,
                    false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText(mDatas.get(position));
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {

            TextView tv;

            public MyViewHolder(View view) {
                super(view);
                tv = (TextView) view.findViewById(R.id.id_num);
            }
        }
    }
}


3.activity_single_recyclerview.xml

<?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:id="@+id/id_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

4.item_text.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EFF5F5">

    <TextView
        android:id="@+id/id_num"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="20sp"
        android:gravity="center"
        android:text="1" />
</FrameLayout>

相关文章

网友评论

    本文标题:Recyclerview基本使用<1>

    本文链接:https://www.haomeiwen.com/subject/zjilxxtx.html