2014-09-15 4 views
-1

У меня есть ListView, который показывает строки «заголовки», и я хочу, чтобы возможно открыть «тела» на кране и снова закрыть его. Все могут себе это представить (это как Expandable ListView). Поэтому я не хочу использовать Expandable ListView, но не могу заставить исчезнуть «тело» моих строк. Вот код:Show/hide listview item onclick

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; 
} 

Как вы можете видеть, что я установить обзорность GONE в моем адаптере, а затем в методе OnClick сделать VISIBLE и это работает! Но когда я снова нажимаю на «заголовок», ничего не происходит! Что я должен сделать, чтобы скрыть тело после того, как я его уже показываю?

+0

Что не так с ExpandableListView? –

+0

@AlexanderZhak нет, я просто хочу сделать все правильно :) – vendettacore

+0

Опубликуйте свой макет, пожалуйста. – 323go

ответ

-1

Для этого вы можете использовать Expandable Library. Это похоже на ExpandableListview, но вы должны установить макет для заголовка и контента;)

+1

Это похоже на хорошую идею. Спасибо за информацию. Попробуем в ближайшее время. – greenapps