2016-08-01 30 views
0

Ich benutze eine Listenansicht in einem Listenansicht, und beide Listenansicht teilen denselben Listeneintrag. Es funktioniert gut, aber wenn die Höhe des Artikels in der Listenansicht erhöht wird, wird der Inhalt nicht umgebrochen und ein Scrollview eingefügt, der nicht der Anforderung entspricht. Bitte beachten Sie die folgenden CodesListenansicht innerhalb der Liste Anzeigen in android

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@+id/list_comments" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="5dp" /> 

</RelativeLayout> 

comments_for_topic.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="bottom" 
    android:background="#fafafa"> 

    <RelativeLayout 
     android:id="@+id/commentsRelLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingRight="10dp" 
     android:paddingLeft="7dp" 
     android:background="#d5d5d5" 
     android:orientation="vertical"> 
     <TextView 
      android:id="@+id/UserName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15sp" 

      android:text="User name"/> 
     <TextView 
      android:id="@+id/Comment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/UserName" 
      android:textSize="12sp" 
      android:padding="5dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_toLeftOf="@+id/showRep" 
      android:text="Comment test 1"/> 
     <TextView 
      android:id="@+id/showRep" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="2Replies" 
      android:layout_toLeftOf="@+id/Reply" 
      android:layout_marginRight="3dp" 
      android:textSize="12dp" 
      android:textColor="@android:color/holo_blue_dark" 
      android:layout_below="@+id/UserName"/> 

     <TextView 
      android:id="@+id/Reply" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Reply" 
      android:textSize="12dp" 
      android:textColor="@android:color/holo_blue_dark" 
      android:layout_below="@+id/UserName" 
      android:layout_alignParentRight="true"/> 
    </RelativeLayout> 


    <ListView 
     android:id="@+id/list_commentsLevel1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/commentsRelLayout" 
     android:layout_alignLeft="@+id/commentsRelLayout" 
     android:layout_marginLeft="34dp" 
     android:background="#d5d5d5" 
     android:layout_marginTop="2dp" 
     android:scrollbars="none" 
     android:divider="@android:color/white" 
     android:dividerHeight="1dp" 
     ></ListView> 
    <TextView 
     android:id="@+id/showmorebtn" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="4dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/show_more" 
     android:layout_below="@+id/list_commentsLevel1" 
     android:layout_alignLeft="@+id/list_commentsLevel1" 
     android:textColor="@android:color/holo_blue_dark" 
     /> 

</RelativeLayout> 

AdapterComments.java

