Android support.v7庫中的幾種控件,佈局控件

Android studio引用導包方式
1.在需要用到某個包的Module下的build.gradle文件中dependencies代碼塊中添加引用(本地沒有的包,Android studio會自動幫去下載)
這裏寫圖片描述
2.點擊工具欄setting(設置)右邊的Structure按鈕,選擇需要導包的module,點擊右邊的加號。減號爲移除引用。
這裏寫圖片描述
這裏寫圖片描述


以下示例都要導入相應的包

com.android.support:appcompat-v7包

android.support.v7.widget.Toolbar

使用方法:
1.引用v7 的appcompat 包
2.使用Toolbar的Activity要繼承AppCompatActivity
3.需要更改主題爲NoActionbBar的主題
4.在佈局文件中引用Toolbar , 需引用v7包中的Toolbar , 默認的Toolbar 僅支持 API >= 21 (android 5.0)的系統

<android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="4dp"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

5.在代碼中調用setSupportActionBar(toobar) 方法將Toolbar綁定到當前界面

示例:下文有


android.support.v7.widget.CardView

繼承自FrameLayout並實現了圓角和陰影效果,常用於ListView或RecyclerView中Item佈局的根節點
示例代碼:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:elevation="4dp"
    android:foreground="?android:attr/selectableItemBackground"
    android.support.v7.cardview:cardBackgroundColor="#999"
    android.support.v7.cardview:cardCornerRadius="15dp"
    android.support.v7.cardview:cardElevation="15dp"
    android.support.v7.cardview:contentPadding="0dp">

    <TextView
        android:id="@+id/item_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/hello" />
</android.support.v7.widget.CardView>

添加點擊波紋效果:

  android:clickable="true"
  android:foreground="?android:attr/selectableItemBackground"

com.android.support:recyclerview-v7包

recyclerview

高度解耦 , 異常靈活 , 可以用來代替ListView / GridView

相關類介紹:
這裏寫圖片描述

使用步驟:
1.導包
2.在佈局文件中添加RecyclerView
3.在Java代碼中實現RecyclerView, 至少要添加兩個屬性:
recyclerView.setLayoutManager(new LinearLayoutManager(this));//指定佈局管理器
recyclerView.setAdapter(adapter); //指定Adapter
4.Adapter的寫法
1)創建一個自定義的ViewHolder,在裏面初始化Item的每一個控件
2)讓自定義的Adapt而繼承RecyclerView.Adapter<傳入自定義的ViewHolder>
3)實現對應的方法

示例:

1.佈局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"/>

</RelativeLayout>

2.適配器類

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private Context context;
    private ArrayList<String> list;

    public RecyclerAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String s = list.get(position);
        holder.item_textView.setText(s);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView item_textView;

        public ViewHolder(View itemView) {
            super(itemView);
            item_textView = (TextView) itemView.findViewById(R.id.item_textView);
        }
    }
}

3.適配器用佈局視圖,recycler_item.xml
這裏寫圖片描述

4.活動代碼

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.rv);

        //佈局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //設置佈局管理器
        recyclerView.setLayoutManager(linearLayoutManager);
        ArrayList<String> list = new ArrayList<>();

        //模擬數據
        for (int i = 0; i < 20; i++) {
            list.add("SB---------------->>>>" + i);
        }

        RecyclerAdapter adapter = new RecyclerAdapter(list, this);
        recyclerView.setAdapter(adapter);//綁定適配器
    }
}

com.android.support:design包

Material Design 自定義主題常用屬性值
這裏寫圖片描述

1.FloatingActionButton

漂浮的Button,該控件父類爲ImageView,所以擁有ImageView的所有屬性

效果:
這裏寫圖片描述

常用屬性
android.support.design:fabSize=” ” 指定圖標的大小 值:normal / mini
android.support.design:elevation=”” 指定陰影的深度 int
app:layout_anchor=” ” 指定顯示座標的錨點
app:layout_anchorGravity=” ” 指定錨點的對齊方式

示例:
佈局文件:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:elevation = "10dp"
        android:layout_alignParentBottom="true">

</android.support.design.widget.FloatingActionButton>

說明:app:elevation = “10dp”的app包名爲

xmlns:app="http://schemas.android.com/apk/res-auto"

java代碼部分,將其取出

 FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);

2.TextInputLayout

用於在EditText上顯示Floating提示效果的控件。該控件內部必須包括有且只有一個EditText等輸入控件。

效果:
這裏寫圖片描述
這裏寫圖片描述

常用屬性值:
這裏寫圖片描述

注意:
android:hint需要設置在EditText身上
改變hint的顏色需要更改主題的colorAccent 顏色值

示例:
佈局文件

<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入用戶名"/>
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textInputLayout">

        <EditText
            android:id="@+id/edit_text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入密碼"/>
</android.support.design.widget.TextInputLayout>

java代碼:

