【3】 Android RecyclerView使用

<pre name="code" class="java"><strong><span style="font-size:24px;"> 1</span>.使用RecyclerView需要添加的包</strong>
<strong>import android.support.v7.widget.RecyclerView;</strong>

// 代码如下
 
<pre name="code" class="java">public class MainActivity extends AppCompatActivity {
    protected RecyclerView recyclerVierr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        recyclerVierr = (RecyclerView) findViewById(R.id.recyclerVierr);
        RecyAdapter adapter = new RecyAdapter();
        ArrayList<String>data=new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add("嗨"+i);
        }
        adapter.setMdata(data);
        recyclerVierr.setAdapter(adapter);
        //<strong> 下边是三种可任意实现的状态设置</strong>
        //************************************************ ListView 条目布局
            // LinearLayoutManager layout = new LinearLayoutManager(this);
            // recyclerVierr.setLayoutManager(layout);

     //********************************************************** GridView  九宫格布局
            //  GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
            // recyclerVierr.setLayoutManager(gridLayoutManager);

        //************************************************************
        // 瀑布流 (瀑布流是根据图片大小改变如果图片大小一样效果和九宫格一样)
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL);
         recyclerVierr.setLayoutManager(staggeredGridLayoutManager);
    }
}
 
 
<strong><span style="font-size:24px;">2</span>.RecyclerAdapter 代码如下</strong>
<pre name="code" class="java">public class RecyAdapter extends RecyclerView.Adapter<VH> {
    ArrayList<String>mdata;
    public void setMdata(ArrayList<String> mdata) {
        this.mdata = mdata;
    }
    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(parent.getContext(), R.layout.recycler, null);
        VH vh = new VH(inflate);
        return vh;
    }
    @Override
    public void onBindViewHolder(VH holder, int position) {
 holder.mtextview.setText(mdata.get(position));
    }
    @Override
    public int getItemCount() {
        return mdata.size();
    }
}

3.VH代码
 
<strong></strong><pre name="code" class="java">public class VH extends RecyclerView.ViewHolder {
    TextView mtextview;
    public VH(View itemView) {
        super(itemView);
        mtextview= (TextView) itemView.findViewById(R.id.text);
    }
}


 
<strong><span style="font-size:18px;">4</span>.XML布局如下 Adapter需要定义的XML</strong>
<strong></strong><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'宋体';font-size:13.5pt;"><span style="color:#e8bf6a;"><LinearLayout </span><span style="color:#bababa;">xmlns:</span><span style="color:#9876aa;">android</span><span style="color:#bababa;">=</span><span style="color:#6a8759;">"http://schemas.android.com/apk/res/android"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:orientation=</span><span style="color:#6a8759;">"vertical"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:layout_width=</span><span style="color:#6a8759;">"match_parent"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:padding=</span><span style="color:#6a8759;">"5dp"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:layout_height=</span><span style="color:#6a8759;">"match_parent"</span><span style="color:#e8bf6a;">>
</span><span style="color:#e8bf6a;"><TextView
</span><span style="color:#e8bf6a;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:id=</span><span style="color:#6a8759;">"@+id/text"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:layout_width=</span><span style="color:#6a8759;">"match_parent"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:layout_height=</span><span style="color:#6a8759;">"150dp"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:background=</span><span style="color:#6a8759;">"#97EF03"
</span><span style="color:#6a8759;">    </span><span style="color:#9876aa;">android</span><span style="color:#bababa;">:text=</span><span style="color:#6a8759;">"嗨"</span><span style="color:#e8bf6a;">/>
</span><span style="color:#e8bf6a;"></LinearLayout></span>

 
<strong><span style="font-size:18px;">5</span> MainActivity 布局</strong>
<strong></strong><pre name="code" class="html"><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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.recycledview10_8.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerVierr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值