android leanback browseSupportFragment return wrong selectedPosition? - android

I am using a browseSupportFragment element in my android tv app having headers enabled and a single row for every header
the problem is when i select the first item of every row by scrolling down, or when i select a header of the header list, the function getSelectedPosition return 0 always
it return the right index of row when i select the second item in the row
i am pretty sure that this is a bug !!
below the code of onItemSelected
#Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object
item, RowPresenter.ViewHolder rowViewHolder, Row row) {
int pos = getSelectedPosition();//this return 0 if i scroll down between headers
}

You will need to override it because for some reason it returns 0 by default.
/**
* Returns the selected position.
*/
public int getSelectedPosition() {
return 0;
}
You might need to override the setter as well because it is empty too.
/**
* Selects a Row.
*/
public void setSelectedPosition(int rowPosition, boolean smooth) {
}
It seems leanback support was left a bit undercooked.

the problem was need to add this peace of code
#Override
public int getSelectedPosition() {
return super.getSelectedPosition();
}

Related

Added element doesn't show in RecyclerView after deleting another with swipe

Edit: if anyone is having the same problem in the future it was a pretty easy fix. I used clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) to handle when the entire drag was finished and had forgotten to call it's super-method. That's why it didn't update properly.
Original question:
Here's the entire code: https://github.com/vustav/Ppaaiinntt/tree/master/app/src/main/java/com/decent/rvtest
Everything works fine except when I add an element right after deleting another. It does exist though. If I add another there's a properly sized space between the old list and the new element. I use a string where they add their names before a print and it shows there, and if I drag to change positions of the elements it shows up properly.
My reputation doesn't allow me to post images so an imgur-album will have to do:
http://imgur.com/a/bmb17
On the first image there's three elements and the string is printed at the bottom.
The second image is after the swipe. Notice the string is updated.
The third is after adding another "111". The string is correct but it doesn't show up in the view.
The fourth is after adding another. The string is still correct and the new element shows up in the view.
The last image is after dragging to change the position of the last two elements. Now everything is fine again.
These are the relevant methods (I think):
protected void add(PictureElement pe){
chain.add(pe);
notifyItemInserted(chain.size()-1);
}
public void remove(int position) {
chain.remove(position);
notifyItemRemoved(position);
}
protected void swap(int from, int to){
chain.swap(from, to);
notifyItemMoved(from, to);
}
Edit: onBindViewHolder, getItemCount and the ViewHolder:
#Override
public int getItemCount() {
return chain.size();
}
#Override
public void onBindViewHolder(PEViewHolder PEViewHolder, int i) {
PictureElement pe = chain.get(i);
PEViewHolder.name.setText(pe.getName());
}
protected static class PEViewHolder extends RecyclerView.ViewHolder {
protected TextView name;
public PEViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.txtName);
}
}
Interesting quote on the different notify methods you are using
public final void notifyItemRemoved (int position) Notify any registered observers that the item previously located at position has
been removed from the data set. The items previously located at and
after position may now be found at oldPosition - 1. This is a
structural change event. Representations of other existing items in
the data set are still considered up to date and will not be rebound,
though their positions may be altered. Parameters position : Position
of the item that has now been removed.
public final void notifyItemRangeChanged (int positionStart, int itemCount) Notify any registered observers that the itemCount items
starting at position positionStart have changed. Equivalent to calling
notifyItemRangeChanged(position, itemCount, null);. This is an item
change event, not a structural change event. It indicates that any
reflection of the data in the given position range is out of date and
should be updated. The items in the given range retain the same
identity.
Could you try and comment this block of code and check if it works
//Called by the ItemTouchHelper when the user interaction with an element is over and it also
// completed its animation
/*
#Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//update from where the action took place
mPEAdapter.updateChain(viewHolder.getLayoutPosition());
//clearView is called after onMove so any drags or swipes are complete
dragging = false;
mPEAdapter.setSwipe(false);
}
*/

Add section to recycle View in run time with dynamic array

i am trying to add section/header in recycle View
as we have to handle section stuff in
#Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position == 0 ? section: item;
}
but i am having arraylist which have n number of data . so say 1st section may have 10 row where as 2nd may have 2 row .so how to define section in this case .where i come to know this is the end of 1st section ? and put 2nd one
Iterate over each model and remember the previous section name of each model item. When ever you find a new section name, just map the index.
In your adapter return items count that is fewer than data set.
#Override
public int getItemCount(){
return mData.size() + mSections.size();
}
Always use position + 1, because you are always one position ahead.
Whenever you reach for position that belongs to the mapped indexes, return section type and bind it accordingly:
#Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return isPositionSectionIndex(position) ? VIEW_TYPE_SECTION: VIEW_TYPE_ITEM;
}

How to have my ListView catch onLongClick() (not onItemLongClick)

