简体   繁体   中英

list view inside list View in android

I am Using a list view inside a listview and both listview share same list item. It is working fine but if height of the item increases in inside list view it doesn't wrap the content and put a scrollview which is not as per requirement. Please see the codes below

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

    }
}

please see the highlighted area of image listview height is not proper to wrap the content 请查看图像列表突出显示的区域视图高度不适合包装内容

any help would be appericiated

Their are some points you need to fix.

  • Give a proper height to your ListView so that it can scroll within that boundry, wrap_content is never recommended for ListView . So better to put some height to your ListView .

  • Listview inside a ListView will not give the user experience good. Try a work around or better to Use RecyclerView . RecyclerView is better in performance in case of nested list.

Hope it will help :)

The problem is you can't get the scroll of both listview at a time. if you what you both at the same time. you need to create a custom list view try something like this instead of your list view (inside list).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM