• RecyclerView的用法


    MainActivity的代码如下:

    package com.example.zhangmeng.recyclerview;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    
    public class MainActivity extends AppCompatActivity {
        private RecyclerView rv;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            rv=new RecyclerView(this);
            setContentView(rv);
            rv.setLayoutManager(new LinearLayoutManager(this));
            rv.setAdapter(new MyAdapter());
        }
    
    }
    

      MyAdapter:

     1 package com.example.zhangmeng.recyclerview;
     2 
     3 import android.support.v7.widget.RecyclerView;
     4 import android.view.LayoutInflater;
     5 import android.view.View;
     6 import android.view.ViewGroup;
     7 import android.widget.TextView;
     8 
     9 /**
    10  * Created by zhangmeng on 2016/9/15.
    11  */
    12 class MyAdapter extends RecyclerView.Adapter {
    13 
    14 
    15     @Override
    16     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    17 
    18         return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null));
    19     }
    20 
    21     @Override
    22     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    23         ViewHolder vh = (ViewHolder) holder;
    24         vh.getTv_title().setText("Title"+position);
    25         vh.getTv_content().setText("content"+position);
    26 
    27     }
    28 
    29     @Override
    30     public int getItemCount() {
    31         return 10;
    32     }
    33 }
    34 
    35 class ViewHolder extends RecyclerView.ViewHolder {
    36     private TextView tv_title;
    37     private TextView tv_content;
    38     public ViewHolder(View itemView) {
    39         super(itemView);
    40         tv_title=(TextView) itemView.findViewById(R.id.textView_title);
    41         tv_content=(TextView) itemView.findViewById(R.id.textView_content);
    42     }
    43 
    44     public TextView getTv_title() {
    45         return tv_title;
    46     }
    47     public TextView getTv_content() { return tv_content; }
    48 
    49 
    50 }

    Activitylayout:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.zhangmeng.recyclerview.MainActivity">
    11 
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="Hello World!" />
    16 </RelativeLayout>

    Itemlayout:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical" android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5 
     6     <TextView
     7                 android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:textAppearance="?android:attr/textAppearanceLarge"
    10         android:text="Large Text"
    11         android:id="@+id/textView_title"
    12         android:layout_gravity="center_horizontal" />
    13 
    14     <TextView
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:textAppearance="?android:attr/textAppearanceSmall"
    18         android:text="Small Text"
    19         android:id="@+id/textView_content" />
    20 </LinearLayout>
    View Code
  • 相关阅读:
    wenbao与manacher
    wenbao与拓扑排序
    wenbao与邻接表
    wenbao与最短路floyd
    wenbao与最短路dij
    wenbao与后缀自动机(SAM)
    wenbao与欧拉回路
    wenbao与字符串Hash
    wenbao与后缀数组
    wenbao与扩展kmp
  • 原文地址:https://www.cnblogs.com/WebGiant/p/5877170.html
Copyright © 2020-2023  润新知