我已经创建了一个自定义适配器。现在,当用户点击"取消"时,我希望淡出特定的行布局。按钮。问题是我只创建了淡出的最后一行,而不是我实际按下的行:
public class SwipeableRequestsAdapter extends SwipeAdapter {
private final Context ctx;
private final Followers[] values;
private final Typeface roboto;
private ImageView check;
private ImageView cancel;
private SwipeLayout swipeLayout;
public SwipeableRequestsAdapter(Context context, Followers[] items) {
ctx = context;
values = items;
roboto = Typeface.createFromAsset(ctx.getAssets(), "roboto_thin.ttf");
}
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
@Override
public View generateView(int i, ViewGroup viewGroup) {
return LayoutInflater.from(ctx).inflate(R.layout.grid_item, null);
}
@Override
public void fillValues(int i, View view) {
swipeLayout = (SwipeLayout) view.findViewById(R.id.swipe);
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout swipeLayout) {
}
@Override
public void onUpdate(SwipeLayout swipeLayout, int i, int i2) {
}
@Override
public void onOpen(SwipeLayout swipeLayout) {
}
@Override
public void onHandRelease(SwipeLayout swipeLayout, float v, float v2) {
}
});
TextView requestText = (TextView) view.findViewById(R.id.sender);
requestText.setTypeface(roboto);
TextView requestPhone = (TextView) view.findViewById(R.id.phone);
requestPhone.setTypeface(roboto);
requestPhone.setText(values[i].getNumber_sender());
TextView currentState = (TextView) view.findViewById(R.id.currentState);
currentState.setTypeface(roboto);
TextView stateTitle = (TextView) view.findViewById(R.id.state);
stateTitle.setTypeface(roboto);
String state = values[i].getState();
if (state.equals("0")) {
currentState.setText("Pending");
} else if (state.equals("1")) {
currentState.setText("Accepted");
} else if (state.equals("2")) {
currentState.setText("Rejected");
} else if (state.equals("3")) {
currentState.setText("Track Back");
}
check = (ImageView) view.findViewById(R.id.approve);
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleEffect(v);
Toast.makeText(ctx, "Approved", Toast.LENGTH_SHORT).show();
}
});
cancel = (ImageView) view.findViewById(R.id.trash);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleEffect(v);
swipeLayout.animate().alpha(0f).setDuration(150).start();
Toast.makeText(ctx, "Canceled", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
private void scaleEffect(final View view) {
view.setScaleX(1.5f);
view.setScaleY(1.5f);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
view.post(new Runnable() {
@Override
public void run() {
view.setScaleX(1f);
view.setScaleY(1f);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
答案 0 :(得分:1)
是!!!我找到了解决方案!
我所要做的就是爬上视图层次结构 - 在列表项中我已经设置了一个按钮监听器:
cancel = (ImageView) view.findViewById(R.id.trash);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {}
但是现在我通过传递给onClick的视图获得了该特定按钮的引用 - " v",我可以要求获得它的父视图。无论如何,完整的代码如下所示:
cancel = (ImageView) view.findViewById(R.id.trash);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleEffect(v);
LinearLayout a = (LinearLayout) v.getParent();
SwipeLayout parent = (SwipeLayout) a.getParent();
parent.animate().alpha(0f).setDuration(300).start();
Toast.makeText(ctx, "Canceled", Toast.LENGTH_SHORT).show();
}
});