UI控件

作者: tiger桂 | 来源:发表于2017-04-27 17:20 被阅读0次

RecyclerView

RecyclerView是一个更先进,更灵活的版本的ListView。

RecyclerView这个控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动。当你list中的元素经常动态改变时可以使用RecyclerView控件。

RecyclerView非常容易使用,它提供了如下两个功能:

为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager)

LinearLayoutManager

GridLayoutManager

StaggeredGridLayoutManager(加强的GridLayoutManager)

为每个条目设置了操作动画(RecyclerView.setItemAnimator)

XML 布局:

/layout

1

2

3

4

5

android:id="@+id/rv"

android:scrollbars="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

初始化RecyclerView参数,设置layoutManager和adapter

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_recycler_view);

// introduce

tvIntro = (TextView) findViewById(R.id.tv_intro);

tvIntro.setText(R.string.RecyclerViewActivityIntro);

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

// improve performance if you know that changes in content

// do not change the size of the RecyclerView

mRecyclerView.setHasFixedSize(true);

// use a linear layout manager

// 布局有三种

//     LinearLayoutManager

//     GridLayoutManager

//     StaggeredGridLayoutManager(加强的GridLayoutManager)

mLayoutManager =newLinearLayoutManager(this);

mRecyclerView.setLayoutManager(mLayoutManager);

// specify an adapter (see also next example)

initData();

mRecyclerViewAdapter =newRecyclerViewAdapter(mData);

mRecyclerView.setAdapter(mRecyclerViewAdapter);

}

创建adapter

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47publicclassRecyclerViewAdapterextendsRecyclerView.Adapter {

privateString[] mData;

// Provide a reference to the type of views that you are using

// (custom viewholder)

publicclassViewHolderextendsRecyclerView.ViewHolder {

publicTextView mTextView;

publicViewHolder(View v) {

super(v);

mTextView = (TextView) v;

}

}

// Provide a suitable constructor (depends on the kind of dataset)

publicRecyclerViewAdapter(String[] data) {

mData = data;

}

// Create new views (invoked by the layout manager)

@Override

publicRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,intviewType) {

// create a new view

View v = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent,false);

ViewHolder vh =newViewHolder(v);

returnvh;

}

// Replace the contents of a view (invoked by the layout manager)

@Override

publicvoidonBindViewHolder(ViewHolder holder,intposition) {

// - get element from your dataset at this position

// - replace the contents of the view with that element

Log.d(TAG,"Element "+ position);

holder.mTextView.setText(mData[position]);

}

// Return the size of your dataset (invoked by the layout manager)

@Override

publicintgetItemCount() {

if(null!= mData) {

returnmData.length;

}else{

return0;

}

}

}

CardView

CardView继承于FrameLayout类。CardView能让你的布局统一的显示在卡片布局中。(需要导入 v7CardView support 库)

XML实现:

XML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

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

... >

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

android:id="@+id/card_view"

android:layout_gravity="center"

android:layout_width="200dp"

android:layout_height="200dp"

card_view:cardCornerRadius="4dp">

android:id="@+id/info_text"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

Java Code实现:

Java

1

2

3

4// 设置曲率

mCardView.setRadius(progress);

// 设置高度 阴影

mCardView.setElevation(progress);

相关文章

  • UI常用控件

    UI常用控件 // // ViewController.m // UI常用控件 // // Created by ...

  • 自用命名规范

    UI命名规范 普通UI命名 控件类型_ui名称 UI绑定脚本命名 控件类型简称_ui绑定脚本名称_bind 文件规...

  • Android理解自定义View

    当Android SDK中提供的系统UI控件无法满足业务需求时,我们就需要考虑自己实现UI控件。 自定义UI控件有...

  • setText的优化

    Android的许多UI控件都有显示文字的功能,如TextView、EditText等UI控件。这些控件都通过se...

  • Android自定义View

    当Android SDK中提供的系统UI控件无法满足业务需求时,就需要考虑自己实现UI控件 自定义UI控件有2种方...

  • iOS资源汇总

    iOS·Objective-C UI控件详解整理 iOS UI控件详解—「UIScrollView滚动视图」 iO...

  • 第一行代码(2)UI设计

    1. 基础UI控件 Android中的基础UI控件有这样几种: TextView Button EditText ...

  • IOS规范,扎实基础(1)

    目录: UI Bars UI Views (用户界面视图) UI Controllers (用户界面控件) UI ...

  • Android为什么不允许在子线程中访问UI

    首先,UI控件不是线程安全的,如果多线程并发访问UI控件可能会出现不可预期的状态那为什么系统不对UI控件的访问加上...

  • 第五章 Android 常见UI基础控件(一)

    这里主要讲讲新的Android 基础的UI控件,此外还拓展下新的控件。所有的控件都是View的子类。常见的UI控件...

网友评论

      本文标题:UI控件

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