I have a RecyclerView that shows a list of items.
If there is no item to show, The recyclerview shows one item with a specific view (to tell the user there is no item instead of a white screen).
Within HistoryFragment:
private void initRecyclerView(Boolean isNoResult){
HistoryRecyclerViewAdapter adapter = new HistoryRecyclerViewAdapter(mContext, mRecords, **isNoResult**);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setAdapter(adapter);
}
Within HistoryRecyclerViewAdapter:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if(**isEmpty**) {
**view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory_empty, parent, false);**
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory, parent, false);
}
ViewHolder holder = new ViewHolder(view);
return holder;
}
So, it's possible to remove items one by one, if we click on them.
I would like to set isEmpty to true and refresh the RecyclerView when the dataSet is null.
I already know where to call that method but I really don't know how I can do that? (i.e. refresh the RecyclerView with isEmpty = true so I can display the cell that explain to the user that there is no record anymore).
Don't inflate different view-holders, because when the adapter has no items, not a single one of them will ever be inflated. Instead one can wrap the RecyclerView together with a "no data" Fragment into a ViewFlipper, which then can be switched to the Fragment, when the RecyclerView adapter has no items.
Best practice is to use an empty view outside of RecyclerView but in case you like to do what you want:
1.in onCreateViewHolder only inflate one layout which has empty and item views
on item delete check if your array is empty and then add a null item
then in onBindViewHolder check the model if model is Null visible empty view otherwise show item view
summary:
onBind:
model is null : empty View visible
model is not null: item View visible
use interface to refresh the RecyclerView after remove something like this
public interface RefreshRecyclerView {
public void Refresh();
}
then in activity or fragment implement the interface
Fragment implements RefreshRecyclerView
you will have override method like this
#Override
public void Refresh() {
// set adapter again here
}
then pass the interface to adapter like this
RefreshRecyclerView refresh = (RefreshRecyclerView) this;
yourRecycler.setadapter(refresh);
fially when user clicked on adapter use this
refresh. Refresh();
Related
I have been reading articles and questions about recycling a listItem rather than Inflating a new one which is costly. In particular I read this question and I understood the answers, but my question is: When we scroll down a list some listItems from the top disappear and they should be recycled and be shown again in the bottom of the List.
How does android determine the position of them?
When we use the viewHolder = (ViewHolder)convertView.getTag; the viewHolder obviously holds the row who wants to be recycled and when we set View Elements by using viewHolder.txtName.setText(dataModel.getName()); I understand that we update the Elements in that particular viewHolder so how does it go to the bottom of the List?
I found this article very useful but there is not enough explanation and comments to make recycling clear for me.
Android ListView with Custom Adapter
How does android determine the position of them?
The ListView adapter (ArrayAdapter for example) has ArrayAdapter.getView() and the equivalent adpater for RecyclerViews is RecyclerView.Adapter.onBindViewHolder(). They both have a position parameter in their method signatures:
// ListView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
}
// RecyclerView
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
...
}
This is the list row position that has been scrolled into view, and needs to be bound to fill in the data.
Furthermore, RecyclerView.ViewHolder objects have the method getAdapterPosition(), so that you can find ViewHolder's list row position outside of the onCreateViewHolder() and onBindViewHolder() methods, for example in a click event handler.
See also:
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance
http://www.vogella.com/tutorials/AndroidRecyclerView/article.html
I have custom listview and array adapter with ViewHolder. When click listview item, it expands new layout below. Problem is: unfortunately it is opened for every +9th item in listview.. For example: if item 0 is clicked; 0,9,18th elements opens their expand layouts. Any idea without looking code ?
I have no idea, but this sounds familair with an issue I had. I had a listview with TextView objects and multiple were selected and got typed in the same value.
I had a very, very dirty fix for that, in my custom adapter I'd always make a new View, no matter what:
#Override
public View getView (final int position, View convertView, final ViewGroup parent) {
//if (convertView == null) {
// Inflate the view from the converter
final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(converter.getLayout(), parent, false);
//}
// Populate the view from the converter
converter.populateInflatedView(convertView, getItem(position));
return convertView;
}
Source can be found here if you want to know what the converter is about.
On a side note, I have created a sort of interface type of thing for a TreeView, however this is in Activity form which uses a ScrollView. So this is not really a ListView type of deal, but might help you. The Tree part can be found on the Tree-link, with an implementation in the subject package.
In the case of a ListView if we want to make a particular item selected we use the setSelection method. How do we do this in case of RecyclerView?
Use RecyclerView LayoutManager to scroll item at position
recyclerView.getLayoutManager().scrollToPosition(position)
Check
scrollToPositionWithOffset(int position, int offset)
scrollToPositionWithOffset(5,0);
from LinearLayoutManager
Scroll to the specified adapter position with the given offset from resolved layout start.
pass offset as 0 if you want selection at top
This worked for me
Check
recyclerView.scrollToPosition(cursor.getcount() - 1);
ListView.setSelected() does (at least) two things:
It sets the item in the list to be selected (while removing the selection from another item - if such exists)
It scrolls the list so that the item will be visible on the screen.
To achieve 2. either call scrollToPosition() method of RecyclerView (as indicated by Loser), or call one of the scrolling methods of the LayoutManager object depending on your desired scrolling behavior.
For example,
recyclerView.getLayoutManager().smoothScrollToPosition()
You may want to scroll the minimum so that the selected item shows on the screen.
If so and you are using LinearLayoutManager or GridLayoutManager, you can build such scroll logic based on
findFirstCompletelyVisibleItemPosition() and findLastCompletelyVisibleItemPosition() defined in these classes.
Achieving 1. is more tricky. You may want to use the following recipe:
First define a background color in colors.xml, item_state_selected_color, to be used when an item is selected.
In your onCreateViewHolder() implementation create a StateListDrawalbe and set it as the background of the view.
Say something like this:
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// inflate the item view
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(itemResourceId,viewGroup, false);
// create color drawable by a resorce id
ColorDrawable colorDrawableSelected =
new ColorDrawable(resources.getColor(R.color.item_state_selected_color));
// create StateListDrawable object and define its states
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, colorDrawableSelected);
stateListDrawable.addState(StateSet.WILD_CARD, null);
// set the StateListDrawable as background of the item view
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
itemView.setBackgroundDrawable(stateListDrawable);
}
else {
itemView.itemView.setBackground(stateListDrawable);
}
// create view holder object providing it with the item view
return new YourViewHolder(itemView);
}
In YourAdapter object (or elsewhere) save a variable, mCurrentSelectedPosition (probably initialized to -1) that holds the current selected position.
Still in the adapter, define handler for clicks on recycler view items, depending on your click logic. For example:
void onItemClick(int position) {
YourViewHolder yourViewHolder;
int oldSelectedPosition = mCurrentSelectedPosition;
if (position != mCurrentSelectedPosition) {
mCurrentSelectedPosition = position;
if (oldSelectedPosition != -1) {
yourViewHolder = findViewHolderForPosition(oldSelectedPosition);
yourViewHolder.itemView.setSelected(false);
}
yourViewHolder = findViewHolderForPosition(mCurrentSelectedPosition);
yourViewHolder.itemView.setSelected(true);
}
}
Next, in the constructor of YourViewHolder set listener to clicks on the item:
public YourViewHolder(View itemView,YourAdapter adapter) {
mAdapter = adapter;
// ... other code here ...
itemView.setOnClickListener(this);
}
Still in YourViewHolder override the onClick() method to delegate handling to the adapter. like this
#Override
public void onClick(View v) {
mAdapter.onItemClick(getPosition());
}
Now there is just last problem to solve - we need to keep track of the selected item with respect to recycling.
#Override
public void onBindViewHolder(YourViewHolder yourViewHolder, int position) {
if (position == mCurrentSelectedPosition) {
yourViewHolder.itemView.setSelected(true);
}
else {
yourViewHolder.itemView.setSelected(false);
}
// ... other code here ...
}
Good luck!
I got a ListView with a custom Adapter.
In the Adapter a layout is inflated from xml.
All works fine, and I can see the items, until the Screen Orientation is changed.
I know that the Activity is recreated (or resumed) then, and the ListView is recreated too, as well as the Adapter.
But there are no items in the ListView now. The Adapter isn't empty, I use toasts to display the count of items in the Adapter.
I guess there is an inflating problem, because if I use the same Adapter (or an adapter with the same data) to a new ListView nothing is shown as well.
But the most crazy thing I don't understand is, that if I let my getView() method return a simple TextView, all works fine, even after orientation change.
I tried several things, like don't recycle a View so that it is inflated every time, or save the View to the matching Item (from getItem(position) from the Adapter).
I'm grateful for all hints :)
EDIT: so I was asked for some code.
Here is the getView() of my Adaptar
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
final Event event = getItem(position);
if (view == null) {
view = inflater.inflate(R.layout.new_event_item_layout, parent,
false);
view.setTag(R.id.eventDate, view.findViewById(R.id.eventDate));
view.setTag(R.id.eventTime, view.findViewById(R.id.eventTime));
view.setTag(R.id.eventName, view.findViewById(R.id.eventName));
view.setTag(R.id.eventBemerkungen,
view.findViewById(R.id.eventBemerkungen));
view.setTag(R.id.eventIcon, view.findViewById(R.id.eventIcon));
}
((TextView) view.getTag(R.id.eventDate)).setText(event.getDate());
((TextView) view.getTag(R.id.eventTime)).setText(event.getName());
((TextView) view.getTag(R.id.eventName)).setText(event.getTime());
((TextView) view.getTag(R.id.eventBemerkungen)).setText(event
.getDescription());
SquaredImageView icon = (SquaredImageView) view.getTag(R.id.eventIcon);
Picasso.with(context).load(event.getUri())
.placeholder(R.drawable.ic_reload).into(icon);
view.setBackgroundColor(event.getBackgroundColor());
view.setVisibility(View.VISIBLE);
return view;
//return getDummyTextView();
}
public TextView getDummyTextView()
{
TextView tv=new TextView(context);
tv.setText("YOLO BIATCHSES");
tv.setTextColor(Color.WHITE);
return tv;
}
I have had the same exact problem although it wasn't because of orientation change and I found the solution by just setting listView.setAdapter(adapter) again after the recreation of the activity or whatever case you have. I suspect the listview is basically losing the pointer to the adapter.
i currently create an app that needs an custom listview. Everything with my listview is fine, but now i neet to know, how to set an onClickListener to an view, defined in my list_row.xml. i just want the onclicklistener on the whole item, and on this one inner view. I attach a picture to demonstrate my problem, because it is so hard to describe >.<
Picture (dropbox): https://www.dropbox.com/s/72xdxuwz47vl7s5/problem.png
I need a function that is called when clicking into the view [my Problem] indicates. its an ImageView filled with an image.
Here's something I've done before that seems pretty similar to what you want to accomplish.
First, you declare an onItemClickListener for your ListView. This will handle standard list item taps (that is, taps inside a list item but outside the inner view region that you're concerned about). You can do this in a variety of places in your code, but onCreate() is a common one.
Example:
mListView.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
// Handle standard list item tap
// ...
}
} );
Then, you can just declare whatever onClickListeners you need for your inner view(s) inside your adapter's getView() method to handle click/tap events on your inner view.
Example:
#Override
public View getView( int position, View convertView, ViewGroup parent ) {
LinearLayout itemView;
// Inflate layout XML, etc.
// ...
// Find subviews in layout
ImageView innerView = (ImageView) itemView.findViewById( R.id.myInnerViewId );
// ...
// Set up onClickListener for inner view
innerView.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
// Handle inner view tap
// ...
}
} );
// ...
}
To set an OnClickListener in each row simply extend your current Adapter and override the getView() method. In there you can define specific listeners as you normally would.
This is discussed in great detail in this Google Talk by Romain Guy.