扩大在语境中的一个ListView - 机器人 [英] Expanding in Context in a ListView - Android

查看:306
本文介绍了扩大在语境中的一个ListView - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,该行的一个组成部分是一个TextView。默认情况下,我想只显示两行文字的TextView中。但是,如果用户点击了TextView的,我想TextView的上下文扩大。

我确实有这部分的工作,但我想有更多的内容指标:

我目前的执行情况(如下图),有它自己的问题瓦特/没有崩溃,如果列表视图滚动,但我会处理,通过存储在某些集合每个光标记录的值。

我试图用chatBubble.getLineCount(),但返回零开始B / C它不是通过onLayout了(从我的理解)。

我只想显示它,如果有超过2行的内容。

我想,我的解决方案将创建我自己的实现的TextView的,它可以处理我的一些要求,但不知道如果任何人有任何的例子我可以看看......(或其他建议)。

 < RelativeLayout的
        机器人:ID =@ + ID / linear_layout_row_three
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / linear_layout_row_two
        机器人:layout_toRightOf =@ + ID / munzeeQuickContact
        机器人:方向=横向>

        <的TextView
            机器人:ID =@ + ID / chat_bubble
            机器人:可点击=真
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:MAXLINES =2
            机器人:背景=@可绘制/ chat_bubble
            机器人:文本=我去寻找在雨天这种特殊的标记,而我无法找到它,由于天气恶劣,下一次请留下嗯我去寻找在雨天这种特殊munzee,我无法找到它,由于天气恶劣,下一次请留下嗯/>
        < ImageView的
            机器人:ID =@ + ID / minimize_maximize
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignBottom =@ ID / chat_bubble
            机器人:layout_alignRight =@ ID / chat_bubble
            机器人:能见度=水涨船高
            机器人:SRC =@机器人:可绘制/ ic_menu_more/>
    < / RelativeLayout的>
 

下面是一些源我目前拥有的:

 最后的TextView chatBubble =(TextView中)view.getTag(R.id.chat_bubble);
        最后的ViewGroup expandableContainer =(ViewGroup中)view.getTag(R.id.linear_layout_row_three);
        最后ImageView的minimizeMaximize =(ImageView的)view.getTag(R.id.minimize_maximize);
        chatBubble.setOnClickListener(

                新View.OnClickListener(){
                    布尔isExpanded = FALSE;
                    INT lastHeight = 0;
                    //这是自动不断扩大文本视图
                    @覆盖
                    公共无效的onClick(视图v){
                        如果(isExpanded){
                            ViewGroup.LayoutParams PARAMS =(ViewGroup.LayoutParams)expandableContainer
                                    .getLayoutParams();

                            params.height = lastHeight;
                            chatBubble.setMaxLines(2);

                            expandableContainer.setLayoutParams(PARAMS);
                            expandableContainer.invalidate();
                            minimizeMaximize.setVisibility(View.VISIBLE);
                        } 其他 {
                            lastHeight = expandableContainer.getHeight();

                            ViewGroup.LayoutParams PARAMS =(ViewGroup.LayoutParams)expandableContainer
                                    .getLayoutParams();

                            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                            chatBubble.setMaxLines(99);

                            expandableContainer.setLayoutParams(PARAMS);
                            expandableContainer.invalidate();
                            minimizeMaximize.setVisibility(View.VISIBLE);
                        }

                        isExpanded = isExpanded!;

                    }
                });
 

解决方案
  

我想,我的解决方案将创造我自己的实施   TextView中它可以处理我的一些要求,但不能肯定是否   任何人有任何的例子我可以看看。

看一看下面的类:

 公共类LimitedTextView扩展TextView的{

    私人布尔MSTATUS;

    公共LimitedTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        涂料P = getPaint();
        字符串s =的getText()的toString()。
        如果(S =空&安培;!&安培;!s.equals()){
            INT M =(int)的p.measureText(S);
            如果(米&其中; getMeasuredWidth()* 2){
                modifyParent(真正的);
            } 其他 {
                modifyParent(假);
            }
        }
    }

    私人无效modifyParent(布尔如何){
        RelativeLayout的RL =(RelativeLayout的)的getParent();
        rl.findViewById(R.id.minimize_maximize).setVisibility(
                怎么样 ? View.GONE:View.VISIBLE);
        如果(MSTATUS){
            setMaxLines(40); //任意数,设置尽可能高
        } 其他 {
            setMaxLines(2);
        }
    }

    公共无效storeCurrentStatus(布尔状态){
        MSTATUS =状态;
    }

}
 

LimitedTextView 将测量用自己的油漆对象,它的文字和对测量宽度测试。如果在这两个允许的行适合它会隐藏的ImageView ,否则会表现出来。它还存储行(扩/未膨胀的)和增加的当前状态或减小的最大行数,以获得适当的外观。 在 getView 适配器的方法,你会:

  • 设置文本
  • 根据位置从布尔数组设置状态(这也需要保持各行依次为滚动列表):

    textView.storeCurrentStatus(MSTATUS [位置])

  • 设置 OnClickListener LimitedTextView 本身并从那里更新状态:

      MSTATUS [(整数)v.getTag()] = MSTATUS [(整数)v.getTag()]!;
    notifyDataSetChanged();
     

  • 基于同一 MSTATUS 布尔数组你可能会改变的绘制的的ImageView ,以显示不同的一个根据,如果的TextView 展开或不

