2016-08-01 2 views
0

私はリストビュー内でリストビューを使用していますが、両方のリストビューは同じリストアイテムを共有しています。 うまくいきましたが、内側のリストビューでアイテムの高さが上がった場合は、コンテンツをラップせず、要件に従わないscrollviewを配置します。 リストビュー内のリストアンドロイドで表示

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(); 

    } 
} 
以下のコードを参照してください。

画像のリストビューの高さの強調表示された領域は、任意のヘルプは

+0

textviewの小文字を設定する –

+0

リストビューアイテム全体を表示する必要があります。あなたが望むのは? – DKV

+0

@VVはい私はそれが欲しいだけ –

答えて

1

問題は、一度に両方のリストビューのスクロールを得ることができないです。もしあなたが同じ時にあなたの両方であれば。カスタムリストビューを作成する必要があります(リスト内のリストビューではなく)。

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

あなたがそれを保存した感謝の男:) –

+0

おはよう:) – DKV

1

あなたが修正する必要があるいくつかのポイントが彼らのですがappericiatedことになるコンテンツ please see the highlighted area of image listview height is not proper to wrap the content

をラップするために適切ではありません参照してください。 。

  • それがそのboundry内をスクロールすることができるようwrap_contentListViewために推奨されることはありません、あなたのListViewに適切な高さを与えます。だからあなたのListViewにいくつかの高さを置くことをお勧めします。

  • ListviewListViewのユーザーエクスペリエンスは得られません。回避策を試してください。RecyclerViewを使用してください。 RecyclerViewは、ネストされたリストの場合にパフォーマンスが優れています。

はそれに役立つことを願っています:)

+0

私はRecyclerViewを使用してネストされたリストを実装するチュートリアルをお勧めします –

+0

リサイクルビューでListViewを置き換え、対応するアダプタを更新してください。 – Neo

+0

ok私はそれを試み、あなたの答えに応答しますとにかく –

関連する問題