2014-02-12 113 views
0

我在另一個ListView的行內使用ListView。在wishlist.xml,我有一個ListView。該項目在wishlist_items.xml中,在wishlist_items中也有一個listView。這是設計在wishlist_items_advisors.xml。我的問題是,發送ListView只顯示一個項目。任何人都可以告訴我如何解決這個問題嗎? 而適配器也在下面給出。包含另一個ListView的ListView行

wishlist.xml

<LinearLayout 
     android:id="@+id/logo_container" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="15dp" 
      android:contentDescription="@string/app_name" /> 

     <LinearLayout 
      android:id="@+id/title_container" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/titleredbg" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="@string/wishlist_title" 
       android:textColor="#fff" 
       android:textSize="20sp" /> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_marginTop="10dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/help" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="10dp" 
      android:contentDescription="@string/app_name" /> 

     <Button 
      android:id="@+id/add_person" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginRight="10dp" 
      android:contentDescription="@string/app_name" 
      android:padding="5dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="57" 
      android:orientation="horizontal" 
      android:weightSum="100" > 

      <TextView 
       android:id="@+id/wishlist_name_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="40" 
       android:text="@string/wishlist_name" 
       android:textColor="@color/Black" /> 

      <TextView 
       android:id="@+id/wishlist_email_text" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="60" 
       android:text="@string/wishlist_email" 
       android:textColor="@color/Black" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/wishlist_relation_text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="43" 
      android:text="@string/wishlist_relation" 
      android:textColor="@color/Black" /> 
    </LinearLayout> 



    <LinearLayout 
     android:id="@+id/items_footer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="10dp" 
     android:orientation="vertical" 
     android:weightSum="100" > 

     <ListView 
      android:id="@+id/listView_main" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:divider="@null" 
      android:dividerHeight="4dp" 
      android:visibility="visible" > 
     </ListView> 

     <LinearLayout 
      android:id="@+id/empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#fff" 
      android:gravity="center" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:text="@string/no_data" 
       android:textColor="#000" > 
      </TextView> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

wishlist_items.xml

<TextView 
     android:id="@+id/hr1" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_marginTop="5dp" 
     android:background="#D2D2D2" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff" 
     android:baselineAligned="false" 
     android:orientation="horizontal" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" 
     android:weightSum="100" > 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="57" 
      android:orientation="horizontal" 
      android:weightSum="100" > 

      <TextView 
       android:id="@+id/wishlist_name" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="40" 
       android:ellipsize="end" 
       android:maxLines="2" 
       android:text="John John John John John John" 
       android:textColor="@color/Black" /> 

      <TextView 
       android:id="@+id/wishlist_email" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="60" 
       android:ellipsize="end" 
       android:maxLines="2" 
       android:text="[email protected] [email protected]" 
       android:textColor="@color/Black" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="43" 
      android:orientation="horizontal" 
      android:weightSum="100" > 

      <TextView 
       android:id="@+id/wishlist_relation" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="60" 
       android:ellipsize="end" 
       android:maxLines="2" 
       android:text="Birthday Birthday vv Birthday Birthday" 
       android:textColor="@color/Black" /> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_weight="40" > 

       <ImageButton 
        android:id="@+id/editButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="10dp" 
        android:layout_toLeftOf="@+id/deleteButton" 
        android:background="@drawable/wishlistediticon" 
        android:contentDescription="@string/app_name" /> 

       <ImageButton 
        android:id="@+id/deleteButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="10dp" 

        android:contentDescription="@string/app_name" /> 
      </RelativeLayout> 
     </LinearLayout> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/hr4" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#D2D2D2" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" > 

     <TextView 
      android:id="@+id/gift_advisor_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/wishlist_getadvisor" 
      android:textColor="@color/Black" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/hr1" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#D2D2D2" /> 

    <ListView android:id="@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     ></ListView> 

    <TextView 
     android:id="@+id/hr5" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#D2D2D2" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:background="#fff" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     > 


     <ImageButton 
      android:id="@+id/invite_advisor" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 

      android:contentDescription="@string/app_name" /> 
    </RelativeLayout> 
</LinearLayout> 

wishlist_items_advisors.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="wrap_content" 
    android:background="#fff" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp" 
    android:weightSum="100" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="57" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <TextView 
      android:id="@+id/advisor_name_text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="40" 
      android:ellipsize="end" 
      android:maxLines="2" 
      android:text="John John" 
      android:textColor="@color/Black" /> 

     <TextView 
      android:id="@+id/advisor_email_text" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="60" 
      android:ellipsize="end" 
      android:maxLines="2" 
      android:text="krishna." 
      android:textColor="@color/Black" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="43" > 

     <TextView 
      android:id="@+id/status_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 
      android:layout_toLeftOf="@+id/deleteButton" 
      android:text="Accept" /> 

     <ImageButton 
      android:id="@+id/deleteButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 

      android:contentDescription="@string/app_name" /> 
    </RelativeLayout> 

</LinearLayout> 

CustomAdapter

public class CustomAdapter extends BaseAdapter{ 

     private String guestIds[]=null; 
     private String names[]=null; 
     private String emails[] = null; 
     private String relationships[] = null; 
     private String occasions[] = null; 
     DisplayImageOptions doption=null; 
     private ImageLoadingListener animateFirstListener =null; 
     private Context context=null; 

     public CustomAdapter(Activity activity,String[] guestId,String[] name,String[] email,String[] relationship, String[] occasion) 
     { 

      this.context=activity; 
      this.guestIds = guestId; 
      this.names =name; 
      this.emails = email; 
      this.relationships = relationship; 
      this.occasions = occasion; 
      doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(5)).build(); 
      animateFirstListener = new AnimateFirstDisplayListener(); 
     } 

     @Override 
     public int getViewTypeCount() { 
      return 2; 
     } 

     @Override 
     public int getItemViewType(int position) { 
      //CustomAdapter item = (CustomAdapter) getItem(position); 
      if (isItemAnAd(position)) { 
       return 0; 
       } else { 
       return 1; 
       } 
     } 
     private boolean isItemAnAd(int position) { 
       // Place an ad at the first 
       return (position == 0); 
      } 
     @Override 
     public Object getItem(int arg0) { 
      return arg0; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      return arg0; 
     } 

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

      View view = convertView; 
      final ViewHolder holder; 

      if (convertView == null) { 
       view = ((Activity) context).getLayoutInflater().inflate(R.layout.wishlist_items, parent, false); 
       holder = new ViewHolder(); 
       holder.wishlistName = (TextView) view.findViewById(R.id.wishlist_name); 
       holder.wishlistEmail = (TextView) view.findViewById(R.id.wishlist_email); 
       holder.wishlistRelation = (TextView) view.findViewById(R.id.wishlist_relation); 
       holder.wishGiftAdvisorText = (TextView) view.findViewById(R.id.gift_advisor_text); 
       holder.advisorListview = (ListView) view.findViewById(R.id.listView); 
       holder.inviteAdvisor = (ImageButton) view.findViewById(R.id.invite_advisor); 

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

      holder.wishlistName.setText(names[position]); 
      holder.wishlistEmail.setText(emails[position]); 
      holder.wishlistRelation.setText(relationships[position]); 
      holder.wishGiftAdvisorText.setText(getResources().getString(R.string.wishlist_getadvisor)+" "+names[position]+"'s "+getResources().getString(R.string.wishlist_title)); 
      GuestId = guestIds[position]; 
      holder.wishlistName.setTypeface(tf); 
      holder.wishlistEmail.setTypeface(tf); 
      holder.wishlistRelation.setTypeface(tf); 
      holder.wishGiftAdvisorText.setTypeface(tf); 

      if(occasions[position].contains("[")) { 
       try { 
        array = new JSONArray(occasions[position]); 
        System.out.println(array.toString(2)); 
        //loadOccasionData(array); 


        // TODO Auto-generated method stub 

        if(array!= null) { 

         advisorIds = new String[array.length()]; 
         advisorNames = new String[array.length()]; 
         advisorEmails = new String[array.length()]; 
         advisorRelationships = new String[array.length()]; 
         advisorStatuses = new String[array.length()]; 

         for (int i = 0; i < array.length(); i++) { 
          JSONObject c; 
          try { 

           c = array.getJSONObject(i); 

           // Storing each json item in variable 

           advisorIds[i] = c.getString("advisor_id"); 
           advisorNames[i] = c.getString("name"); 
           advisorEmails[i] = c.getString("email"); 
           advisorRelationships[i] = c.getString("relationship"); 
           advisorStatuses[i] = c.getString("status"); 

          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

         } 
         CustomAdvisorAdapter adapter = new CustomAdvisorAdapter(WishList.this, 
           advisorIds, advisorNames, advisorEmails, advisorRelationships , advisorStatuses); 
         holder.advisorListview.setAdapter(adapter); 

        } 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 

       holder.advisorListview.setAdapter(null); 

      } 



      return view; 
     } 

     private class AnimateFirstDisplayListener extends SimpleImageLoadingListener { 

      final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); 

      @Override 
      public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
       if (loadedImage != null) { 
        ImageView imageView = (ImageView) view; 
        boolean firstDisplay = !displayedImages.contains(imageUri); 
        if (firstDisplay) { 
         FadeInBitmapDisplayer.animate(imageView, 500); 
         displayedImages.add(imageUri); 
        } 
       } 
      } 
     } 

     private class ViewHolder { 
      public TextView wishlistName; 
      public TextView wishlistEmail; 
      public TextView wishlistRelation; 
      public TextView wishGiftAdvisorText; 
      public ListView advisorListview; 
      public ImageButton inviteAdvisor; 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return names.length; 
     } 
    } 

CustomAdvisorAdapter

