简体   繁体   English

使用自定义ListView时的SetVisibility

[英]SetVisibility when use custom ListView

I've made a list view. 我做了一个列表视图。 The list view works properly. 列表视图正常工作。 But the view list item is a xml file. 但是视图列表项是一个xml文件。 Which contains a imagView and a layer. 其中包含一个imagView和一个图层。 I want when the user clicks on the ImagView, the layer to be displayed. 我想要当用户单击ImagView时要显示的图层。 The problem is, is that when the user clicks ImagView was Items layers 3 and 13, is displayed (twice). 问题是,当用户单击ImagView时,项目层3和13被显示(两次)。 I do not know what's the problem? 不知道是什么问题? Somebody help? 有人帮忙吗?

custom_item.xml custom_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImagView
    android:id="@+id/imagView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#0f0" />


   <LinearLayout
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00f" />

</LinearLayout>

.java .java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder Holder;

    if (convertView == null) {
        Holder = new ViewHolder();
        convertView = myInflater.inflate(R.layout.cystom_item, null);

        Holder.my_image= (ImageView) convertView.findViewById(R.id.imagView);

        Holder.linear = (LinearLayout) convertView.findViewById(R.id.linear2);

    } else
        Holder = (ViewHolder) convertView.getTag();

    Holder.my_image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (lastView == Holder.linear);
            else {

                if (lastView != null)
                    lastView.setVisibility(View.GONE);

                    Holder.linear.startAnimation(anim);
                    Holder.linear.setVisibility(View.VISIBLE);
                    lastView = Holder.linear;
                }
        }
    });

    return convertView;
}

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    return null;
}


public class ViewHolder
{

ImageView my_image;
LinearLayout linear;

}

You wire the onClickListener multiple times: 您多次连接onClickListener:

Holder.linear1.setOnClickListener(new OnClickListener()...

Set the clickListener only if it's a new Holder and not a recycled one. 仅当clickListener是新的Holder而不是可回收的Holder时才设置。

You should notice that you don't have curly braces here: 您应该注意这里没有花括号:

else
   Holder = (ViewHolder) convertView.getTag();

So everything after this line is executing. 因此,此行之后的所有内容都在执行。

Try this: 尝试这个:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder Holder;

if (convertView == null) {
    Holder = new ViewHolder();
    convertView = myInflater.inflate(R.layout.cystom_item, null);

    Holder.my_image= (ImageView) convertView.findViewById(R.id.imagView);

    Holder.linear = (LinearLayout) convertView.findViewById(R.id.linear2);

} else
{
    Holder = (ViewHolder) convertView.getTag();
    Holder.my_image.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if (lastView == Holder.linear);
        else {

            if (lastView != null)
                lastView.setVisibility(View.GONE);

                Holder.linear.startAnimation(anim);
                Holder.linear.setVisibility(View.VISIBLE);
                lastView = Holder.linear;
            }
    }
  });
 }

  return convertView;
 }

Actually i think that you have placed the below if statement two times in the code, on the first time you done this with if statement and the second time, you initialized that in the else statement. 实际上,我认为您已经在代码中两次放置了以下if语句,第一次使用if语句完成了此操作,第二次在else语句中进行了初始化。 I think that this is causing the doubling of the listitem views. 我认为这导致listitem视图加倍。

 if (lastView == Holder.linear); 

You should try this: 您应该尝试这样:

 { if (lastView != null) lastView.setVisibility(View.GONE); else { Holder.linear.startAnimation(anim); Holder.linear.setVisibility(View.VISIBLE); lastView = Holder.linear; } } 

Instead of this: 代替这个:

 { if (lastView == Holder.linear); else { if (lastView != null) lastView.setVisibility(View.GONE); Holder.linear.startAnimation(anim); Holder.linear.setVisibility(View.VISIBLE); lastView = Holder.linear; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM