Since I contacted Android studio, I have a new understanding and desire to learn and master the new Android UI design concept and V7 package. Therefore, I have a special look at the use of the new control RecycleView, which is summarized as follows.
Its usage is similar to that of listview. It consists of three parts: adapter, arrangement and layout, and execution of animation. Among them, adapter does not need to inherit the property of RecyclerView.Adapter from baseadapter. This is a method stipulated and recommended by Google. View The main use of holder is to declare the layout file in the item. Here, MyViewHolder inherits from ViewHolder, and I use it directly in adapter as its internal class.
In addition, recycleview can realize the layout of list view, gridview and waterfall flow. Therefore, in addition to setting its adapter, you need to declare its layout attribute. The layout manager, LinearLayoutManager, has no significant difference with the use of list view, and directly pastes the code:
Primary activity file:
private RecyclerView mRecycleView;
private List<String> mDatas;
private com.im.ui.adapter.SimpleAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_first , container , false);
//Instantiate control
mRecycleView = (RecyclerView) v.findViewById(R.id.recycle);
//Layout manager.VERTICAL: list Set to vertical
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity() , LinearLayoutManager.VERTICAL , false);
mRecycleView.setLayoutManager(linearLayoutManager);
mAdapter = new com.im.ui.adapter.SimpleAdapter(getActivity() , mDatas);
mRecycleView.setAdapter(mAdapter);
//Set split line, DividerItemDecoration: a split line class on GitHub, which can be searched by yourself
mRecycleView.addItemDecoration(new DividerItemDecoration(getActivity() , DividerItemDecoration.VERTICAL_LIST));
return v;
}
- 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
Adapter file:
public class SimpleAdapter extends RecyclerView.Adapter<MyViewHolder>{
private LayoutInflater mInflater;
private Context mContext;
private List<String> mDatas;
public SimpleAdapter(Context context , List<String> datas){
this.mContext = context;
this.mDatas = datas;
mInflater = LayoutInflater.from(context);
}
//Create ViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_first , parent , false);
MyViewHolder viewHolder = new MyViewHolder(view );
return viewHolder;
}
//Bind a ViewHolder
@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 itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv_item_first);
}
}
- 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
xml:
<android.support.v7.widget.RecyclerView
android:id="@+id/test_recycle"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>