显示/隐藏列表视图项目onclick

时间:2014-09-15 20:31:28

标签: android listview onclicklistener

ListView显示行“标题”,我希望可以点击打开“正文”并再次点按它。每个人都可以想象它(就像Expandable ListView)。所以我不想使用Expandable ListView但不能使我的行的“身体”消失。这是代码:

feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, final View view, int position, long arg) {             
            LinearLayout body = (LinearLayout) view.findViewById(R.id.lInfo);
            ImageView div = (ImageView) view.findViewById(R.id.divider);

           switch(body.getVisibility()){
                case (View.GONE): {                       
                    body.setVisibility(View.VISIBLE);
                    div.setVisibility(View.VISIBLE);
                    Log.w("body vis",""+body.getVisibility());
                    Log.w("set visible","VISIBLE!");
                }
                break;
               case (View.VISIBLE):{                       
                   body.setVisibility(View.GONE);
                   div.setVisibility(View.GONE);
               }
               break;

              }

        }
    });
}

我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">




<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fon1">

<com.susudashonline.ParallaxListView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lvContacts"
android:layout_gravity="center_horizontal"
android:divider="#00ffffff"
android:dividerHeight="1dp"
android:clickable="false"/>
</RelativeLayout>



<ListView android:id="@+id/left_drawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#002d1816"
android:dividerHeight="2dp"
android:background="#D92d1816"/>


</android.support.v4.widget.DrawerLayout>

我的 list_item 布局:

<?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="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/fName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:gravity="center"
    android:minHeight="40dp"
    android:background="#2d1816"
    />


<ImageView
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/divider"
    android:contentDescription="@string/imgDesc"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="-1dp"

  />
<LinearLayout
    android:id="@+id/lInfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:background="#CC2d1816"

    >
    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="address"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:id="@+id/tvDekanat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvPhone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:autoLink="email"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:id="@+id/tvSite"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:autoLink="web"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvEmailTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:autoLink="email"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvSiteTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:autoLink="web"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvTime"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvShedule"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tvWorkTime"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffff"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:text="@string/workTime"
        android:textSize="17sp"
        android:layout_marginTop="10dp"/>
    <LinearLayout
        android:id="@+id/llShedule"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvShedule1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
            <TextView
                android:id="@+id/tvTime1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
            </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvShedule2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
            <TextView
                android:id="@+id/tvTime2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvShedule3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
            <TextView
                android:id="@+id/tvTime3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvShedule4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
            <TextView
                android:id="@+id/tvTime4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_marginTop="10dp"/>
        </LinearLayout>
        </LinearLayout>

</LinearLayout>
</LinearLayout>

这是我的适配器 getView

 public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.contact_item, null);
        holder = new ViewHolder();
        holder.body = (LinearLayout)convertView.findViewById(R.id.lInfo);
        holder.facultyName = (TextView) convertView.findViewById(R.id.fName);
        holder.mAddress = (TextView) convertView.findViewById(R.id.tvAddress);
        holder.mDekanat = (TextView) convertView.findViewById(R.id.tvDekanat);
        holder.mPhone = (TextView) convertView.findViewById(R.id.tvPhone);
        holder.mEmail = (TextView) convertView.findViewById(R.id.tvEmail);
        holder.mSite = (TextView) convertView.findViewById(R.id.tvSite);
        holder.mSiteTwo = (TextView) convertView.findViewById(R.id.tvSiteTwo);
        holder.mEmailTwo = (TextView) convertView.findViewById(R.id.tvEmailTwo);
        holder.mShedule = (TextView) convertView.findViewById(R.id.tvShedule);
        holder.mTime = (TextView) convertView.findViewById(R.id.tvTime);
        holder.mTime1 = (TextView) convertView.findViewById(R.id.tvTime1);
        holder.mTime2 = (TextView) convertView.findViewById(R.id.tvTime2);
        holder.mTime3 = (TextView) convertView.findViewById(R.id.tvTime3);
        holder.mTime4 = (TextView) convertView.findViewById(R.id.tvTime4);
        holder.llShedule = (LinearLayout)convertView.findViewById(R.id.llShedule);
        holder.mShedule1 = (TextView) convertView.findViewById(R.id.tvShedule1);
        holder.mShedule2 = (TextView) convertView.findViewById(R.id.tvShedule2);
        holder.mShedule3 = (TextView) convertView.findViewById(R.id.tvShedule3);
        holder.mShedule4 = (TextView) convertView.findViewById(R.id.tvShedule4);
        holder.mWorkTime = (TextView) convertView.findViewById(R.id.tvWorkTime);
        holder.mDivider = (ImageView) convertView.findViewById(R.id.divider);

// holder.mImage =(ImageView)convertView.findViewById(R.id.imgContact);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }
    ContactItem contactItem = (ContactItem) listData.get(position);
    Typeface font = Typeface.createFromAsset(mContext.getAssets(), "OpenSans-Regular.ttf");
    holder.facultyName.setTypeface(font);
    holder.facultyName.setText(contactItem.getName());
    holder.mEmail.setVisibility(View.GONE);
    holder.mDekanat.setVisibility(View.GONE);
    holder.mPhone.setVisibility(View.GONE);
    holder.mSite.setVisibility(View.GONE);
    holder.mAddress.setVisibility(View.GONE);
    holder.mSiteTwo.setVisibility(View.GONE);
    holder.mShedule.setVisibility(View.GONE);
    holder.mEmailTwo.setVisibility(View.GONE);
    holder.mTime.setVisibility(View.GONE);
    holder.llShedule.setVisibility(View.GONE);
    holder.mWorkTime.setVisibility(View.GONE);
    holder.body.setVisibility(View.GONE);

    return convertView;
}

正如你所看到的,我在我的适配器中将VISIBILITY设置为GONE,然后在OnClick方法中使其可见并且它可以工作!但当我再次点击“标题”时,没有任何反应!在我展示身体后,我该怎么做才能隐藏身体?

1 个答案:

答案 0 :(得分:-1)

您可以使用Expandable Library来实现此目的。它就像ExpandableListview,但您必须为标题和内容设置布局;)