public class AdapterComments extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<CommentListModel> itemsArrayList = new ArrayList<CommentListModel>(); 
    private ArrayList<CommentListModel> Comments = new ArrayList<CommentListModel>(); 
    CommentListModel tempValues=null; 
    private static LayoutInflater inflater=null; 
    ArrayList<AdapterComments> adapterCommentsArrayList = new ArrayList<AdapterComments>(); 
    ViewHolder holder = new ViewHolder(); 
    ViewHolder viewHolder; 
    ArrayList<String> list =new ArrayList<String>(); 
    private int currentID=0; 
    boolean isChild =false; 
    FragmentDiscussinTopic fragmentDiscussinTopic; 

    public AdapterComments() { 
    } 

    public AdapterComments(Activity context, FragmentDiscussinTopic fragmentDiscussinTopic, ArrayList<CommentListModel> itemsArrayList) { 
     this.activity = context; 
     this.itemsArrayList = itemsArrayList; 
     this.isChild = false; 
     this.fragmentDiscussinTopic = fragmentDiscussinTopic; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     for (int i = 0 ; i<itemsArrayList.size();i++){ 
      AdapterComments adapterComments1 = new AdapterComments(); 
      adapterCommentsArrayList.add(adapterComments1); 
     } 

    } 

    public AdapterComments(Activity context, ArrayList<CommentListModel> itemsArrayList) { 
     this.activity = context; 
     this.itemsArrayList = itemsArrayList; 
     this.isChild = true; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     for (int i = 0 ; i<itemsArrayList.size();i++){ 
      AdapterComments adapterComments1 = new AdapterComments(); 
      adapterCommentsArrayList.add(adapterComments1); 
     } 

    } 

    @Override 
    public int getCount() { 
      if(itemsArrayList.size()<=0) 
       return 1; 
      return itemsArrayList.size(); 
    } 

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

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



    public static class ViewHolder{ 
     public TextView UserName; 
     public TextView Comment; 
     public TextView showRep; 
     public TextView Reply; 
     public TextView showmorebtn; 
     public ListView list_commentsLevel1; 
    } 

    @Override 
    public View getView(final int position, View rowView, ViewGroup parent) { 
     View v1 = rowView; 





     if(rowView==null){ 

      v1 = inflater.inflate(R.layout.comments_for_topic,null); 


      viewHolder =new ViewHolder(); 
      viewHolder.UserName=(TextView)v1.findViewById(R.id.UserName); 
      viewHolder.Comment=(TextView)v1.findViewById(R.id.Comment); 
      viewHolder.showRep=(TextView)v1.findViewById(R.id.showRep); 
      viewHolder.showmorebtn=(TextView)v1.findViewById(R.id.showmorebtn); 
      viewHolder.Reply=(TextView)v1.findViewById(R.id.Reply); 
      viewHolder.list_commentsLevel1=(ListView)v1.findViewById(R.id.list_commentsLevel1); 
      v1.setTag(viewHolder); 
     } 
     else 
      viewHolder=(ViewHolder)v1.getTag(); 

     if(itemsArrayList.size()<=0) 
     { 
      Log.e("data", " no data found"); 

     } 
     else { 


      tempValues = itemsArrayList.get(position); 

      if (tempValues.isVisible()){ 
       viewHolder.list_commentsLevel1.setAdapter(adapterCommentsArrayList.get(position)); 

       viewHolder.list_commentsLevel1.setVisibility(View.VISIBLE); 
       ObjectAnimator anim = ObjectAnimator.ofFloat(viewHolder.list_commentsLevel1, "alpha", 0f, 1f); 
       anim.setDuration(1000); 
       anim.start(); 
       setListViewHeightBasedOnChildren(viewHolder.list_commentsLevel1); 
//    scrollMyListViewToBottom(); 

      }else { 
       Animation animation = new TranslateAnimation(0,0,0,1000); 
       animation.setDuration(1000); 
       viewHolder.list_commentsLevel1.startAnimation(animation); 
       viewHolder.list_commentsLevel1.setVisibility(View.GONE); 
      } 

      viewHolder.UserName.setText(tempValues.getUserName()); 
      viewHolder.Comment.setText(tempValues.getComment()); 
      int subcmntcount = tempValues.getSubCommentCount(); 
      if (subcmntcount>=1){ 

       viewHolder.showRep.setVisibility(View.VISIBLE); 
       if (subcmntcount==1){ 
        String repText=new String("1Reply"); 
        SpannableString content = new SpannableString(repText); 
        content.setSpan(new UnderlineSpan(), 0, repText.length(), 0); 
        viewHolder.showRep.setText(content); 
       }else { 
        String repText=new String(subcmntcount+"Replies"); 
        SpannableString content = new SpannableString(repText); 
        content.setSpan(new UnderlineSpan(), 0, repText.length(), 0); 
        viewHolder.showRep.setText(content); 
       } 
       viewHolder.showmorebtn.setVisibility(View.GONE); 
      }else { 
       viewHolder.showRep.setVisibility(View.GONE); 
       viewHolder.list_commentsLevel1.setVisibility(View.GONE); 
       viewHolder.showmorebtn.setVisibility(View.GONE); 
      } 

      if (tempValues.isChild()){ 
       viewHolder.Reply.setVisibility(View.GONE); 
       viewHolder.showRep.setVisibility(View.GONE); 
      }else { 
       viewHolder.Reply.setVisibility(View.VISIBLE); 
       String repText=new String("Reply"); 
       SpannableString content = new SpannableString(repText); 
       content.setSpan(new UnderlineSpan(), 0, repText.length(), 0); 
       viewHolder.Reply.setText(content); 
      } 



       v1.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 



          for (int i = 0;i<itemsArrayList.size();i++){ 
           CommentListModel commentListModel = (CommentListModel) itemsArrayList.get(i); 
           if (i==position){ 

            isChild =true; 
            if (commentListModel.getSubCommentCount()>0){ 
             commentListModel.setVisible(true); 
             itemsArrayList.remove(i); 
             itemsArrayList.add(i,commentListModel); 
             new getDiscussionForumComments(commentListModel.getDiscussionCommentID(),position).execute(); 
            } 


           }else { 
            commentListModel.setVisible(false); 
            itemsArrayList.remove(i); 
            itemsArrayList.add(i,commentListModel); 
           } 
          } 

        } 
       }); 


      viewHolder.Reply.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Configuration.currentDiscussID = itemsArrayList.get(position).getDiscussionCommentID(); 
        fragmentDiscussinTopic.requestEditPop(); 
       } 
      }); 
     } 
     return v1; 
    } 
public static void setListViewHeightBasedOnChildren(ListView listView) 
    { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) 
      return; 

     int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); 
     int totalHeight=0; 
     View view = null; 

     for (int i = 0; i < listAdapter.getCount(); i++) 
     { 
      view = listAdapter.getView(i, view, listView); 

      if (i == 0) 
       view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, 
         ViewGroup.LayoutParams.WRAP_CONTENT)); 

      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 
      totalHeight += view.getMeasuredHeight(); 

     } 

     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount())); 

     listView.setLayoutParams(params); 
     listView.requestLayout(); 

    } 
} 

bitte auf den markierten Bereich der Bildlistenansicht Höhe sehen, ist nicht der eigentliche Inhalt please see the highlighted area of image listview height is not proper to wrap the content

jede Hilfe zu wickeln

+0

minline von textview setzen –

+0

Sie müssen den gesamten Listenansicht-Artikel anzeigen. willst du das? – DKV

+0

@VV ja ich will das nur –

Antwort

1

Das Problem ist, dass Sie die Rolle nicht von beiden Listenansicht zu einem Zeitpunkt, bekommen. wenn du was du beide gleichzeitig machst. Sie müssen eine benutzerdefinierte Listenansicht erstellen, indem Sie etwas Ähnliches anstelle Ihrer Listenansicht (in der Liste) versuchen.

public class NonScrollListView extends ListView { 
    public NonScrollListView(Context context) { 
     super(context); 
    } 

    public NonScrollListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } 
} 
+0

danke Mann du hast es gerettet :) –

+0

gut zu wissen :) – DKV

1

Ihr sind einige Punkte, die Sie beheben müssen appericiated würde zu .

  • eine richtige Höhe so zu Ihrem ListView Gib, dass es innerhalb dieser boundry bewegen kann, wird wrap_content nie für ListView empfohlen. Also besser, um etwas Höhe zu Ihrem ListView zu setzen.

  • Listview innerhalb einer ListView wird nicht die Benutzererfahrung gut geben. Versuchen Sie eine Arbeit oder besser zu verwenden RecyclerView. RecyclerView ist im Falle einer verschachtelten Liste besser in der Leistung.

Hoffe, es hilft :)

+0

kannst du mir bitte irgendein Tutorial vorschlagen, um eine verschachtelte Liste mit RecyclerView zu implementieren –

+0

Ersetzen Sie einfach Ihren ListView durch RecyclerView und aktualisieren Sie den entsprechenden Adapter. – Neo

+0

ok Ich werde das versuchen und auf Ihre Antwort antworten Danke trotzdem –