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.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why the position is always 0 when generateView is called?
I want to make a listview and i hold a reference to a list with all the objects in the swipeadapter.
I have a trash imageview in the bottom view (just like in the tutorial) which when clicked it deletes that item, but i can't notify the adapter with the position since it's always 0. So the wrong item gets deleted.
I did some more testing and i found out that if i pass a list that aready contains some items everything seem to work fine. The position is correct and the right item is deleted.
If the list is empty and start adding later, the position will always be 0 on the new items.
publicclassMyAdapterextendsSwipeAdapter
{
privateContextcontext;
//holding the reference, i have an add button on the main activity that adds more strings in the listprivateList<String> mylist;
publicMyAdapter(Contextcontext, List<String> mylist)
{
this.context = context;
this.mylist = mylist;
}
@OverridepublicViewgenerateView(finalintposition, ViewGroupviewGroup)
{
Viewv = LayoutInflater.from(context).inflate(R.layout.list_layout, null);
finalSwipeLayoutswipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
swipeLayout.findViewById(R.id.myimageview).setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(Viewview)
{
//The position here is always 0 for the new itemsToast.makeText(context, "my position : " + String.valueOf(position), Toast.LENGTH_SHORT).show();
//mylist.remove(position);//notifyDataSetChanged();
}
});
returnv;
}
@OverridepublicvoidfillValues(intposition, Viewview)
{
TextViewtextView = (TextView) view.findViewById(R.id.mytextview);
textView.setText(mylist.get(position));
}
@OverridepublicintgetCount()
{
returnmylist.size();
}
@OverridepublicObjectgetItem(intposition)
{
returnmylist.get(position);
}
@OverridepubliclonggetItemId(intposition)
{
returnposition;
}
remember: generateView should only do as it's name, just generate views.. do not bind any listeners or fill values.
So, a quick way to solve this problem:
move the following code:
swipeLayout.findViewById(R.id.myimageview).setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(Viewview)
{
//The position here is always 0 for the new itemsToast.makeText(context, "my position : " + String.valueOf(position), Toast.LENGTH_SHORT).show();
//mylist.remove(position);//notifyDataSetChanged();
}
});
Yes, fillValues position will work since you pass the position from getView. My mistake then i though at first the position from geverateView is the same as getView too
The problem you met is really a pitfall (my mistake)... My English is not very well, I'm not good at writing, explaining. If you are available, could you please help this project to improve the wiki to prevent other developers get into this trouble again:
Only this page: https://github.com/daimajia/AndroidSwipeLayout/wiki/SwipeAdapter
Click and swipe have some comflict.I want to enter another Activity when I click an item, but also I want to swipe left to show the BottomView and handle the BottomView.My code has something wrong? @OverRide
public void fillValues(int position, View convertView) {
@de-Stan I have tried:LinearLayout linoutMemo = (LinearLayout) convertview.findViewById(R.id.linoutMemo);But result is: the item can click to enter a new Activity,but this item can not swipe again.
@de-Stan OK,what you answer is handle the problem.But if you set an ItemClickListener on the listview from your Activity,the click effect is always.No matter the SwipeLayout is open, the item can click always.But I what I want is: when the SwipeLayout is open, the SurfaceView in this item can not clicked, but the BottomView in the item can click because I want do something in the BottomView.
How to resolve this problem?
I haven't used the librady almost a year now, but i didn't have any problems as you describe. Maybe some things change or broke with an update? Or are you doing something wrong in your side?
Some code should give some more info, also @daimajia should notice this also.
Activity
daimajia commentedon Sep 1, 2014
Could you please paste your code?
vaggelis-net commentedon Sep 2, 2014
I did some more testing and i found out that if i pass a list that aready contains some items everything seem to work fine. The position is correct and the right item is deleted.
If the list is empty and start adding later, the position will always be 0 on the new items.
daimajia commentedon Sep 2, 2014
Hey guy.
You should view the source of the
SwipeAdapter, and try to know the principle of theSwipeAdapter.remember:
generateViewshould only do as it's name, just generate views.. do not bind any listeners or fill values.So, a quick way to solve this problem:
move the following code:
to
daimajia commentedon Sep 2, 2014
Have a try, if any problem, feel free to contact me.
vaggelis-net commentedon Sep 2, 2014
Yes,
fillValuesposition will work since you pass the position fromgetView. My mistake then i though at first the position fromgeverateViewis the same asgetViewtoodaimajia commentedon Sep 2, 2014
It works right?
I think I should add this notice in Wiki. It's also my mistake. :-D
vaggelis-net commentedon Sep 2, 2014
Yes i just tested it. Well i fall for it because of the View in both generateView and the usual getView
daimajia commentedon Sep 2, 2014
ask a favor:
The problem you met is really a pitfall (my mistake)... My English is not very well, I'm not good at writing, explaining. If you are available, could you please help this project to improve the wiki to prevent other developers get into this trouble again:
Only this page:
https://github.com/daimajia/AndroidSwipeLayout/wiki/SwipeAdapter
vaggelis-net commentedon Sep 2, 2014
Yes i can do some editing if you want :) But to let you know English is not my native language too!
daimajia commentedon Sep 2, 2014
Do your best !
I trust you ! 😄
angelen10 commentedon Nov 30, 2014
Click and swipe have some comflict.I want to enter another Activity when I click an item, but also I want to swipe left to show the BottomView and handle the BottomView.My code has something wrong?
@OverRide
public void fillValues(int position, View convertView) {
My layout code:
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.daimajia.swipe.SwipeLayout>
How to handle this problem?
Sorry for my poor English.
vaggelis-net commentedon Nov 30, 2014
Try this:
LinearLayout linoutMemo = (LinearLayout) convertview.findViewById(R.id.linoutMemo);
instead of
LinearLayout linoutMemo = (LinearLayout) swipeLayout.findViewById(R.id.linoutMemo);
angelen10 commentedon Nov 30, 2014
@de-Stan I have tried:LinearLayout linoutMemo = (LinearLayout) convertview.findViewById(R.id.linoutMemo);But result is: the item can click to enter a new Activity,but this item can not swipe again.
vaggelis-net commentedon Nov 30, 2014
Ok, what i believe you should do is set an ItemClickListener on the listview from your activity/fragment:
and start the activity from there.
no need to do that inside the adapter's fillview. And maybe that's your problem, if i understand it correctly.
angelen10 commentedon Nov 30, 2014
@de-Stan OK,what you answer is handle the problem.But if you set an ItemClickListener on the listview from your Activity,the click effect is always.No matter the SwipeLayout is open, the item can click always.But I what I want is: when the SwipeLayout is open, the SurfaceView in this item can not clicked, but the BottomView in the item can click because I want do something in the BottomView.
How to resolve this problem?
vaggelis-net commentedon Nov 30, 2014
You can use the SwipeLayout's status. From the ItemClickListener you will get a view which is a SwipeLayout. Do something like this:
angelen10 commentedon Nov 30, 2014
@de-Stan My problem is solved, thank you very much!
vaggelis-net commentedon Nov 30, 2014
Glad i could help!
vaggelis-net commentedon Jul 8, 2015
Sorry i can't understand what you asking. Some code could be helpful to see what you are up to.
vaggelis-net commentedon Jul 8, 2015
I haven't used the librady almost a year now, but i didn't have any problems as you describe. Maybe some things change or broke with an update? Or are you doing something wrong in your side?
Some code should give some more info, also @daimajia should notice this also.