我手动写的,所以有可能会出现一些错误,我失去了现在,把它作为一种思想。该 LimitedTextView 可作为性能提升,我也不知道它会表现得有多好,如果你想扩大动画的文字。

I have a listView, one component of the row is a TextView. By default, I wish to show only 2 rows of text in the TextView. But if the user taps on the TextView I wanted to the textView to expand in context.

I actually have this portion working, but I want to have a more content indicator :

My current implementation (Below) has it's own issues w/ not collapsing if the list view is scrolled, but I will handle that by storing the values for each cursor record in some collection..

I tried using chatBubble.getLineCount() but that returns zero initially b/c it has not gone through onLayout (from my understanding).

I only wish to show it if there is more than 2 lines of content.

I figure my solution will be creating my own implementation of TextView which can handle some of my requirements, but not sure if anyone has any examples I can look at.. (Or other suggestions).

   <RelativeLayout
        android:id="@+id/linear_layout_row_three"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linear_layout_row_two"
        android:layout_toRightOf="@+id/munzeeQuickContact"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/chat_bubble"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:background="@drawable/chat_bubble"
            android:text="I went looking for this particular token on a rainy day, and I was unable to locate it due to the bad weather, next time please leave an um I went looking for this particular munzee on a rainy day, and I was unable to locate it due to the bad weather, next time please leave an um" />
        <ImageView 
            android:id="@+id/minimize_maximize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/chat_bubble"
            android:layout_alignRight="@id/chat_bubble"
            android:visibility="gone"
            android:src="@android:drawable/ic_menu_more"/>
    </RelativeLayout>

Here is some of the source I currently have :

final TextView chatBubble = (TextView) view.getTag(R.id.chat_bubble);
        final ViewGroup expandableContainer = (ViewGroup) view.getTag(R.id.linear_layout_row_three);
        final ImageView minimizeMaximize = (ImageView) view.getTag(R.id.minimize_maximize);
        chatBubble.setOnClickListener(

                new View.OnClickListener() {
                    boolean isExpanded = false;
                    int lastHeight = 0;
                    // This is for the auto expanding text view
                    @Override
                    public void onClick(View v) {
                        if (isExpanded) {
                            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) expandableContainer
                                    .getLayoutParams();

                            params.height = lastHeight;
                            chatBubble.setMaxLines(2);

                            expandableContainer.setLayoutParams(params);
                            expandableContainer.invalidate();
                            minimizeMaximize.setVisibility(View.VISIBLE);
                        } else {
                            lastHeight = expandableContainer.getHeight();

                            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) expandableContainer
                                    .getLayoutParams();

                            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                            chatBubble.setMaxLines(99);

                            expandableContainer.setLayoutParams(params);
                            expandableContainer.invalidate();
                            minimizeMaximize.setVisibility(View.VISIBLE);
                        }

                        isExpanded = !isExpanded;

                    }
                });

解决方案

I figure my solution will be creating my own implementation of TextView which can handle some of my requirements, but not sure if anyone has any examples I can look at..

Have a look at the class below:

public class LimitedTextView extends TextView {

    private boolean mStatus;

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Paint p = getPaint();
        String s = getText().toString();
        if (s != null && !s.equals("")) {
            int m = (int) p.measureText(s);
            if (m < getMeasuredWidth() * 2) {
                modifyParent(true);
            } else {
                modifyParent(false);
            }
        }
    }

    private void modifyParent(boolean how) {
        RelativeLayout rl = (RelativeLayout) getParent();
        rl.findViewById(R.id.minimize_maximize).setVisibility(
                how ? View.GONE : View.VISIBLE);
        if (mStatus) {
            setMaxLines(40); // arbitrary number, set it as high as possible
        } else {
            setMaxLines(2);
        }
    }

    public void storeCurrentStatus(boolean status) {
        mStatus = status;
    }

}

The LimitedTextView will measure its text using its own Paint object and test it against the measured width. If it fits on the two allowed rows it will hide the ImageView, otherwise it will show it. It also stores the current status of row(expanded/not-expanded) and increases or decreases the maximum number of lines to obtain the proper appearance. In the getView method of the adapter you would:

  • set the text
  • set the status from a boolean array according to a position(this is also required to keep the rows in order as you scroll the list):

    textView.storeCurrentStatus(mStatus[position])

  • set the OnClickListener on the LimitedTextView itself and from there update the status:

    mStatus[(Integer) v.getTag()] = !mStatus[(Integer) v.getTag()];
    notifyDataSetChanged();
    

  • based on the same mStatus boolean array you'll probably change the drawable of the ImageView, to show a different one depending on if the TextView is expanded or not

I manually wrote it, so there could be some mistakes I'm missing right now, take it as an idea. The LimitedTextView could be improved as in performance, I also don't know how well it would behave if you want to animate expanding the text.

这篇关于扩大在语境中的一个ListView - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