简体   繁体   中英

editable EditText in clickable listView

I want to make a clickable listView with editable editText.
I have a custom list adapter.java, custom list item.xml, mainActivity.java.

I tried
1. android:descendantFocusability="blocksDescendants"
=> failed. can't edit editText.
2. editText android:focusable/enable/clickable = true
=> failed. can't click listView Item
3. getView{editText.onClickListener}
=>failed.

I want to EDITABLE editText, not just clickable editText & Clickable listView(listItem). please help.

customitem.XML

...

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:descendantFocusability="blocksDescendants"
    >

    //android:descendantFocusability="blocksDescendants" doesn't work

    <EditText 
        android:id="@+id/tvItem"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:layout_marginLeft="25dp"
        android:textStyle="bold"
        android:background="@null"
        android:shadowColor="@color/shadow"
        android:shadowDx="3"
        android:shadowDy="3"
        android:shadowRadius="1"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:focusable="true"
        />
    <TextView
        android:id="@+id/tvItemCount"
        android:layout_width="@dimen/list_height"
        android:layout_height="70dp"
        android:layout_alignParentRight="true"
        android:textColor="#ffffff"
        android:textSize="22dp"
        android:text="6"
        android:background="#3300b7ff"
        android:gravity="center_vertical|center_horizontal"
        android:shadowColor="@color/shadow"
        android:shadowDx="3"
        android:shadowDy="3"
        android:shadowRadius="1"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/dark_shadow" >

    </LinearLayout>
</RelativeLayout>

...

After few attempts which I've made by myself, I've asked uncle Google about your problem. Do you know, what he said?

ListView aren't able to handle EditText view well. If many people couldn't resolve this issue before, maybe you will consider some 'workaround' like this desribed HERE . It is an answer to other issue, but probably even if you will fix above problem, you will meet this one.

In brief @Andrew recommends to use ScrollLayout with simple LinearLayout inside instead of ListView. In onCreate method he inflates the View used for list items and add it to LinearLayout, and store this in ArrayList as well, to save data to every view later.

I know it isn't solution for your problem, but maybe it let you a lot of time, which you will spend looking for any reasonable solution.

Edit

It is funny. Inspired by @Rishabh Srivastava link I've tried to find some solution (I know, I'm a little bit stubborn).

I've create adapter layout - RelativeLayout, which is fully filled by Button and above it (I mean literally above it - in Z axis) I've placed EditText view. I thought that edittext will handle click on it and button will handle clicks outside of edittext view. Unfortunately 'click' event propagate through all of views - so by clicking on edittext, we will click on button as well.

I thought I am smarter than everybody so I used OnTouchListener - we can handle single 'touch' event and return true value, as information to OS that we handle it.

And you know what? I've met problem exactly the same like desribed in above link:

When I click on an EditText, the virtual keyboard shows itself, but the EditText loses focus and I have to click the EditText again.

I hope you don't want lost your time any more;)

first of all, thank you everyone!

I tried all of the answer, but it didn't work...:(
Focusable EditText in the ListView and onItemClick
it works for me

my code ▼

public class subMyListAdapter extends BaseAdapter{
Context context;
LayoutInflater Inflater;
ArrayList<subMyItem> arraySrc;
int layout;
static int currentTheme = 0;
EditText tvItem;
RelativeLayout rl_inflate;
UserHolder holder;
public subMyListAdapter(Context context, int layout, ArrayList<subMyItem> arraySrc)
{
    this.context = context;
    this.layout = layout;
    this.arraySrc = arraySrc;
    Inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
    return arraySrc.size();
}
public String getItem(int position)
{
    return arraySrc.get(position).list;
}
public long getItemId(int position)
{
    return position;
}
public View getView(final int position, View conv, ViewGroup parent)
{
    holder = null;
     if (conv == null) 
     {
       LayoutInflater inflater = ((Activity) context).getLayoutInflater();
       conv = inflater.inflate(layout, parent, false);
       holder = new UserHolder();
       holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
       conv.setTag(holder);
     }
     else 
     {
       holder = (UserHolder) conv.getTag();
     }
     if(holder == null)
     {
       holder = new UserHolder();
       holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
       conv.setTag(holder);
     }
     subMyItem user = arraySrc.get(position);
     holder.tvItem.setOnTouchListener(test);
     conv.setOnTouchListener(test);
    if(conv == null)
    {
        conv = conv;
    }
    tvItem = (EditText) conv.findViewById(R.id.tvItem);
    user = arraySrc.get(position);
    tvItem.setText(user.list);
    tvItem.setOnClickListener(new View.OnClickListener() 
     {
       @Override
       public void onClick(View v) 
       {
           Toast.makeText(context, "tvItem button Clicked",
           Toast.LENGTH_LONG).show();
       }
    });
    return conv;
}
View.OnTouchListener test=  new View.OnTouchListener() 
{
    @Override
    public boolean onTouch(View view, MotionEvent event) 
    {
        if (view instanceof EditText) 
        {
            EditText editText = (EditText) view;
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
        } else 
        {
            UserHolder holder = (UserHolder) view.getTag();
            holder.tvItem.setFocusable(false);
            holder.tvItem.setFocusableInTouchMode(false);
        }
    return false;
    }
};
 static class UserHolder 
 {
      EditText tvItem;
     }
 }

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