public class CustomAdvisorAdapter extends BaseAdapter{ 

     private String advisorIds[]=null; 
     private String advisorNames[]=null; 
     private String advisorEmails[] = null; 
     private String advisorRelationships[] = null; 
     private String advisorStatuses[] = null; 
     DisplayImageOptions doption=null; 
     private ImageLoadingListener animateFirstListener =null; 
     private Context context=null; 

     public CustomAdvisorAdapter(Activity activity,String[] advisorId,String[] advisorName,String[] advisorEmail,String[] advisorRelationship, String[] advisorStatus) 
     { 

      this.context=activity; 
      this.advisorIds = advisorId; 
      this.advisorNames =advisorName; 
      this.advisorEmails = advisorEmail; 
      this.advisorRelationships = advisorRelationship; 
      this.advisorStatuses = advisorStatus; 
      doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(5)).build(); 
      animateFirstListener = new AnimateFirstDisplayListener(); 
     } 


     @Override 
     public Object getItem(int arg0) { 
      return arg0; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      return arg0; 
     } 

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

      View viewAdvisor = convertView; 
      final ViewHolder advisorHolder; 

      if (convertView == null) { 
       viewAdvisor = ((Activity) context).getLayoutInflater().inflate(R.layout.wishlist_items_advisor, parent, false); 
       advisorHolder = new ViewHolder(); 
       advisorHolder.advisorNameText = (TextView) viewAdvisor.findViewById(R.id.advisor_name_text); 
       advisorHolder.advisorEmailText = (TextView) viewAdvisor.findViewById(R.id.advisor_email_text); 
       advisorHolder.statusText = (TextView) viewAdvisor.findViewById(R.id.status_text); 


       viewAdvisor.setTag(advisorHolder); 
      } else { 
       advisorHolder = (ViewHolder) viewAdvisor.getTag(); 
      } 

      advisorHolder.advisorNameText.setText(advisorNames[position]); 
      advisorHolder.advisorEmailText.setText(advisorEmails[position]); 
      advisorHolder.statusText.setText(advisorStatuses[position]); 

      advisorHolder.advisorNameText.setTypeface(tf); 
      advisorHolder.advisorEmailText.setTypeface(tf); 
      advisorHolder.statusText.setTypeface(tf); 


      return viewAdvisor; 
     } 

     private class AnimateFirstDisplayListener extends SimpleImageLoadingListener { 

      final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); 

      @Override 
      public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
       if (loadedImage != null) { 
        ImageView imageView = (ImageView) view; 
        boolean firstDisplay = !displayedImages.contains(imageUri); 
        if (firstDisplay) { 
         FadeInBitmapDisplayer.animate(imageView, 500); 
         displayedImages.add(imageUri); 
        } 
       } 
      } 
     } 

     private class ViewHolder { 
      public TextView advisorNameText; 
      public TextView advisorEmailText; 
      public TextView statusText; 

     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return names.length; 
     } 
    } 
+0

這個問題太長了。不要在這裏發佈代碼/佈局文件,你應該共享整個項目(樣本),以便有人可以嘗試並可以幫助你! –

+1

爲什麼你不使用可擴展列表來達到你的目的?我認爲你需要重寫'onMeasure' –

+1

'有另一個listview'的listview行告訴一件事:使用['ExpandableListView'](http://developer.android.com/reference/android/widget/ExpandableListView.html) – gunar

回答

0

可以ExpandableListView到位使得自定義視圖還用於數據處理的,你可以使用ExpandableListAdapter

+0

現在無法在ExpandableListview上工作,任何人都可以幫助我只在上面的代碼 – Android

+0

@Android你正在嘗試的方式是完全不可能的事情來管理。你不能在listview裏面添加listview。 'ExpandableListView'是這種需求的唯一解決方案。 – GrIsHu

1

無法在可滾動視圖內製作可滾動視圖。但作爲一個解決這個問題的方法,並且只有在所有視圖都被加載的情況下,這個listviews纔會佔用很多內存。 您可以再次使用這個

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ListView; 

public class NonScrollableListView extends ListView { 
    public NonScrollableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // Do not use the highest two bits of Integer.MAX_VALUE because they are 
     // reserved for the MeasureSpec mode 
     int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightSpec); 
     getLayoutParams().height = getMeasuredHeight(); 
    } 

} 

,這不是很好的使用此解決辦法

你可以將其作爲一個定製的UI組件使用的非滾動列表視圖中child.xml佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <com.youpackage.uiutils.NonScrollableListView 
     android:id="@+id/non_scrollable_listview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
+0

我在哪裏需要調用這個onMeasure函數以及哪個適配器......你能解釋一下 – Android

+0

你將使用這個自定義的listview作爲listview的子項,但是從內存規範來說它並不是一個好的解決方法。嘗試使用Expandable列表視圖。我發現這個教程幫助我開發了我的第一個可擴展列表:http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.html –

相關問題