为什么我的LayoutInflater在OnFling中不起作用?

时间:2014-06-28 22:05:08

标签: android layout-inflater onfling

我正在尝试这样做,以便当我在listview项目上从左向右滑动时,它将从我的Customadapter中的textview中减去一个。但是,我无法使LayoutInflater工作,所以我可以访问我需要的TextView。我能够在我的onItemClick中完成它,但我无法继续工作。我向它添加了一个Toast通知,以确保它正常工作(确实如此),但是我无法让这段代码工作。

MainActivity

public class MainActivity extends Activity {


    //identify the elemts we are using
    private ArrayAdapter aa;
    private ArrayList<String> list = new ArrayList<String>();
    private ArrayList<Number> amount = new ArrayList<Number>();
    private ListView lv;
    private EditText et;
    private Button eom;
    private static final int SWIPE_MIN_DISTANCE = 75;
    private static final int SWIPE_THRESHOLD_VELOCITY = 50;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    GestureDetector gt;
    View.OnTouchListener gl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //assign values to the elements we are using
        lv = (ListView) findViewById(R.id.OneMoreList);
        et = (EditText) findViewById(R.id.edit_om);
        eom = (Button) findViewById(R.id.add_button);
        gt = new GestureDetector(this, new MyGestureDetector());


        //onclick listener for the button to create a new OneMore
        eom.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String s = et.getText().toString();
                String skree = "One More " + s;    
                aa.add(skree);
                //notification of the data being sent, wipes the text, and closes the keyboard.
                et.setText("");
                hideSoftKeyboard();
            }
        });

        //defines the adapter we are using and sets it.
        aa = new OneMoreArrayAdapter(MainActivity.this, list);
        lv.setAdapter(aa);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                TextView tv2 = (TextView) view.findViewById(R.id.layout_count);
                String value = tv2.getText().toString();
                int n = Integer.valueOf(value);
                int nz = n + 1;
                String skr = Integer.toString(nz);
                tv2.setText(skr);
            }
        });

        lv.setOnTouchListener(new AdapterView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gt.onTouchEvent(event);
            }
        });


    }

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            int pos = lv.pointToPosition((int)e1.getX(), (int)e1.getY());
            LayoutInflater inflator=MainActivity.this.getLayoutInflater();
            final String[] listarray = new String[list.size()];
            list.toArray(listarray);



            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //deletes
                    aa.remove(listarray[pos]);
                    aa.notifyDataSetChanged();
                }
                //left to right
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){


                    final View view = inflator.inflate(R.layout.one_more_list, null);
                    TextView tv2 = (TextView) view.findViewById(R.id.layout_count);
                    String value = tv2.getText().toString();
                    int n = Integer.valueOf(value);
                    int nz = n - 1;
                    String skr = Integer.toString(nz);
                    tv2.setText(skr);


                    //test to see if swipe works
                    Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }
}

适配器

class OneMoreArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private ArrayList<String> list;
    private int[] colors = new int[]{R.color.Goldenrod, R.color.Crimson, R.color.ForestGreen};

    public OneMoreArrayAdapter(Context context, ArrayList<String> list) {
        super(context, R.layout.one_more_list, list);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.one_more_list, parent, false);

        TextView tvtext = (TextView) view.findViewById(R.id.layout_text);
        TextView tvcount = (TextView) view.findViewById(R.id.layout_count);

        int colorPos = position % colors.length;
        view.setBackgroundResource(colors[colorPos]);

        final String[] listarray = new String[list.size()];
        list.toArray(listarray);


        tvtext.setText(listarray[position]);
        Number n = 0;
        String ns = n.toString();
        tvcount.setText(ns);
        return view;
    }
}

我真的很想知道为什么我无法访问OnFling中的TextView,我会感谢任何朝着正确方向的点头。

0 个答案:

没有答案