如果列表视图中的行在android中超过2行,如何添加显示更多

时间:2015-02-11 02:14:14

标签: android listview button row

我是android的新手。我在TextView中有一个简单的ListView作为列表项。如果行有超过3行文本,我必须动态地向列表视图中的行添加一个按钮。

我已尝试在android中使用ViewTreeObserverOnPreDrawListener函数。但是它不起作用,因为只有在渲染了所有列表项之后才调用ViewTreeObserver。因此ViewTreeObserver仅在最后一项上被调用。

请帮帮我!我遇到了麻烦。

这是我写的代码:

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

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.review = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);
        convertView1 = convertView;
    }
    holder = (ViewHolder) convertView.getTag();
    item = reviews.get(position);
    Log.d("position",""+position);
    holder.review.setText(item.getReview());
    mViewTreeObserver = holder.review.getViewTreeObserver();
    mViewTreeObserver.addOnPreDrawListener(onPreDrawListener);

    return convertView;
}

OnPreDrawListener onPreDrawListener = new OnPreDrawListener()
{

    @Override
    public boolean onPreDraw()
    {

        System.out.println("Line Count is : " + holder.review.getLineCount());
        noOfLines = holder.review.getLineCount();
        if (noOfLines > 3)
        {

            int lineEndIndex = holder.review.getLayout().getLineEnd(2);
            String text = holder.review.getText().subSequence(0, lineEndIndex - 3) + "...";
            holder.review.setText(text);
            mReadMoreButton = new Button(context);
            RelativeLayout ll = (RelativeLayout) convertView1.findViewById(R.id.layout);
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.BELOW, holder.review.getId());
            ll.addView(mReadMoreButton, lp);
            mReadMoreButton.setText("Readmore");
            mReadMoreButton.setId(5);
            mReadMoreButton.setOnClickListener(onReadMoreClickListener);
        }


        return true;
    }

1 个答案:

答案 0 :(得分:0)

这里我使用以下代码解决了这个问题。它适用于我。

@覆盖     public View getView(int position,View convertView,ViewGroup parent)     {

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.review = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);
        layouts[position] = convertView;

    }

    holder = (ViewHolder) convertView.getTag();
    item = reviews.get(position);
    length = position;
    Log.e("inside getView",""+holder);
    Log.e("item",""+item.getReview());
    if(flag)
    {   
    holder.review.setText(item.getReview());
    }
    arr[position] = holder.review;
    mViewTreeObserver = arr[position].getViewTreeObserver();
    mViewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener);
    return convertView;
}

OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener()
{

    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout()
    {
        Log.e("inside globallayout","");
        ViewTreeObserver obs = holder.review.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
        for (; index <= length; index++)
        {
            Layout l = arr[index].getLayout();
            if (l != null)
            {
                int lines = l.getLineCount();
                Log.d("lines", "" + lines);
                if (lines > 3)
                {

                    System.out.println("at index" + index + "is arr[index]" + arr[index]);
                    int lineEndIndex = arr[index].getLayout().getLineEnd(2);
                    String text = arr[index].getText().subSequence(0, lineEndIndex - 3) + "...";
                    holder = (ViewHolder) layouts[index].getTag();
                    holder.review.setText(text);
                    holder.mReadMoreButton = new Button(context);
                    RelativeLayout ll = (RelativeLayout) layouts[index].findViewById(R.id.layout);
                    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    lp.addRule(RelativeLayout.BELOW, arr[index].getId());
                    System.out.println("holder.review.getId()" + arr[index].getId());
                    ll.addView(holder.mReadMoreButton, lp);
                    System.out.println("ll " + ll + "lp " + lp);
                    holder.mReadMoreButton.setText("Readmore");
                    holder.mReadMoreButton.setId(j++);
                    holder.mReadMoreButton.setTag(index);
                    holder.mReadMoreButton.setOnClickListener(onReadMoreClickListener);

                }
            }

        }
    }
};

OnClickListener onReadMoreClickListener = new OnClickListener()
{

    @Override
    public void onClick(View v)
    {


        v.setVisibility(View.GONE);
        clickedPosition = (Integer) v.getTag();
        item = reviews.get(clickedPosition);
        //Log.d("item",""+item);
        Log.d("layout[clickedPosition]",""+layouts[clickedPosition]);
        holder = (ViewHolder) layouts[clickedPosition].getTag();
        holder.review.setText(item.getReview());
        holder.mReadLessButton = new Button(context);
        RelativeLayout ll = (RelativeLayout) layouts[clickedPosition].findViewById(R.id.layout);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, v.getId());
        ll.addView(holder.mReadLessButton, lp);
        holder.mReadLessButton.setText("Readless");
        holder.mReadLessButton.setId(k++);
        holder.mReadLessButton.setTag(clickedPosition);
        holder.mReadMoreButton.setVisibility(View.GONE);
        holder.mReadLessButton.setOnClickListener(onReadLessClickListener);



    }
};

OnClickListener onReadLessClickListener = new OnClickListener()
{



    @Override
    public void onClick(final View v)
    {

        v.setVisibility(View.GONE);
        clickedPosition = (Integer) v.getTag();
        Layout l = arr[clickedPosition].getLayout();
        if (l != null)
        {
            int lines = l.getLineCount();
            Log.d("lines", "" + lines);
            if (lines > 3)
            {
                holder = (ViewHolder) layouts[clickedPosition].getTag();
                int lineEndIndex = arr[clickedPosition].getLayout().getLineEnd(2);
                String text = arr[clickedPosition].getText().subSequence(0, lineEndIndex - 3) + "...";
                flag=false;
                holder.review.setText(text);
                holder.mReadMoreButton = new Button(context);
                RelativeLayout ll = (RelativeLayout) layouts[clickedPosition].findViewById(R.id.layout);
                LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.BELOW, holder.review.getId());
                ll.addView(holder.mReadMoreButton, lp);
                holder.mReadMoreButton.setText("Readmore");
                holder.mReadMoreButton.setId(a++);
                holder.mReadMoreButton.setTag(clickedPosition);
                holder.mReadLessButton.setVisibility(View.GONE);
                holder.mReadMoreButton.setOnClickListener(onReadMoreClickListener);

            }
        }

    }
};