I have a ListView that is refreshed at a high rate (3 times per second).
I need to catch a long press on such a ListView (as well as on the parent layout); the ListView has it's height set to wrap_content.
I can catch the long click on the parent layout, but I wish the long click on any item to be handled by the parent layout.
OnItemLongClick does not work well due to the high refresh rate, I have tried the onLongClickListener to the ListView but the the long click is not fired.
The rows are set as non-clickable, not-focusable as well as all the items contained in the row.
The question is how to handle a long click anywhere in the ListView if the position / item does not matter?
I faced similar issue (with clicks on ImageButtons) when I was updating progress of my downloads in a ListView.
The solution was to update the individual rows instead of calling adapter.notifyDataSetChanged() (as what I understand is it interferes with your click/touch listeners). So to do this I problem was to find the row accurately. I need two things for this:
mapping of URL strings to my Transfer objects (mPathToTransfers),
list of URLs (mPathKeys).
Here's some code from my adapter:
protected LinkedHashMap<String, BaseTransfer> mPathToTransfers;
protected List<String> mPathKeys;
#Override
public int getCount() {
mPathKeys = new ArrayList<String>(mPathToTransfers.keySet());
return mPathToTransfers.size();
}
#Override
public BaseTransfer getItem(int position) {
return mPathToTransfers.get(mPathKeys.get(position));
}
/**
* Update transfer (download) progress in ListView's specific row
*/
public void setItemTransferredSize(String path, long transferredSize, ListView lv) {
if (mPathToTransfers.containsKey(path)) {
BaseTransfer dItem = (BaseTransfer) mPathToTransfers.get(path);
dItem.setTransferredSize(transferredSize);
Holder holder = getViewHolderForTransfer(path, lv);
if (holder != null)
setTransferProgressUpdate(holder, dItem);
}
}
/**
* Use to get the View Holder of the row of the transfer
*
* #param path
* #param lv
* #return {#linkplain Holder} for row, if it is visible, null otherwise
*/
private Holder getViewHolderForTransfer(String path, ListView lv) {
int rowIndex = mPathKeys.indexOf(path);
if (lv.getFirstVisiblePosition() <= rowIndex && lv.getLastVisiblePosition() >= rowIndex)
return (Holder) lv.getChildAt(rowIndex - lv.getFirstVisiblePosition()).getTag();
else
return null;
}
If you have ambiguities, ask in comments below (or in your question's comments). :)

listview getting new instance when swiping the listview

I have one listview in my application,it contains two rows one for task and another one for alarm,date,severity. Initially first row of the list item only displayed for all list item and the second one is invisible. When i click the list item the second row displayed for that item as well as click another list item at that time the above list item closed that second row. Its working fine for me...My problem is if i open one list item and then swipe the listview at then i click the another list item at that time the above one cannot be closed because the above list item instance will be chnaged.please any one help me how to solve this problem...
int lastselectedPosition == -1
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
TextView textviewDate=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
selectedtaskDate=textviewDate.getText().toString().trim();
if (lastselectedPosition == -1) {
Log.i(TAG,"Loopif:"+lastselectedPosition);
TextView twTaskTime = (TextView) view
.findViewById(R.id.taskTimeidDaytoDay);
TextView twSeverity = (TextView) view
.findViewById(R.id.severityidDaytoDay);
TextView twAlarm = (TextView) view
.findViewById(R.id.alarmidDaytoDay);
twAlarm.setVisibility(view.VISIBLE);
twSeverity.setVisibility(view.VISIBLE);
twTaskTime.setVisibility(view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
// Log.i(TAG,"LoopElse:"+lastselectedPosition);
lastSelectedItem.findViewById(R.id.taskTimeidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.severityidDaytoDay)
.setVisibility(lastSelectedItem.GONE);
lastSelectedItem.findViewById(R.id.alarmidDaytoDay).setVisibility(
lastSelectedItem.GONE);
if (lastselectedPosition != position) {
view.findViewById(R.id.taskTimeidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.severityidDaytoDay).setVisibility(
view.VISIBLE);
view.findViewById(R.id.alarmidDaytoDay).setVisibility(
view.VISIBLE);
lastselectedPosition = position;
lastSelectedItem = arg0.getChildAt(position);
} else {
lastselectedPosition = -1;
lastSelectedItem = null;
}
}
GetView():
#Override
public View getView(int position, View view, ViewGroup parent) {
Log.i("XXXX", "Inside getView");
final DaytoDayTaskGetterSetter objDaytoDaygetset=getItem(position);
TextView textviewTask;
TextView txtviewAlarm ,txtviewTaskTime ,txtviewSeverity;
Log.i(TAG,"InsideGetView:"+position);
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(view==null)
{
view=inflater.inflate(R.layout.daytodaylistlayout,null);
}
Log.i("XXXX", "before first test");
textviewTask=(TextView)view.findViewById(R.id.tasknameidDaytoDay);
txtviewAlarm=(TextView)view.findViewById(R.id.alarmidDaytoDay);
txtviewSeverity=(TextView)view.findViewById(R.id.severityidDaytoDay);
txtviewTaskTime=(TextView)view.findViewById(R.id.taskTimeidDaytoDay);
return view;
}
In first i click the "gdfgdtet" list item it show another row and then i click the second list item "dfgsdgsd" at that time the above list item "gdfgdtet" closed the second row.This is a normal output.Suppose if i open the "gdfgdtet" list item and then swipe the listview at that time both of "gdfgdtet" "dfgsdgsd" will be opened and crashed...because the above one list item reference changed when i am swiping please how to solve this problem...
I'll try to provide you a good answer that explains why you are having this problems, but the general idea is that you have to see this video - http://www.youtube.com/watch?v=wDBM6wVEO70
please take my words kindly - you don't seems to understand what ListView + BaseAdapter recycling mechanism is all about, and I strongly recommend you see the full video I linked you to, and read more about that.
in general, the specific problem in your code is that you are holding reference to listview item (lastSelectedItem), then trying to use it latter assuming it's still representing same list item. that's wrong. in that stage (after scrolling) the view already been recycled to represent another item in the list (based on the adapter implementation).
listView's number of childs is not the size of adapter.getCount()!!!!!!!!
listViews's number of childs = number of visible list items on screen + 1 + headers + footers
let's say you have the 5 first items visible on screen, then you are scrolling down. when you see the 7 item you actually see the same view instance that used to show the first list item and been recycled.
getView will call in this stage with convertView != null and position in the adapter to let you reuse the item by putting new values such different text/image to the same instance
this mechanism provides ability to display list of "infinite" number of items in the list, and holding in memory only a few number of views. imagine that you have list of 5000 items in the list, and each one of them have different view instance - you would get outOfMemory exception in a sec!
complete explanation about that would be hard to write in stackoverflow answer's context.
it just too long trying to explain one of the most important and complex UI components in android, but this links would be a good start:
http://www.youtube.com/watch?v=wDBM6wVEO70
How ListView's recycling mechanism works
http://mobile.cs.fsu.edu/the-nuance-of-android-listview-recycling-for-n00bs/
if you are interstead in "quick" fix for your specific problem, the solution would be:
hold in the data structure represents your list item additional field indicating if it in "close" or "open state. when item been clicked - change the data accordinly and call notifyDatasetChanged(). inside the getView() check if item is open or close and populate it accordinly
by the way - it's not only "quick fix" solution, but also the right thing to do anyway
You should pay attention to Tal Kanel's answer and consider this one to be an extension to it. His advice will help you in the long run.
Add a boolean field to DaytoDayTaskGetterSetter class:
public class DaytoDayTaskGetterSetter {
....
....
boolean open;
public DaytoDayTaskGetterSetter (.., .., boolean o) {
....
....
open = o;
}
....
....
public boolean shouldOpen() {
return open;
}
public void setOpen(boolean o) {
open = o;
}
}
In your getView(), check if the object has its open value set:
DaytoDayTaskGetterSetter obj = (DaytoDayTaskGetterSetter) getItem(position);
if (obj.shouldOpen()) {
// Set visibility to true for the items
} else {
// Set visibility to false for the items
}
On list item click, traverse the list and set open for all list items to false. Use the position to retrieve DaytoDayTaskGetterSetter and set its open to true:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
clickedItem.setOpen(true);
yourAdapter.notifyDataSetChanged();
}
Edit 1:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
DaytoDayTaskGetterSetter clickedItem = (DaytoDayTaskGetterSetter)
yourAdapter.getItem(position);
if (clickedItem.shouldOpen()) {
clickedItem.setOpen(false);
} else {
for (DaytoDayTaskGetterSetter obj : listContainingObjects) {
obj.setOpen(false);
}
clickedItem.setOpen(true);
}
yourAdapter.notifyDataSetChanged();
}

