我有一个OnClickListener
的线性布局。监听器正在工作,但它在多行上被触发(当通过列表时,还有其他行也有效果)。
以下是getView
中ArrayAdapter
的代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TaskRowHolder holder;
if (convertView == null)
{
holder = new TaskRowHolder();
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder.audit_flag = (ImageView) convertView.findViewById(R.id.audit_flag);
holder.expandable = (LinearLayout) convertView.findViewById(R.id.expandable);
holder.camera_button = (ImageButton) convertView.findViewById(R.id.camera_button);
holder.forms_button = (ImageButton) convertView.findViewById(R.id.forms_button);
holder.attachments_button = (ImageButton) convertView.findViewById(R.id.attachments_button);
holder.label = (LinearLayout) convertView.findViewById(R.id.label);
holder.functions_layout = (LinearLayout) convertView.findViewById(R.id.functions_layout);
holder.expandable_icon = (ImageView) convertView.findViewById(R.id.expandabl_icon);
convertView.setTag(holder);
}
else
{
holder = (TaskRowHolder) convertView.getTag();
}
holder.expandable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if (!holder.functions_layout.isShown())
{
holder.functions_layout.setVisibility(View.VISIBLE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_up);
holder.label.setAnimation(null);
holder.label.startAnimation(animationDown);
}
else
{
holder.functions_layout.setVisibility(View.GONE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_down);
holder.label.setAnimation(null);
holder.label.startAnimation(animationDown);
}
}
});
.
.
.
.
答案 0 :(得分:0)
ArrayList<String> expandablesClicked = new ArrayList<>(); //storing the row positions.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TaskRowHolder holder;
if (convertView == null)
{
holder = new TaskRowHolder();
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder.audit_flag = (ImageView) convertView.findViewById(R.id.audit_flag);
holder.expandable = (LinearLayout) convertView.findViewById(R.id.expandable);
holder.camera_button = (ImageButton) convertView.findViewById(R.id.camera_button);
holder.forms_button = (ImageButton) convertView.findViewById(R.id.forms_button);
holder.attachments_button = (ImageButton) convertView.findViewById(R.id.attachments_button);
holder.label = (LinearLayout) convertView.findViewById(R.id.label);
holder.functions_layout = (LinearLayout) convertView.findViewById(R.id.functions_layout);
holder.expandable_icon = (ImageView) convertView.findViewById(R.id.expandabl_icon);
convertView.setTag(holder);
}
else
{
holder = (TaskRowHolder) convertView.getTag();
}
holder.expandable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if (!expandablesClicked.contains(position+"")) //ADDED
{
expandablesClicked.add(position+""); //ADDED
holder.functions_layout.setVisibility(View.VISIBLE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_up);
holder.label.setAnimation(null);
holder.label.startAnimation(animationDown);
}
else
{
expandablesClicked.remove(position+""); //ADDED
holder.functions_layout.setVisibility(View.GONE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_down);
holder.label.setAnimation(null);
holder.label.startAnimation(animationDown);
}
}
});
//ADDED
if (!expandablesClicked.contains(position+""))
{
holder.functions_layout.setVisibility(View.GONE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_down);
}
else
{
holder.functions_layout.setVisibility(View.VISIBLE);
holder.expandable_icon.setBackgroundResource(R.drawable.arrow_up); } 。 。 。
答案 1 :(得分:0)
如果您希望尽可能轻松地处理您提到的所需行为(一次只点击一个列表行),请使用setOnItemClickListener而不是在每个视图上应用onClickListener并检查多个条件。实施就像 -
yourlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = list.getItemAtPosition(position); //get your view restricted to your list row here and typecast to required layout.
}
});
是的,这只会在您点击的行上调用。