简体   繁体   English

更改子LinearLayout可见性“onClick”内部LinearLayout内的TextView

[英]Change child LinearLayout visibility “onClick” ing a TextView inside parent LinearLayout

I am trying to toggle the visibility of a child LinearLayout with an onClickListener for a TextView present in a parent LinearLayout. 我正在尝试使用onClickListener为子LinearLayout中存在的TextView切换子LinearLayout的可见性。 Can someone help me achieve this? 有人可以帮我实现吗?

The following is a part of my layout file: 以下是我的布局文件的一部分:

<LinearLayout android:id="@+id/main_space"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="10"
    android:padding="2dp">
    <TextView android:id="@+id/currentUserHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/currentuserheader_textsize"
        android:textColor="@color/currentuserheader_textcolor"/>
    <TextView android:id="@+id/currentUserBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/currentuserbody_textsize"
        android:textColor="@color/currentuserbody_textcolor"
        android:clickable="true"
        android:onClick="toggleUserActionBar"/>
    <LinearLayout android:id="@+id/useractions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="gone"
        android:background="@color/useractions_background">
        <TextView android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Save"
            android:textColor="@color/useractions_label"
            android:textSize="@dimen/useractions_textsize"
            android:layout_weight="1"
            android:gravity="center"/>
        <TextView android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Delete"
            android:textColor="@color/useractions_label"
            android:textSize="@dimen/useractions_textsize"
            android:layout_weight="1"
            android:gravity="center"/>
        <TextView android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Forward"
            android:textColor="@color/useractions_label"
            android:textSize="@dimen/useractions_textsize"
            android:layout_weight="1"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

When I click on the TextView "currentUserBody", I want to toggle the visibility of the LinearLayout "useractions" which is right under that text view. 当我单击TextView“currentUserBody”时,我想切换正好在该文本视图下的LinearLayout“useractions”的可见性。

This layout is actually a layout for a row inside a ListView. 此布局实际上是ListView中行的布局。 So I want this functionality to work for every row in that ListView. 所以我希望这个功能适用于该ListView中的每一行。 The user can simply tap the text and it should open up options just under the text for performing those actions quickly. 用户只需点击文本即可在文本下方打开选项,以便快速执行这些操作。

Can you help me achieve this? 你能帮助我实现这个目标吗?

PS: I have created a custom ArrayAdapter to populate the in the ListView. PS:我创建了一个自定义ArrayAdapter来填充ListView。

A quick and dirty solution could be: 快速而肮脏的解决方案可能是:

public void toggleUserActionBar(View v) {
    View actions = v.getParent().findViewById(R.id.useractions);
    if(actions.getVisibility() == View.VISIBLE) {
         actions.setVisibility(View.GONE);
    } else {
         actions.setVisibility(View.VISIBLE);
    }
}

A cleaner solution would be inflating the item layout in a custom view, that can handle all this inside itself. 一个更清洁的解决方案是在自定义视图中膨胀项目布局,它可以自己处理所有这些内容。

Here is an example of a getView method that does what you want: 以下是执行所需操作的getView方法示例:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // do the normal thing you would do, inflate stuff, use the view holder pattern etc 
    // set as the tag for the clickable TextView the current position
    ((TextView) convertView.findViewById(R.id.currentUserBody)).setTag(Integer.valueOf(position));
    // set the listener for the TextView
    ((TextView) convertView.findViewById(R.id.currentUserBody)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // retrieve the position value
            Integer realPos = (Integer) v.getTag();
            // get the parent and then search for the useractions LinearLayout
            View hiddingLayout = ((LinearLayout) v.getParent()).findViewById(R.id.useractions);
            // modify a status holder with the new value
            status[realPos] = !status[realPos];
            // change the visibility 
            hiddingLayout.setVisibility(status[realPos] ? View.VISIBLE : View.GONE);
        }
    });
    // this is required so we don't mess up the rows with visible/gone layouts 
    convertView.findViewById(R.id.useractions).setVisibility(status[position] ? View.VISIBLE : View.GONE);
    return convertView;
}

The status array is a boolean array which will hold the visibility status for each row(a false value means GONE , VISIBLE otherwise). status数组是一个boolean数组,它将保存每行的可见性状态( false值表示GONE ,否则为VISIBLE )。 You would create this array in the constructor based on the size of your data. 您可以根据数据大小在构造函数中创建此数组。 If you plan to call notifyDataSetChanged on your adapter you would need to reset the array. 如果您计划在适配器上调用notifyDataSetChanged ,则需要重置阵列。

Edit: 编辑:

Another option is to break the current layout file in two and use an ExpandableListView where the hidden layout is a child view. 另一种选择是将当前布局文件分成两部分并使用ExpandableListView ,其中隐藏布局是子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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