Set getChildView() to return only selected rows

I have a BaseExpandableListAdapter class and getChildView() has to return only a couple rows. But, of course, returning null will crash the app and setting the visibility to GONE will create a blank space in the list.
So, the user has a checkbox in each row, and when one is checked the next time he opens the list it won't appear (like being deleted from the ArrayAdapter, but I can't do that).
Any good option?
EDIT
#Override
public int getChildrenCount(int groupPosition) {
int childrenCount = categories.get(groupPosition).size();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
/** If the user wants to remove the completed items */
if (settings.getBoolean("deleteCompletedItemsPref", false) == true) {
for (int childPosition=0; childPosition<childrenCount; childPosition++) {
if (categories.get(groupPosition).getItem(childPosition).isChecked()) {
parent.removeViewAt(childPosition);
}
}
}
return childrenCount;
}
Here I run the group's children and try to remove the unwanted views, although I can't remove views here because is an AdapterView.
your BaseExpandableListAdapter must have implemented a getChildrenCount() method. you can make it return the size of your category list (or maybe a copy of the list).
public int getChildrenCount(int groupPosition) {
return categories.get(groupPosition).size();
}
Then simply remove the child position if it's checked (preferably in a click listener). Of course you'd also need to adjust your data set accordingly. perhaps make it an arraylist if it isn't then remove the same position in it.

Categories

Resources