Replacing ListView row with another layout onClick

前端 未结 2 1301
温柔的废话
温柔的废话 2021-01-06 10:35

I have a ListView with only TextView. I want an implementation in which if I click on a ListView row, an edittext with a replace button should appear and whatever I type in

相关标签:
2条回答
  • 2021-01-06 11:11

    You can achieve this in two ways :-

    1. First is in listview's item layout add EditText and a Button and hide them. Now set onItemClickListener of listview in which hide textview and show editext and replace button.

    2. Second is create a new layout with edit text and a button and set onItemClickListener for listview and when clicked on row you can easily inflate that layout into your listview's item layout.

    Second Solution code:-

    inflate.xml

    <EditText
                android:id="@+id/enter_txt"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
    />
    
    <Button
                android:id="@+id/btn_replace"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
    />
    

    *list_view_item.xml*

    <TextView
                android:id="@+id/my_txt"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="ABCD"
    />
    
    <RelativeLayout
                android:id="@+id/rl_inflate"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
    />
    

    listview onItemClickListener

            listView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
    
                               TextView txt_view = (TextView)view.findViewById(R.id.my_txt);
    
                               txt_view.setVisibility(View.GONE);
    
                           RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate);
                               View child = getLayoutInflater().inflate(R.layout.inflate);
                               rl_inflate.addView(child);
    
    
                              Button my_btn = (Button)child.findViewById(R.id.btn_replace);
                              EditText enter_txt = (EditText)child.findViewById(R.id.enter_txt);
    
                              my_btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                                     txt_view.setText(enter_txt.getText().toString());
                                     txt_view.setVisibility(View.VISIBLE);
                }
                });
            }
        });
    
    0 讨论(0)
  • 2021-01-06 11:20

    You can go with either of two solutions below :

    1. Include an edittext and replace button along with the textview layout and that should be in hidden state. While clicking on the list item change the visiblity of edit text and button to visible and change that of textview to Gone. While clicking on the replace button, reverse the visibility change.

    2. While clicking on the list item, show an alert box with a edittext and replace button. After changing the content and clicking on the replace button, change the textview content to changed value.

    0 讨论(0)
提交回复
热议问题