Shopping Cart ListView contains edittext problem, Final Solution change Recyclerview to do, haha haha

Source: Internet
Author: User
Tags gettext

Write this article is mainly to record edittext, this edittext treatment really makes people headache ah. Solve a problem and there is a problem, I spent 3 days altogether no solution, I was drunk ...

The end is to use Recyclerview,

Put the final one here: then write the two-step process containing edittext in the ListView.


Note that the settag://note that the tag must be at the top, that is, in front of the setup listener


Mainactivity:

Package Tech.androidstudio.simplecartdemorecyclerview;
Import Android.content.Context;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.EditText;

Import Android.widget.TextView;

Import java.util.ArrayList; public class Mainactivity extends appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate
        ) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_main);
        Recyclerview cart = (recyclerview) Findviewbyid (R.id.recyclerview);
        Cart.setlayoutmanager (New Linearlayoutmanager (this));
        arraylist<integer> Cartdatas = new arraylist<integer> (); for (int i = 0; i < i++) {CartdataS.add (i);
        } cartadapter Cartadapter = new Cartadapter (cartdatas,this);

    Cart.setadapter (Cartadapter); } public class Cartadapter extends recyclerview.adapter<cartadapter.cartviewholder> implements Textview.onedito
        Ractionlistener {private arraylist<integer> data = new arraylist<integer> ();
        Private context context;
            Public Cartadapter (arraylist<integer> data,context Context) {this.data = data;
        This.context = context; } @Override Public Cartviewholder oncreateviewholder (viewgroup parent, int viewtype) {View R
            ET = Layoutinflater.from (context). Inflate (R.layout.item_simple,parent,false);
        return new Cartviewholder (ret); } @Override public void Onbindviewholder (cartviewholder holder, int position) {Holder.number
            . Settag (position);
  Holder.number.setText (string.valueof (data.get (position)));          Holder.number.setOnEditorActionListener (this);
        LOG.D ("Kodulf", "recyclerviewadapter position" +position);
        } @Override public int getitemcount () {return data.size (); } @Override public boolean oneditoraction (TextView V, Int. ActionId, KeyEvent event) {in
                T position = (int) v.gettag ();
                int Updatedinteger=integer.parseint (V.gettext (). toString ());
                Data.set (Position,updatedinteger);
            Notifydatasetchanged ();

        return false;
            } public class Cartviewholder extends recyclerview.viewholder{private EditText number;
                Public Cartviewholder (View Itemview) {super (Itemview);
            Number = (EditText) Itemview.findviewbyid (R.id.simple_edittext);
 }
        }
    }

}

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout
xmlns:android= "http://schemas.android.com/apk /res/android "
    xmlns:tools=" Http://schemas.android.com/tools "
    android:layout_width=" Match_parent
    " android:layout_height= "Match_parent"
    android:paddingbottom= "@dimen/activity_vertical_margin"
    android: paddingleft= "@dimen/activity_horizontal_margin"
    android:paddingright= "@dimen/activity_horizontal_margin"
    android:paddingtop= "@dimen/activity_vertical_margin"
    tools:context= " Tech.androidstudio.simplecartdemorecyclerview.MainActivity ">

    <android.support.v7.widget.recyclerview
        android:id= "@+id/recyclerview"
        android:layout_width= "match_parent"
        android:layout_height= " Match_parent "
        />

</RelativeLayout>

Item_simple.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
    android:orientation=" Horizontal "android:layout_width=" match_parent "
    android:layout_height = "Wrap_content"
    android:descendantfocusability= "beforedescendants" >
    <textview
        android:layout _width= "Wrap_content"
        android:layout_height= "wrap_content"
        android:textsize= "35sp"
        android:text= " Left "></TextView>
<edittext
    android:inputtype=" number "
    android:digits=" 0123456789 "
    android:singleline= "true"
    android:layout_width= "158DP"
    android:layout_height= "50DP"
    android:imeoptions= "Actiondone"
    android:id= "@+id/simple_edittext"
    />
    <textview
        Android:layout_width= "Wrap_content"
        android:layout_height= "wrap_content"
        android:textsize= "35SP"
        android:text= "right"/>
</LinearLayout>




+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++

Record it:


The ListView contains the optimization of the edittext, and the optimization forgets the question of the reuse of the cursor, but it is always much better than before.

0: Just set android:windowsoftinputmode= "Adjustpan" in the manifest file

1: Set android:descendantfocusability= "Beforedescendants" in item

Mainactivity

Package Tech.androidstudio.simplecartdemo;
Import Android.content.Context;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.text.InputType;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.inputmethod.EditorInfo;
Import Android.view.inputmethod.InputMethodManager;
Import Android.widget.AdapterView;
Import Android.widget.BaseAdapter;
Import Android.widget.EditText;
Import Android.widget.ImageButton;
Import Android.widget.ListView;

Import Android.widget.TextView;

Import Com.facebook.drawee.backends.pipeline.Fresco;

Import java.util.ArrayList;
    public class Mainactivity extends appcompatactivity{private ListView mcart;
    Private arraylist<string> Mitems;
    Private Cartadapter Madapter;

    Private ListView cart; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_main);
        Cart = (ListView) Findviewbyid (R.id.cart);
        arraylist<integer> Cartdatas = new arraylist<integer> ();
        for (int i = 0; i < i++) {cartdatas.add (i);
        } cartadapter Cartadapter = new Cartadapter (cartdatas,this);

    Cart.setadapter (Cartadapter); }//In order to avoid the problem of cursor drift,//And in the item set android:descendantfocusability= "Beforedescendants"//problem again, this time when the first time to click EditText
    Display the default interface of the soft keyboard, and then switch to the number when the cursor does not have//and then click the cursor again, and then click on the number will be there. That is, when the first click is not used, the second click is useful//as long as in the manifest file set android:windowsoftinputmode= "Adjustpan" to <activity android:name= ".
    Mainactivity "//This time there is a problem with the cursor ticket,//set A current click of the position initial value is-1, set the position in Ontouch, and then set this in Oneditoraction to 1 In GetView, if it is the current position then the request focus, and set some parameters, if not, then the focus//and then in the Ontouch method, set the display keyboard//This time again encountered a problem: Click on the keyboard display is the number But if the current cursor is out of the screen when scrolling, it will pop up the defaultKeyboard.
    public class Cartadapter extends Baseadapter implements Textview.oneditoractionlistener, View.ontouchlistener {  public class Cartadapter extends Baseadapter implements Textview.oneditoractionlistener {arraylist<integer>
        Datas = new arraylist<integer> ();
        Context context;
        int currenttouchedposition=-1;
            Public Cartadapter (arraylist<integer> datas, Context context) {This.datas = datas;
        This.context = context;
        } @Override public int getcount () {return datas.size ();
        } @Override public Object getItem (int position) {return datas.get (position);
        } @Override public long getitemid (int position) {return position; 
            } @Override public view getView (int position, view Convertview, ViewGroup parent) {VIEW ret; if (convertview==null) {ret=lAyoutinflater.from (context). Inflate (R.layout.item_simple,parent,false);
            }else {ret = Convertview;
            } viewholder tag = (viewholder) ret.gettag ();
            if (tag==null) {tag=new viewholder (ret);
            } tag.number.setText (String.valueof (datas.get (position)));
            Tag.number.setTag (position);
Tag.number.setOnEditorActionListener (this);
Tag.number.setOnTouchListener (this); if (currenttouchedposition!=-1&¤ttouchedposition==position) {//Tag.number.requestFocus
();
Tag.number.setCursorVisible (TRUE);
Tag.number.setInputType (Inputtype.type_class_number);
Tag.number.setFocusable (TRUE);
Tag.number.setImeOptions (Editorinfo.ime_action_done);
Tag.number.setFocusableInTouchMode (TRUE); Tag.number.setSelection (Tag.number.getText (). Length ());
            //}else{//Tag.number.clearFocus ();//} Ret.settag (tag);
        return ret; } @Override public boolean oneditoraction (TextView v, int ActionId, keyevent event) {int pos
            ition = (int) v.gettag ();
            LOG.D ("Kodulftest", "Editor action eeeeeeeeee position=" +position);
            int Updatedinteger=integer.parseint (V.gettext (). toString ());
Datas.set (Position,updatedinteger);
            Currenttouchedposition=-1;
            Notifydatasetchanged ();
        return false; }////@Override//public boolean OnTouch (View V, motionevent event) {//if (event.getaction () = = keyevent.action_up) {//int position = (int) v.gettag ();//LOG.D ("Kodulftest", "OnTouch
TTTTTT position= "+ position);
Currenttouchedposition = position;
Notifydatasetchanged ();              //  Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service); if (!imm.isactive ()) {//LOG.D ("kodulftest", "Input method not activated, start activation");//I
Mm.showsoftinput (V, inputmethodmanager.show_forced);
LOG.D ("Kodulftest", "Input method not activated, activated");            } else {//LOG.D ("kodulftest", "Input Method activated");//}//}//
return false;

            } public class viewholder{private EditText number;
            Public Viewholder (view view) {Number = (EditText) View.findviewbyid (R.id.simple_edittext);
 }
        }
    }

}

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest
xmlns:android= "http://schemas.android.com/apk/res/ Android "
    package=" Tech.androidstudio.simplecartdemo ">

    <application
        android:allowbackup=" true "
        android:icon=" @mipmap/ic_launcher "
        android:label=" @string/app_name "
        android:supportsrtl=" true "
        android:theme= "@style/apptheme" >
        <activity android:name= ". Mainactivity "
            android:windowsoftinputmode=" Adjustpan "
            >
            <intent-filter>
                <action Android:name= "Android.intent.action.MAIN"/>

                <category android:name= "Android.intent.category.LAUNCHER "/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout
xmlns:android= "http://schemas.android.com/apk /res/android "
    xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent
    " android:layout_height= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin"
    android: paddingright= "@dimen/activity_horizontal_margin"
    android:paddingtop= "@dimen/activity_vertical_margin"
    android:paddingbottom= "@dimen/activity_vertical_margin" tools:context= ". Mainactivity ">

    <listview android:id=" @+id/cart "android:layout_width=" wrap_content "
        android:layout _height= "Wrap_content"/>

</RelativeLayout>

Item_simple.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
    android:orientation=" Horizontal "android:layout_width=" match_parent "
    android:layout_height = "Match_parent"
    android:descendantfocusability= "beforedescendants" >
    <textview
        android:layout _width= "Wrap_content"
        android:layout_height= "wrap_content"
        android:textsize= "35sp"
        android:text= " Left "></TextView>
<edittext
    android:inputtype=" number "
    android:digits=" 0123456789 "
    android:singleline= "true"
    android:layout_width= "158DP"
    android:layout_height= "50DP"
    android:imeoptions= "Actiondone"
    android:id= "@+id/simple_edittext"
    />
    <textview
        Android:layout_width= "Wrap_content"
        android:layout_height= "wrap_content"
        android:textsize= "35SP"
        android:text= "right"/>
</LinearLayout>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.