My code expands
the LinearLayout
that is clicked
. It works fine when there are 14 items or less in the list. Once there are more than 14 items it repeats the reference. What happens is clicking on the first item in the list expands both the 1st and the 15th item. It even seems randomized when there are 300 or more items. I've been trouble shooting this for a while with no luck
Here is my custom adapter
public class Scanvinadapter extends BaseAdapter {
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater = null;
public Resources res;
Scanvinmodel model = null;
int i = 0;
ScanlistListener mCallback;
public Scanvinadapter(Activity a, ArrayList d, Resources resLocal){
activity = a;
data = d;
res = resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
mCallback = (ScanlistListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ScanlistListener");
}
}
@Override
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView scanlistvin;
public TextView scanlistbay;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LinearLayout ll = null;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.scanlistview, null);
holder = new ViewHolder();
ll = (LinearLayout) vi.findViewById(R.id.scanlist);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
Log.v("position", String.valueOf(position));
i = i+1;
vi.setId(i);
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCallback.scanlistclick(v, 0);
}
});
//vi.setOnClickListener(new OnItemClickListener( position ));
holder.scanlistvin = (TextView) vi.findViewById(R.id.scanlistvin);
holder.scanlistbay = (TextView) vi.findViewById(R.id.scanlistbay);
vi.setTag(holder);
}else
holder = (ViewHolder)vi.getTag();
if(data.size()<=0){
holder.scanlistvin.setText("No Data");
}else{
model = null;
model = (Scanvinmodel) data.get(position);
holder.scanlistvin.setText(model.getVin());
holder.scanlistbay.setText(model.getBay());
return vi;
}
return null;
}
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", String.valueOf(mPosition) );
mCallback.scanlistclick(v, mPosition);
}
}
public interface ScanlistListener{
public void scanlistclick(View v, int i);
}
}
Here is the implement override begin called on each click. I display the id and it seems that it is duplicating ids. All the data is unique.
@Override
public void scanlistclick(View v, int i) {
new Animutils().expand(v);
Log.wtf("wtf", String.valueOf(v.getId()));
}
This is the animation that expands the view
public class Animutils {
private static final long ANIMATION_DURATION = 250;
protected static final String TAG = "Animutils.java";
public void expand(final View v) {
//v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
//final int targtetHeight = 500;
//v.getLayoutParams().height = 114;
// v.setVisibility(View.VISIBLE);
Animation a = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT : (int)(targtetHeight * interpolatedTime);
//v.getLayoutParams().height = interpolatedTime == 1 ? 500 : (int)(targtetHeight + 25);
if(interpolatedTime == 0){
v.getLayoutParams().height = targtetHeight + 25;
}else{
if(v.getLayoutParams().height == 500){
cancel();
}else{
v.getLayoutParams().height = v.getLayoutParams().height +25;
}
}
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(ANIMATION_DURATION);
// a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(ANIMATION_DURATION);
// a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
UPDATED ADAPTER
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LinearLayout ll = null;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.scanlistview, null);
ll = (LinearLayout) vi.findViewById(R.id.scanlist);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
holder = new ViewHolder();
holder.scanlist = (LinearLayout) vi.findViewById(R.id.scanlist);
holder.scanlistvin = (TextView) vi.findViewById(R.id.scanlistvin);
holder.scanlistbay = (TextView) vi.findViewById(R.id.scanlistbay);
Log.v("position", String.valueOf(position));
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
ll = holder.scanlist;
Log.v("position", String.valueOf(position));
}
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCallback.scanlistclick(v, 0);
}
});
if(data.size()<=0){
holder.scanlistvin.setText("No Data");
}else{
model = null;
model = (Scanvinmodel) data.get(position);
holder.scanlistvin.setText(model.getVin());
holder.scanlistbay.setText(model.getBay());
}
return vi;
}