final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
TextInputLayout textInputLayout2 = (TextInputLayout) findViewById(R.id.textInputLayout2);
EditText editText = (EditText) findViewById(R.id.edit_text);
EditText editText2 = (EditText) findViewById(R.id.edit_text2);

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String string = s.toString();
                if (string.length() > 6) {
                    textInputLayout.setErrorEnabled(true);
                    textInputLayout.setError("輸入長度不能大於6!");
                }
            }
});

3.Snackbar

介於Toast和AlertDialog之間的輕量級控件,方便的實現消息的提示和動作的反饋

效果:
這裏寫圖片描述

常用方法和屬性

Snackbar.make(View view, CharSequence text, int duration).show(); 
Snackbar.setAction();
Snackbar.dismiss()

注意事項:
1.構造函數中第一個參數不能是ScrollView,因爲SnackBar的實現邏輯是向view中addView,而ScrollView只能有一個childView
2.如果用SnackBar來代替Toast,需要注意的是Toast會浮在所有View之上,包括鍵盤.而SnakeView是在View之上addView的,所以要注意如果有鍵盤的時候,一定要先調用Keyboard.hide() , 否則鍵盤會將SnackBar遮住 .

示例:

final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(button, "我是Snackbar提示!",Snackbar.LENGTH_LONG);
                snackbar.setAction("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SnackbarActivity.this, "你點了Snackbar上的取消", Toast.LENGTH_SHORT).show();
                    }
                });
                snackbar.show();
            }
});

4.TabLayout

封裝好了tab頁和指示器
效果:
這裏寫圖片描述

常用方法及屬性

addTab(TabLayout.Tab tab, boolean setSelected)    //添加Tab
addTab(TabLayout.Tab tab, int position)
addTab(TabLayout.Tab tab, int position, boolean setSelected)
newTab()   //創建Tab
setOnTabSelectedListener(TabLayout.OnTabSelectedListener onTabSelectedListener)  //設置監聽
setTabTextColors(int normalColor, int selectedColor)  //設置被選中Tab的文字的顏色
setSelectedTabIndicatorColor(int color)   //設置被選中Tab指示條的顏色
setSelectedTabIndicatorHeight(int height)  //設置被選中Tab指示條的高度
setTabMode(int mode)  //設置Tab的模式  MODE_FIXED/MODE_SCROLLABLE

注意:
與ViewPager結合Fragment使用,PagerAdapter必須重寫getPageTitle()方法
設置適配器:

tabLayout.setupWithViewPager(ViewPager viewPager)  //必須在ViewPager.setAdapter() 之後調用
tabLayout.setTabsFromPagerAdapter(PagerAdapter adapter)

示例:
1.佈局文件

<android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tablayout">

</android.support.v4.view.ViewPager>

2.ViewPager的適配器

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        int p = position + 1;
        return MyFragment.getInstance("我是第" + p + "頁");
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title = "null";
        if (position == 0) {
            title = "Tab1";
        } else if (position == 1) {
            title = "Tab2";
        } else if (position == 2) {
            title = "Tab3";
        } else if (position == 3) {
            title = "Tab4";
        }
        return title;
    }
}

3.碎片Fragment類

public class MyFragment extends Fragment {
    public static Fragment getInstance(String s) {
        MyFragment fragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("content", s);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView textView = new TextView(inflater.getContext());

        String content = getArguments().getString("content");
        textView.setText(content);

        return textView;
    }
}

4.活動Activity代碼

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab4"));

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

        tabLayout.setupWithViewPager(viewPager);
}

5.NavigationView

類似SlidingMenu

效果:
這裏寫圖片描述

寫法步驟:
1.Android studio中,創建Activity選擇模板
這裏寫圖片描述

2.手動創建

常用屬性和方法

android:layout_gravity="left"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu"

app:itemBackground   //指定menu背景資源
app:itemIconTint     //指定menu Icon的顏色
app:itemTextColor    //指定menu item的字體顏色

說明:app屬性爲控件持有的屬性,需要導入包名xmlns:app="http://schemas.android.com/apk/res-auto"

注意:
1.自動生成的代碼中的Toolbar只能支持在API 21(android 5.0)以上
2.點擊某一項後自動隱藏:drawerLayout.closeDrawers();

示例:
手動創建
1.主佈局文件,根節點標籤爲android.support.v4.widget.DrawerLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.supportdemo.navigationview.NaviActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="18sp"/>
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"
        />

</android.support.v4.widget.DrawerLayout>

2.頭部部分佈局文件,drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/colorPrimaryDark"
                android:padding="15dp">

    <TextView
        android:id="@+id/desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="[email protected]"
        android:textSize="18sp"/>

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@id/desc"
        android:layout_marginBottom="30dp"
        android:src="@mipmap/ic_launcher"/>
</RelativeLayout>

3.主體部分,Item項menu資源文件,drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group>

        <item
            android:id="@+id/bar"
            android:title="Bar"/>
        <item
            android:id="@+id/car"
            android:title="Car"/>
        <item
            android:id="@+id/Mute"
            android:title="Mute"/>
        <item
            android:id="@+id/Up"
            android:title="Up"/>

    </group>
    <item android:title="Settings">
        <menu>
            <item
                android:id="@+id/Mute1"
                android:title="Mute"/>
            <item
                android:id="@+id/Up1"
                android:title="Up"/>

        </menu>
    </item>
</menu>

4.活動Activity代碼

public class NaviActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navi);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
//        setSupportActionBar(toolbar);

        final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        final NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);

        final TextView textView = (TextView) findViewById(R.id.textView1);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R
                .string.app_name, R.string.app_name);
        drawerLayout.setDrawerListener(toggle);//添加監聽,自動幫我們處理事項
        toggle.syncState();//異步啓動

        navigationView.setNavigationItemSelectedListener(new NavigationView
                .OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                CharSequence title = item.getTitle();
                textView.setText(title);
                drawerLayout.closeDrawers();//設置點擊後自動隱藏
                return true;
            }
        });
    }
}

效果圖:
這裏寫圖片描述


以下6、7、8結合使用

6.CoordinatorLayout

更加強大的FrameLayout。

主要用於以下情形:
1.作爲一個佈局的根佈局
2.作爲一個爲childView之間協調手勢效果的協調視圖

主要屬性:
app:layout_scrollFlags:控制那個childView可滑動
屬性值:
1.scroll: 所有想滾動出屏幕的view都需要設置這個flag,沒有設置這個flag的view將被固定在屏幕頂部
2.enterAlways: 這個flag讓任意向下的滾動都會導致該view變爲可見,啓用快速“返回模式”
3.enterAlwaysCollapsed: 當你的視圖已經設置minHeight屬性又使用此標誌時,你的視圖只能以最小高度進入,只有當滾動視圖到達頂部時才擴大到完整高度
4.exitUntilCollapsed: 滾動退出屏幕,最後摺疊在頂端

7.AppBarLayout

一個垂直方向的LinearLayout,實現了Matrerial Design效果的App Bar,支持滑動手勢操作。
注意事項:
1.必須作爲CoordinatorLayout的childView使用,否則很多特效無法實現。
2.同級控件中,必須有一個可滾動的siblingView,不然你用我幹嘛呢?

實現滑動的步驟:
1.CoordinatorLayout必須作爲整個佈局的父佈局容器
2.CoordinatorLayout佈局下包裹一個可以滑動的佈局,比如 RecyclerView,NestedScrollView(ListView,ScrollView不支持)
3.給AppBarLayout設置app:layout_scrollFlags=”scroll|enterAlways” 屬性
4.給可滑動的組件,也就是RecyclerView 或者 NestedScrollView 設置如下屬性:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

8.CollapsingToolbarLayout

提供一個可摺疊的Toolbar容器,對容器中的不同視圖設置layout_collapseMode摺疊模式,來達到不同的摺疊效果

常用屬性:
這裏寫圖片描述
這裏寫圖片描述

指定childView摺疊模式使用

app:layout_collapseMode

值:1.parallax 視差模式 , 需要增加 app:layout_collapseParallaxMultiplier 屬性 , 屬性值範圍爲 0.0-1.0 , 必須是float類型
2.pin 固定模式

注意事項:
1.CollapsingToolbarLayout的高度必須是固定值而且要大於Toolbar的高度 , 不能使用wrap_content , 否則不會有摺疊效果
2.所有需要摺疊的childView必須指定app:layout_collapseMode 屬性
3.如果要添加一個自動摺疊的FloatingActionBar , 必須指定錨點,錨點需爲CollapsingToolbarLayout的父容器
使用parallax模式的時候,app:layout_scrollFlags的屬性值不能包含enterAlwaysCollapsed或enterAlways

6、7、8示例:
1.佈局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.supportdemo.coordinate.CoordinateActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                />
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/appBarlayout"
        app:layout_anchorGravity="bottom|right"
        />
</android.support.design.widget.CoordinatorLayout>

2.活動java代碼

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinate);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar); //Toolbar 5.0以上才支持

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            list.add("This is " + i);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerAdapter adapter = new RecyclerAdapter(list, this);
        recyclerView.setAdapter(adapter);
}

另附Adapter代碼

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private Context context;
    private ArrayList<String> list;

    public RecyclerAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String s = list.get(position);
        holder.item_textView.setText(s);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView item_textView;

        public ViewHolder(View itemView) {
            super(itemView);
            item_textView = (TextView) itemView.findViewById(R.id.item_textView);
        }
    }
}

效果:
這裏寫圖片描述
這裏寫圖片描述

發佈了60 篇原創文章 · 獲贊 22 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章