In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use RecyclerView scroll control in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The use of RecyclerView is more complicated than that of ListView. The use of ListView is five steps, while the use of our RecyclerView has seven steps, which are:
1. Add compile 'com.android.support.recyclerview-v7:xx.x.x' to the dependencies closure in the build.gradle of the current project (x is the current latest version)
two。 Layouts add RecyclerView controls and create child layouts and adapter classes.
3. Create an adapter
4. Define data sources
5. Find the RecylerView list control through findViewById
6. Set the layout manager for RecylerView through setLayoutManager ()
7. Set the adapter through setAdapter ()
Let's use the code or picture to describe each step one by one:
* * step 1: add compile 'com.android.support.recyclerview-v7:xx.x.x' (x is the latest version) to the dependencies closure in the build.gradle of the current project.
Because Android defines RecyclerView in the support library, you must add a corresponding dependent library if you want to use the control. So there is the first step of operation. Here are two ways to add library dependencies: * *
1. Add it manually directly in the file, and also add it manually:
Remember to save after adding, and then click the Sync Now below to synchronize. What? What? Sync Now doesn't know where it is? 0.0
two。 Open the layout file:
When the system is added, you will find that there is still nothing in the layout interface. Don't worry! At this time, when you drag the RecyclerView control to the layout in the options bar, you will find the following interface, which proves that the library dependency was added successfully. You can use the RecyclerView control:
Compared with the first method, the second method is more convenient, it does not need to enter manually and know the current version, and there is less error rate.
Step 2: add RecyclerView controls to the layout and create child layouts and adapter classes. In fact, we have already said the first step, just drag the control directly to the layout interface.
-Let's go to the layout code and find the following code snippet in the layout code:
Friends, have you found any features among them? Yes, that is, how does RecyclerView differ from the header label of our Button, TextView and other controls? That's because RecyclerView is not built into the system SDK, so you need to write the full package path name android.support.v7.widget.RecyclerView.
Note: direct drag does not generate ID, so we need to add it manually. If you add RecyclerView manually, remember to write down the path of the completion package name.
In the layout file, we need to create a child layout of RecyclerView, as follows:
To achieve the preview effect, we let the interface realize that there are pictures on the left and text on the right:
Let's create a Fruit entity class and add a constructor and override the get () method:
Finally, create a class for the FruitAdapter adapter, let the adapter inherit RecyclerView.Adapter, and specify the generics as FruitAdapter.ViewHolder. Where ViewHolder is an inner class defined in our FruitAdapter. And rewrite: onCreateViewHolder (), onBindViewHolder (), getItemCount () three methods. The full code is as follows:
Public class FruitAdapter extends RecyclerView.Adapter {/ / define collection private List mFruitList; / / use constructor to pass in data public FruitAdapter (List FruitList) {this.mFruitList = FruitList } / / create an instance of ViewHolder / / ① onCreateViewHolder () is used to create the ViewHolder instance and pass the loaded layout to the constructor @ Override public FruitAdapet.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {/ / because the ViewHolder constructor needs to pass in the child layout View object, so you need to instantiate the child layout View view = LayoutInflater.from (parent.getContext ()) .propagate (R.layout.activity_fruit, parent, false) / / create ViewHolder object ViewHolder viewHolder = new ViewHolder (view); / / return value return viewHolder;} / / ② onBindViewHolder () is used to assign the data of RecyclerView sub-items. @ Override public void onBindViewHolder (final FruitAdapet.ViewHolder holder, int position) {/ / take the fruit object out of the collection Fruit fruit = mFruitList.get (position) when each sub-item is scrolled into the screen. / / call holder.ImageView object to set picture holder.imageView.setImageResource (fruit.getImageId ()); / / call holder.TextView object to set text holder.textView.setText (fruit.getName ());} / / ③ getItemCount () to know how many children RecyclerView has @ Override public int getItemCount () {return mFruitList.size ();} / / create ViewHolder inherit RecyclerView.ViewHolder class ViewHolder extends RecyclerView.ViewHolder {ImageView imageView; TextView textView / / override ViewHolder public ViewHolder (View itemView) {super (itemView); / / the controls that get the child layout through the findViewById method of the View object are stored in the member variables imageView = (ImageView) itemView.findViewById (R.id.imageView); textView = (TextView) itemView.findViewById (R.id.textView);}
3. Define data sources in MainActivity 4 define adapters
5. Find the RecylerView list control through findViewById
6. Set the layout manager for RecylerView through setLayoutManager ()
7. Create an adapter
8. Set the adapter through setAdapter ()
Public class MainActivity extends AppCompatActivity {private List fruitList; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / step 3: define the data source, using a separate method fruitData (); / / step 4: create the adapter FruitAdapet adapet = new FruitAdapet (fruitList); / / step 5: find the layout control RecyclerView recyclerView= (RecyclerView) findViewById (R.id.recyclerView) / / step 6: set the layout manager / / to vertical / / horizontal parameters for RecylerView through setLayoutManager (): (this,LinearLayoutManager.HORIZONTAL, false); LinearLayoutManager linearLayoutManager=new LinearLayoutManager (this, LinearLayoutManager.VERTICAL,false); recyclerView.setLayoutManager (linearLayoutManager); / / step 7: set adapter recyclerView.setAdapter (adapet) } public void fruitData () {/ / create a collection to save data fruitList = new ArrayList (); / / I just use a few pictures and let him change them in a loop. Friends can add all kinds of pictures Fruit apple = new Fruit ("Apple", R.drawable.apple); fruitList.add (apple); Fruit banana = new Fruit ("banana", R.drawable.banana); fruitList.add (banana). Fruit apple1 = new Fruit ("R.drawable.apple"); fruitList.add (apple); Fruit banana1 = new Fruit ("banana", R.drawable.banana); fruitList.add (banana); Fruit apple2 = new Fruit ("apple", R.drawable.apple); fruitList.add (apple); Fruit banana2 = new Fruit ("banana", R.drawable.banana); fruitList.add (banana) Fruit apple3 = new Fruit ("R.drawable.apple"); fruitList.add (apple); Fruit banana3 = new Fruit ("banana", R.drawable.banana); fruitList.add (banana); Fruit apple4 = new Fruit ("apple", R.drawable.apple); fruitList.add (apple); Fruit banana4 = new Fruit ("banana", R.drawable.banana); fruitList.add (banana);}}
All right, the basic code of RecyclerView is just like this, for reference only.
In fact, there are click events in RecyclerView, which I didn't write here. You can add it if your friends need it later.
In fact, this is also the code to achieve the waterfall flow effect, if we want to achieve the waterfall flow effect, we only need to set the sub-item layout to display only the picture (the waterfall flow needs to use pictures of different sizes to see a better effect). And adjust the layout parameters. Set the following code in layout settings layout management:
/ / the first parameter is used to specify the number of columns of the layout / the second parameter is used to specify the arrangement direction of the layout StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager (3Magical StaggeredGridLayoutManager.Vertical); recyclerView.setLayoutManager (layoutManager); "how to use the RecyclerView scroll control in Android" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.