Android RecyclerView and recyclerview

Source: Internet
Author: User

Android RecyclerView and recyclerview

The listview and gridview widgets must be familiar with the development of the components, such as list display, use listview,

Gridview and RecyclerView are all possible. What are their differences? Recently, my own business needs to use the list display, so which one to use knows that RecyclerView is one of Google's

The listview is a traditional control, and the RecyclerView can be used to replace the traditional listview. It is more powerful than the traditional listview. After querying a large amount of data on the Internet, it is concluded that:

If you need to support animations on the list page, you need to update them frequently and refresh the page. We recommend that you use RecyclerView. In other cases, both are OK, but ListView is more convenient and quick to use. If you use listvie

Partial refresh. I believe many children's shoes will remember yydatasetchanged (). We all know that after updating the listview data source, we need to call the notifyDataSetChanged () of the Adapter to tell the view more about listview.

New, so it needs to re-paint each Item. In fact, we don't need to be so tedious. The most typical example is like a friend's likes. We only need to update the items currently liked, not all updates are required, but the listview does not

This API is provided so that RecyclerView is available. RecyclerView provides RecyclerView. Adapter to refresh a single Item. Let's talk about this first. Let's talk about RecyclerView.

This is the topic of this article.

 

RecyclerView is on stage.

Simple use of RecyclerView:

(1) reference the jar package (mainly in conflict with the jar package of the original project). The previous Android Studio version was 2.2.2. The version 2.3.3 is used because of the conflict. I don't know if anyone has met me. Maybe it's amazing.

(2) declare in the layout File

<Android. support. v7.widget. RecyclerView
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
</Android. support. v7.widget. RecyclerView>

(3) Use in Activity
"RecyclerView mRecyclerView = (RecyclerView) getActivity (). findViewById)

MRecyclerView. setHasFixedSize (true); // (optional) If you can determine that the height of each item is fixed, setting this option can improve performance.
"
// Set LayoutManager
LinearLayoutManager mLayoutManager = new LinearLayoutManager (this );
MRecyclerView. setLayoutManager (mLayoutManager );

MRecyclerView. setItemAnimator (new DefaultItemAnimator (); // sets the animation
The ItemAnimator subclass is used to manage ViewHolder animations. It must call the methods above for management. Finally, an official animated class has been released, which can be used directly: DefaultItemAnimator ()
Animation effect on GitHub: RecyclerViewItemAnimators

MAdapter = new BoxProdAdapter (getActivity (), 0 );
MRecyclerView. setAdapter (mAdapter );

》mAdapter.setOnItemClickListener(mOnItemClickListener);
Private BoxProdAdapter. OnItemClickListener mOnItemClickListener = new BoxProdAdapter. OnItemClickListener (){
@ Override
Public void onItemClick (View view, int position ){
ShowDeleteDialog (position); // Delete the selected row
}
};


(4) Create an Adapter
public class BoxProdAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Private ArrayList <BoxProdInfo> mDatas = new ArrayList <> ();
Private OnItemClickListener mOnItemClickListener;
Private Context mContext;
Private int mSwitchFragment; // record the page on which the scan is started

     public BoxProdAdapter(Context context, int switchFragment) {
this.mContext = context;
mSwitchFragment = switchFragment;
}
// Create a new View, called by LayoutManager
@ Override
Public RecyclerView. ViewHolder onCreateViewHolder (ViewGroup parent, int viewType ){
// The product box Association. The displayed column is the row number, product code, and box barcode.
If (mSwitchFragment = 1 ){
View view = LayoutInflater. from (parent. getContext (). inflate (R. layout. box_prod_item, parent, false );
ItemViewHolder viewHolder = new ItemViewHolder (view );
Return viewHolder;
}
Else if (mSwitchFragment = 2 ){
View view = LayoutInflater. from (parent. getContext (). inflate (R. layout. prod_item, parent, false );
ItemViewHolder viewHolder = new ItemViewHolder (view );
Return viewHolder;
}
// Other columns shown in the list: row number, box encoding, and scan time
Else {
View view = LayoutInflater. from (parent. getContext (). inflate (R. layout. box_item, parent, false );
ItemViewHolder viewHolder = new ItemViewHolder (view );
Return viewHolder;
}

// Bind data to the interface
@ Override
Public void onBindViewHolder (RecyclerView. ViewHolder holder, int position ){
If (holder instanceof ItemViewHolder ){
BoxProdInfo data = mDatas. get (position );
// Set the displayed data
If (mSwitchFragment = 1 ){
(ItemViewHolder) holder). mLineNo. setText (Integer. toString (data. getLINE_NO (); // The int type must be converted to a string.
(ItemViewHolder) holder). mProdCode. setText (data. getPROD_CODE ());
(ItemViewHolder) holder). mBoxCode. setText (data. getBOX_CODE ());
} Else {
(ItemViewHolder) holder). mLineNo. setText (Integer. toString (data. getLINE_NO (); // The int type must be converted to a string.
(ItemViewHolder) holder). mScanTime. setText (data. getSCAN_TIME ());
(ItemViewHolder) holder). mBoxCode. setText (data. getBOX_CODE ());
}
}
}

// Number of Retrieved Data
@ Override
Public int getItemCount (){
Return mDatas. size ();
}

// Click the event
Interface defined in 1
Public interface OnItemClickListener {
Void onItemClick (View view, int position );
}
How to add and set interfaces in Article 2

Public void setOnItemClickListener (OnItemClickListener onItemClickListener ){
This. mOnItemClickListener = onItemClickListener;
}
3. onClick in Apater
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mProdCode, mLineNo, mBoxCode, mScanTime;

public ItemViewHolder(View itemView) {
super(itemView);
if (mSwitchFragment == 1) {
mProdCode = (TextView) itemView.findViewById(R.id.tx_prod_code);
mLineNo = (TextView) itemView.findViewById(R.id.tx_line_no);
mBoxCode = (TextView) itemView.findViewById(R.id.tx_box_code);
} else {
mScanTime = (TextView) itemView.findViewById(R.id.tx_scan_time);
mLineNo = (TextView) itemView.findViewById(R.id.tx_line_no);
mBoxCode = (TextView) itemView.findViewById(R.id.tx_box_code);
}
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, this.getAdapterPosition());
}
}
}
// Add and remove
public void addData(BoxProdInfo info) {
boolean isNotExist = true;
for (BoxProdInfo i : mDatas) {
if (i.getBOX_CODE().equals(info.getBOX_CODE())) {
isNotExist = false;
break;
}
}
if (isNotExist) {
info.setLINE_NO(mDatas.size() + 1);
this.mDatas.add(info);
this.notifyDataSetChanged();
}
}

public void removeItem(int position) {
this.mDatas.remove(position);
for (int i = 0; i < mDatas.size(); i++) {
mDatas.get(i).setLINE_NO(i + 1);
}
this.notifyDataSetChanged();
}







}



 


 
 





 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.