- Pull to Refresh with RecyclerView in Android with Example
- Example
- Step by Step Implementation
- Implementing Pull to Refresh Guide
- Swipe To Refresh RecyclerView Android Example | Pull To Refresh
- Step 1. Adding Gradle File Lines
- Step 2. Single Drawable File
- Step 3. Specific XML layout file
- Step 4. Model For DataSet
- Step 5. Making Adapter
- Step 6. Final Writings of Example
- More Details on Main File
- Swipe Down to Refresh ListView
- psydj1 / Event.java
Pull to Refresh with RecyclerView in Android with Example
The SwipeRefreshLayout widget is used for implementing a swipe-to-refresh user interface design pattern. Where the user uses the vertical swipe gesture to refresh the content of the views. The vertical swipe is detected by the SwipeRefreshLayout widget and it displays a distinct progress bar and triggers the callback methods in the app. In order to use this behavior, we need to use the SwipeRefreshLayout widget as the parent of a ListView or GridView. These Material Design UI Patterns are seen in applications like Gmail, Youtube, Facebook, Instagram, etc. It allows the user to refresh the application manually. SwipeRefreshLayout class contains a listener called OnRefreshListener. The classes which want to use this listener should implement SwipeRefreshLayout.OnRefreshListener interface. On vertical swipe-down gesture, this listener is triggered and onRefresh() method is called and can be overridden according to the needs.
Example
In this example, we would store data into the ArrayList which is used for populating the RecyclerView. Whenever onRefresh() method is called the ArrayList data gets rearranged. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding dependencies
Источник
Implementing Pull to Refresh Guide
In Android, the common «pull to refresh» UX concept is not built in to a ListView/RecyclerView. However, many Android applications would like to make use of this concept for their feeds. This is useful for all sorts of feeds such as a Twitter timeline. This effect can be achieved using the SwipeRefreshLayout class
SwipeRefreshLayout is a ViewGroup that can hold only one scrollable view as a child. This can be either a ScrollView or an AdapterView such as a ListView or a RecyclerView .
Edit your app/build.gradle file to include a library:
Make sure your libraries is up to date by adding to your root gradle.file :
Just like the previous section, wrap the scrollable view, in this case a RecyclerView with a SwipeRefreshLayout in the XML layout:
Make sure to have helper methods in your RecyclerView adapter to clear items from the underlying dataset or add items to it.
Next, we need to configure the SwipeRefreshLayout during view initialization in the activity. The activity that instantiates SwipeRefreshLayout should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed.
The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content.
Next, we need to configure the SwipeRefreshLayout during view initialization in the activity:
Note that upon successful reload, we must also signal that the refresh has completed by calling setRefreshing(false) . Also note that you should clear out old items before appending the new ones during a refresh.
If you are using SwipeRefreshLayout with Android’s new Paging Library, the data sources used to provide data to the RecyclerView need to be invalidated. Review this guide for more information.
Note: ListView is an old UI component that is no longer used in modern Android applications. Only refer this guide if you intend to update some old code that still relies on ListView.
Set SwipeRefreshLayout at the Layout you want the SwipeRefresh functionality
activity_main.xml
activity_main.xml
You could use a ScrollView instead a ListView
In the activity who points to activity_main.xml, which is main_activity(in this example), this code should be enough
main_activity.java
Now just run your application!
You could check this example on GitHub.
If you aren’t able to get the swipe to refresh working, check the following tips:
Did you accidentally call setContentView twice? Ensure that inside your activity, you’ve only called setContentView once as the 2nd line of your onCreate method.
Did you invoke setRefreshing(false) after data finished loading? With the swipe to refresh control, you are responsible for notifying the system once the new data has been loaded into the list. You must make sure to invoke setRefreshing only after the data has come back and not before. This means if you are loading data from the network, calling this within the onSuccess method.
Did you clear out the old items before updating the list? Make sure that in order for the new items to be displayed that you clear the list of any old items if needed. In other words, if you are replacing items in the list with new versions, be sure to remove the old versions from the adapter first with adapter.clear();
Are you using CoordinatorLayout? If you are using a CoordinatorLayout to manage scrolling, be sure to move the app:layout_behavior=»@string/appbar_scrolling_view_behavior» property to the SwipeRefreshLayout rather than the child RecyclerView or ListView .
Источник
Swipe To Refresh RecyclerView Android Example | Pull To Refresh
Welcome to Swipe To Refresh RecyclerView Android Example.
This Pull/swipe To Refresh RecyclerView Android Example will answer your question like “how to refresh recyclerview in android”
Swipe or pull to refresh recyclerview feature provides easy way to handle data which are updating frequently.
In this example, we will use three different lists to implement refresh facility.
One list is of cars, second is of player names and third one is of country names. I have created video for more reference.
First of all, go through the below video to show the final outcome.
Step 1. Adding Gradle File Lines
Create a new project in the android studio.
For using RecyclerView and CardView, you need to add two lines into your build.gradle(Module:app) file.
Below are those two lines,
First one is for RecyclerView and second one is for CardView.
These lines will add some classes to our projects which we can use directly in our Java files.
Step 2. Single Drawable File
Now go to app->res->drawable directory and create a new file called cardview.xml
You need to add the below code lines in this cardview.xml file.
Above lines will create the background for cardview.
It will use the gradient effect of blue colors to make attractive background for cardview.
We will use this file inside the XML layout file which we will create in the next step.
Step 3. Specific XML layout file
Now navigate to app->res->layout directory and make a new XML file.
Give this file a name like rv_child.xml and add the below code in it
We are using tag in the above file.
All the elements which are inside the tag will have the background like the cards.
In this, we have inserted one text view.
In the background of this text view, we are using an XML file which we have created in drawable folder. (cardview.xml)
All the child rows of the recyclerview will have the view created by this rv_child.xml file.
Step 4. Model For DataSet
Now make a new JAVA class and give it a name like Model.java
Write down the below code lines in Model.java file.
There is one string variable called “name” in the above class.
For this string variable, I have created two methods.
One is getter ( getName() ) and another is setter ( setName(String name) ) .
These methods will help us to maintain the data among all the child rows of the recycler view.
We will use this class and it’s objects in the Adapter class.
Step 5. Making Adapter
Time to create adapter class. Create a new JAVA class with the name Adapter.java
Adapter.java should contain the following coding lines.
Inside the constructor of the adapter class, compiler will get the arraylist with the objects of the Model.java class.
Name of this arraylist is imageModelArrayList
Now focus on the method onCreateViewHolder().
Inside this onCreateViewHolder() method, compiler will first inflate the rv_child.xml file.
Now read the method onBindViewHolder() . Inside this onBindViewHolder() method, compiler will set the text inside the text view.
To set the text inside text view, compiler will use imageModelArrayList and the methods of Model.java class.
Step 6. Final Writings of Example
Now there should be two main files when you created a new project.
Those two main files are : activity_main.xml and MainActivity.java
Inside activity_main.xml file, you have to write the below source lines
Above main XML layout file is very simple.
We need to add only RecyclerView inside this main layout file.
But we need to implement SwipeToRefresh element as the parent of the RecyclerView.
Now go to your MainActivity.java file and write the following words in it
More Details on Main File
First of all, see the below
First line is the object of RecyclerView class. Second is the arraylist with the object of the Model.java class.
Third line is the object of the Adapter class. Fourth one is integer variable.
Fifth is the object of the SwipeRefreshLayout class.
Now read the below lines
You can see that there are three string arrays are there.
All three includes the names of vehicles,players and countries respectively.
We will use all these three lists to set in the recycler view.
You can see the below lines in the onCreate() method.
Above line is populating the data inside the imageModelArrayList using populateList() method.
Below are the code lines for populateList() method.
This method will get one integer value inside it’s parameter (int number). This integer value is defining which names recyclerview should display.
If it’s value is 0 then compiler will display vehicle names, for the value 1, it will display player names and for value 2 it will display country names in the recyclerview.
Now read the following source code snippet
When the user pull or swipe down the recyclerview, compiler will run the above lines.
Here, compiler will check the value of the integer variable currentList.
If it’s value is 2 then it will set the value of currentList as -1
Then it will increment the value of currentList by 1.
After this, compiler will run the populateList() method to put data inside imageModelArrayList.
Then it will simply attach the adapter to the recycler view.
So it is all about our swipe to refresh recyclerview android example tutorial.
Swipe Down to Refresh ListView
If you want to create listview with swipe or pull down to refresh feature then read below
Источник
psydj1 / Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
RelativeLayout xmlns : android = » http://schemas.android.com/apk/res/android « |
xmlns : tools = » http://schemas.android.com/tools « |
android : id = » @+id/activity_main « |
android : layout_width = » match_parent « |
android : layout_height = » match_parent « |
android : paddingBottom = » @dimen/activity_vertical_margin « |
android : paddingTop = » @dimen/activity_vertical_margin « |
android : background = » #A8A8A8 « |
tools : context = » com.example.danieljarosz.animationtest.MainActivity » > |
android:paddingRight=»5dp» —> |
android:paddingLeft=»5dp» —> |
android:paddingLeft=»@dimen/activity_horizontal_margin» —> |
android:paddingRight=»@dimen/activity_horizontal_margin» —> |
android .support.v4.widget.SwipeRefreshLayout |
xmlns : android = » http://schemas.android.com/apk/res/android « |
android : id = » @+id/swipe_refresh_layout « |
android : layout_width = » match_parent « |
android : layout_height = » wrap_content « |
android : layout_alignParentTop = » true » > |
android .support.v7.widget.RecyclerView |
android : id = » @+id/recyclerList « |
android : layout_width = » match_parent « |
android : layout_height = » wrap_content » > |
android .support.v7.widget.RecyclerView> |
android .support.v4.widget.SwipeRefreshLayout> |
—> |
android:id=»@+id/listView» —> |
android:layout_width=»wrap_content» —> |
android:layout_height=»wrap_content»> —> |
RelativeLayout > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
apply plugin : ‘ com.android.application ‘ |
android < |
compileSdkVersion 25 |
buildToolsVersion » 25.0.0 « |
defaultConfig < |
applicationId » com.example.danieljarosz.animationtest « |
minSdkVersion 15 |
targetSdkVersion 25 |
versionCode 1 |
versionName » 1.0 « |
testInstrumentationRunner » android.support.test.runner.AndroidJUnitRunner « |
> |
buildTypes < |
release < |
minifyEnabled false |
proguardFiles getDefaultProguardFile( ‘ proguard-android.txt ‘ ), ‘ proguard-rules.pro ‘ |
> |
> |
> |
dependencies < |
compile fileTree( dir : ‘ libs ‘ , include : [ ‘ *.jar ‘ ]) |
androidTestCompile( ‘ com.android.support.test.espresso:espresso-core:2.2.2 ‘ , < |
exclude group : ‘ com.android.support ‘ , module : ‘ support-annotations ‘ |
>) |
compile ‘ com.mcxiaoke.volley:library-aar:1.0.0 ‘ |
compile ‘ com.android.support:recyclerview-v7:25.3.1 ‘ |
compile ‘ com.android.support:cardview-v7:25.3.1 ‘ |
compile ‘ com.android.support:appcompat-v7:25.3.1 ‘ |
compile ‘ com.android.support.constraint:constraint-layout:1.0.0-alpha9 ‘ |
compile ‘ com.android.support:design:25.3.1 ‘ |
testCompile ‘ junit:junit:4.12 ‘ |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
resources > |
color name = » colorPrimary » >#3F51B5 color > |
color name = » colorPrimaryDark » >#303F9F color > |
color name = » colorAccent » >#FF4081 color > |
string-array name = » movie_serial_bg » > |
item >#24c6d5 item > |
item >#57dd86 item > |
item >#ad7dcf item > |
item >#ff484d item > |
item >#fcba59 item > |
item >#24c6d5 item > |
string-array > |
color name = » white » >#FFFFFF color > |
color name = » grey » >#e5e5e5 color > |
resources > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
resources > |
Default screen margins, per the Android Design guidelines. —> |
dimen name = » activity_horizontal_margin » >16dp dimen > |
dimen name = » activity_vertical_margin » >16dp dimen > |
dimen name = » fab_margin » >16dp dimen > |
resources > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.example.danieljarosz.animationtest.model ; |
import android.content.Context ; |
import android.provider.BaseColumns ; |
public class Event implements BaseColumns < |
public String groupID; |
public int rank; // posi |
public String eventName; |
public String streetName; |
public String postCode; |
public String description; |
public boolean isOpen; |
public int peopleNeeded; |
public int houseNumber; |
public Long longitude; |
public Long latitude; |
public Event () < |
> |
public Event ( Context context ) < |
> |
public Event ( int position , String name ) < |
this . eventName = name; |
this . rank = position; |
> |
public String getGroupID () < |
return groupID; |
> |
public void setGroupID ( String groupID ) < |
this . groupID = groupID; |
> |
public String getEventName () < |
return eventName; |
> |
public void setEventName ( String eventName ) < |
this . eventName = eventName; |
> |
public String getStreetName () < |
return streetName; |
> |
public void setStreetName ( String streetName ) < |
this . streetName = streetName; |
> |
public String getPostCode () < |
return postCode; |
> |
public void setPostCode ( String postCode ) < |
this . postCode = postCode; |
> |
public String getDescription () < |
return description; |
> |
public void setDescription ( String description ) < |
this . description = description; |
> |
public boolean isOpen () < |
return isOpen; |
> |
public void setOpen ( boolean open ) < |
isOpen = open; |
> |
public int getPeopleNeeded () < |
return peopleNeeded; |
> |
public void setPeopleNeeded ( int peopleNeeded ) < |
this . peopleNeeded = peopleNeeded; |
> |
public int getHouseNumber () < |
return houseNumber; |
> |
public void setHouseNumber ( int houseNumber ) < |
this . houseNumber = houseNumber; |
> |
public Long getLongitude () < |
return longitude; |
> |
public void setLongitude ( Long longitude ) < |
this . longitude = longitude; |
> |
public Long getLatitude () < |
return latitude; |
> |
public void setLatitude ( Long latitude ) < |
this . latitude = latitude; |
> |
public int getRank () < |
return rank; |
> |
public void setRank ( int rank ) < |
this . rank = rank; |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.example.danieljarosz.animationtest ; |
import android.os.Bundle ; |
import android.support.design.widget.FloatingActionButton ; |
import android.support.design.widget.Snackbar ; |
import android.support.v4.widget.SwipeRefreshLayout ; |
import android.support.v7.app.AppCompatActivity ; |
import android.support.v7.widget.LinearLayoutManager ; |
import android.support.v7.widget.RecyclerView ; |
import android.support.v7.widget.Toolbar ; |
import android.util.Log ; |
import android.view.View ; |
import android.view.Menu ; |
import android.view.MenuItem ; |
import android.widget.Toast ; |
import com.android.volley.Response ; |
import com.android.volley.VolleyError ; |
import com.android.volley.toolbox.JsonArrayRequest ; |
import com.example.danieljarosz.animationtest.model.Event ; |
import org.json.JSONArray ; |
import org.json.JSONException ; |
import org.json.JSONObject ; |
import java.util.ArrayList ; |
import java.util.List ; |
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout . OnRefreshListener < |
private String TAG = MainActivity . class . getSimpleName(); |
private String URL_TOP_250 = » http://api.androidhive.info/json/imdb_top_250.php?offset= » ; |
private SwipeRefreshLayout swipeRefreshLayout; |
private RecyclerListAdapter recyclerListAdapter; |
private RecyclerView recyclerView; |
private List Event > eventList; |
// initially offset will be 0, later will be updated while parsing the JSON |
private int offSet = 0 ; |
@Override |
protected void onCreate ( Bundle savedInstanceState ) < |
super . onCreate(savedInstanceState); |
setContentView( R . layout . activity_event_list); |
eventList = new ArrayList<> (); |
Toolbar toolbar = ( Toolbar ) findViewById( R . id . toolbar); |
setSupportActionBar(toolbar); |
recyclerView = ( RecyclerView )findViewById( R . id . recyclerList); |
LinearLayoutManager llm = new LinearLayoutManager ( this ); |
llm . setOrientation( LinearLayoutManager . VERTICAL ); |
recyclerView . setLayoutManager(llm); |
// eventDetails = (Button)findViewById(R.id.eventDetails); |
swipeRefreshLayout = ( SwipeRefreshLayout ) findViewById( R . id . swipe_refresh_layout); |
recyclerListAdapter = new RecyclerListAdapter ( this , eventList); |
recyclerView . setAdapter(recyclerListAdapter); |
swipeRefreshLayout . setOnRefreshListener( this ); |
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
// fab.setOnClickListener(new View.OnClickListener() < |
// @Override |
// public void onClick(View view) < |
// Snackbar.make(view, «Replace with your own action», Snackbar.LENGTH_LONG).setAction(«Action», null).show(); |
// > |
// >); |
> |
@Override |
public void onRefresh () < |
fetchEvent(); |
> |
@Override |
public boolean onCreateOptionsMenu ( Menu menu ) < |
// Inflate the menu; this adds items to the action bar if it is present. |
getMenuInflater() . inflate( R . menu . menu_main, menu); |
return true ; |
> |
/** |
* Fetching movies json by making http call |
*/ |
private void fetchEvent () < |
// showing refresh animation before making http call |
swipeRefreshLayout . setRefreshing( true ); |
// appending offset to url |
// offset says how many rows it should return. |
String url = URL_TOP_250 + offSet; |
// Volley’s json array request object |
JsonArrayRequest req = new JsonArrayRequest (url, |
new Response . Listener JSONArray > () < |
@Override |
public void onResponse ( JSONArray response ) < |
Log . d( TAG , response . toString()); |
if (response . length() > 0 ) < |
// looping through json and adding to movies list |
for ( int i = 0 ; i response . length(); i ++ ) < |
try < |
JSONObject eventObject = response . getJSONObject(i); |
int rank = eventObject . getInt( » rank » ); |
String title = eventObject . getString( » title » ); |
Event event = new Event (rank, title); |
eventList . add( 0 , event); |
Log . d( » size of list » , » » + eventList . size()); |
// updating offset value to highest value |
if (rank >= offSet) |
offSet = rank; |
> catch ( JSONException e) < |
Log . e( TAG , » JSON Parsing error: » + e . getMessage()); |
> |
> |
recyclerListAdapter . notifyDataSetChanged(); |
> |
// stopping swipe refresh |
swipeRefreshLayout . setRefreshing( false ); |
> |
>, new Response . ErrorListener () < |
@Override |
public void onErrorResponse ( VolleyError error ) < |
Log . e( TAG , » Server Error: » + error . getMessage()); |
Toast . makeText(getApplicationContext(), error . getMessage(), Toast . LENGTH_LONG ) . show(); |
// stopping swipe refresh |
swipeRefreshLayout . setRefreshing( false ); |
> |
>); |
// Adding request to request queue |
MyApplication . getInstance() . addToRequestQueue(req); |
> |
@Override |
public boolean onOptionsItemSelected ( MenuItem item ) < |
// Handle action bar item clicks here. The action bar will |
// automatically handle clicks on the Home/Up button, so long |
// as you specify a parent activity in AndroidManifest.xml. |
int id = item . getItemId(); |
// noinspection SimplifiableIfStatement |
if (id == R . id . action_settings) < |
return true ; |
> |
return super . onOptionsItemSelected(item); |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.example.danieljarosz.animationtest ; |
import android.app.Application ; |
import android.text.TextUtils ; |
import com.android.volley.Request ; |
import com.android.volley.RequestQueue ; |
import com.android.volley.toolbox.Volley ; |
/** |
* |
* Singleton Application object |
* Place where an app starts |
* Prepares Volley Object that is global for a whole app |
* Volley is used to make easy network calls and get json list |
* |
*/ |
public class MyApplication extends Application < |
public static final String TAG = MyApplication . class . getSimpleName(); |
private RequestQueue mRequestQueue; |
private static MyApplication mInstance; |
@Override |
public void onCreate () < |
super . onCreate(); |
mInstance = this ; |
> |
public static synchronized MyApplication getInstance () < |
return mInstance; |
> |
public RequestQueue getRequestQueue () < |
if (mRequestQueue == null ) < |
mRequestQueue = Volley . newRequestQueue(getApplicationContext()); |
> |
return mRequestQueue; |
> |
public T > void addToRequestQueue ( Request T > req , String tag ) < |
req . setTag( TextUtils . isEmpty(tag) ? TAG : tag); |
getRequestQueue() . add(req); |
> |
public T > void addToRequestQueue ( Request T > req ) < |
req . setTag( TAG ); |
getRequestQueue() . add(req); |
> |
public void cancelPendingRequests ( Object tag ) < |
if (mRequestQueue != null ) < |
mRequestQueue . cancelAll(tag); |
> |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.example.danieljarosz.animationtest; |
import android.content.Context; |
import android.support.v7.widget.RecyclerView; |
import android.util.Log; |
import android.view.LayoutInflater; |
import android.view.View; |
import android.view.ViewGroup; |
import android.widget.ImageView; |
import android.widget.TextView; |
import android.support.v7.widget.CardView; |
import com.example.danieljarosz.animationtest.model.Event; |
import java.util.List; |
/** |
* Calm |
* |
*/ |
public class RecyclerListAdapter extends RecyclerView.Adapter < |
public Context context; |
private LayoutInflater inflater; |
private List eventList; |
private String[] bgColors; |
//Custom listener here |
//Define listener member variable |
private static OnItemClickListener listener; |
// Define the listener interface |
public interface OnItemClickListener < |
void onItemClick(View itemView, int position); |
> |
// Define the method that allows the parent activity or fragment to define the listener |
public void setOnItemClickListener(OnItemClickListener listener) < |
this.listener = listener; |
> |
//constructor, data is retrieved here |
public RecyclerListAdapter(Context context, List eventList) < |
this.context = context; |
this.eventList = eventList; |
bgColors = context.getResources().getStringArray(R.array.movie_serial_bg); |
Log.d(«Adapter», «size is » + eventList.size()); |
> |
static class ViewHolder extends RecyclerView.ViewHolder < |
//each data item is just a string in this case |
//need to createe new data for each thing that comes from data |
TextView textView1; |
TextView textView2; |
//different constructors for diffrent data |
ViewHolder(View view) < |
super(view); |
textView1 = (TextView)view.findViewById(R.id.textView1); |
textView2 = (TextView)view.findViewById(R.id.textView2); |
//using the custom listener I created on the top |
view.setOnClickListener(new View.OnClickListener() < |
@Override |
public void onClick(View v) < |
// Triggers click upwards to the adapter on click |
if (listener != null) |
listener.onItemClick(itemView, getLayoutPosition()); |
> |
>); |
> |
> |
// Create new views (invoked by the layout manager) |
@Override |
public RecyclerListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) < |
Log.d(«Adapter», «size is » + eventList.size()); |
//find Id’s from the inflated row |
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
View v = inflater.inflate(R.layout.row_list, parent, false); |
//call different contructors here by hand, no cant because can return only one View holder |
//so the view needs to be passed |
ViewHolder vh = new ViewHolder(v); |
return vh; |
> |
// Replace the contents of a view (invoked by the layout manager) |
@Override |
public void onBindViewHolder(RecyclerListAdapter.ViewHolder holder, int position) < |
Log.d(«Adapter», «size is » + eventList.size()); |
//for now set IDs |
//holder.roomID.setText(eventList.get(position).getEventName()); |
holder.textView1.setText(Integer.toString(eventList.get(position).getRank()) + » mil»); |
holder.textView2.setText(eventList.get(position).getEventName()); |
> |
@Override |
public int getItemCount() < |
Log.d(«Adapter», «size is » + eventList.size()); |
return eventList.size(); |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
LinearLayout xmlns : android = » http://schemas.android.com/apk/res/android « |
android : orientation = » vertical » android : layout_width = » match_parent « |
android : layout_height = » match_parent » > |
android .support.v7.widget.CardView |
android : elevation = » 2dp « |
android : layout_width = » match_parent « |
android : layout_height = » 150dp » > |
LinearLayout |
android : orientation = » vertical « |
android : layout_width = » match_parent « |
android : layout_height = » wrap_content » > |
TextView |
android : text = » Blah1 « |
android : id = » @+id/textView1 « |
android : layout_width = » wrap_content « |
android : layout_height = » wrap_content »/> |
TextView |
android : text = » Blah2 « |
android : id = » @+id/textView2 « |
android : layout_width = » wrap_content « |
android : layout_height = » wrap_content »/> |
LinearLayout > |
android .support.v7.widget.CardView> |
LinearLayout > |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник