购物车 ListView 包含了EditText的问题,最终解决换RecyclerView来做,哈哈哈哈

               

写这篇文章主要是为了记录EditText,这个EditText的处理真的让人头疼啊。解决一个问题又出现一个问题,我花了3天的时间尽然没有解决,我也是醉了!!!

最终是使用RecyclerView,

先把最终的放在这里:然后写一下ListView 里面的包含EditText的两步处理


要注意的是setTag://注意tag一定要在最上面,就是要在设置监听前面


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 cartDatas = new ArrayList();        for (int i = 0; i < 100; i++) {            cartDatas.add(i);        }        CartAdapter cartAdapter = new CartAdapter(cartDatas,this);        cart.setAdapter(cartAdapter);    }    public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> implements TextView.OnEditorActionListener {        private ArrayList data = new ArrayList();        private Context context;        public CartAdapter(ArrayList data,Context context) {            this.data = data;            this.context = context;        }        @Override        public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {            View ret = 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) {                int 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

"1.0" encoding="utf-8"?>"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">    "@+id/recyclerView"        android:layout_width="match_parent"        android:layout_height="match_parent"        />

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="左边">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="右边"/>LinearLayout>




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

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



记录一下:


ListView 包含了EditText的优化,优化忘了还是有光标的复用的问题,但是总比之前好多了。

0:只要在清单文件中设置android:windowSoftInputMode="adjustPan" 

1:item里面设置android:descendantFocusability="beforeDescendants"

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 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 cartDatas = new ArrayList();        for (int i = 0; i < 100; i++) {            cartDatas.add(i);        }        CartAdapter cartAdapter = new CartAdapter(cartDatas,this);        cart.setAdapter(cartAdapter);    }    //为了避免光标漂的问题,    //并且在item 里面设置android:descendantFocusability="beforeDescendants"    //问题又来了,这个时候第一次点击edittext的时候显示的是软键盘的默认的界面,然后切换到数字的时候光标又没有了    //再点击的时候光标又来了,然后再点击数字的时候才会有。    //也就是说第一次点击的时候没有用,第二次点击的时候才有用    //只要在清单文件中设置android:windowSoftInputMode="adjustPan" 到    //这样设置的时候又有光标票的问题了,    //设置一个当前点击的position初始值为-1,在onTouch 里面设置这个position,然后在onEditorAction 里面设置这个为-1    //在getView 里面,如果是当前的position 那么请求焦点,并且设置设置一些参数,如果不是,那么就取消焦点    //然后再在onTouch的方法里面,设置显示键盘    //这个时候又遇到一个问题:点击的时候键盘显示的是数字的,但是如果滚动的时候将当前的光标超出了屏幕了,会弹出默认的全部的键盘。//    public class CartAdapter extends BaseAdapter implements TextView.OnEditorActionListener, View.OnTouchListener {    public class CartAdapter extends BaseAdapter implements TextView.OnEditorActionListener {        ArrayList datas = new ArrayList();        Context context;        int currentTouchedPosition=-1;        public CartAdapter(ArrayList 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 position = (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", "输入法未激活,开始激活");//                    imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);//                    Log.d("kodulftest", "输入法未激活,已经激活");//                } else {//                    Log.d("kodulftest", "输入法激活了");//                }//            }//            return false;//        }        public class ViewHolder{            private EditText number;            public ViewHolder(View view) {                number = (EditText) view.findViewById(R.id.simple_edittext);            }        }    }}

清单文件:

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="左边">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="右边"/>LinearLayout>





           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

你可能感兴趣的:(购物车 ListView 包含了EditText的问题,最终解决换RecyclerView来做,哈哈哈哈)