使用Android维护适当的活动范围

时间:2014-06-04 18:47:31

标签: android android-listview android-gesture

我在组织我的Java类时遇到了麻烦,因此我需要在任何给定对象中访问的所有内容的范围都可用。

例如,我有一个MainActivity,它有一个ListView,我想附加一个onFling手势监听器,以便在从屏幕上刷掉它们时删除它们。但是,我无法弄清楚如何设置监听器,以便监听器可以访问我的ArrayAdapter和ListView,并且MainActivity可以访问它以在onCreate()上实例化它。

设置此方法的正确方法是什么?

public class MainActivity extends FragmentActivity implements onSelectionMade {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    private static View current;
    private GestureDetectorCompat mDetector;

    @Override
    public void doChange(int val){
        if(val == onSelectionMade.OK){
            // Add contact
            current.setBackgroundColor(Color.rgb(60, 242, 102));

        }else if(val == onSelectionMade.CANCEL){
            current.setBackgroundColor(Color.rgb(237, 47, 47));
        }
    }

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

            // THIS IS THE LINE WITH THE ERROR BECAUSE MyGestureListener IS NOT IN THE                         SCOPE OF THE MAIN ACTIVITY
        this.mDetector = new GestureDetectorCompat(this, new MyGestureListener());


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        ContactAdapter adapter;
        ListView view;

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            Contact contactList[] = new Contact[] 
            {
                    new Contact("Matt", "Hintzke", "2062259311", "matt.hintzke@email.wsu.edu"),
                    new Contact("John", "Smith", "2062236457", "john@email.com"),
                    new Contact("Bob", "Harrison", "2064318756", "bob@email.com"),
                    new Contact("Colleen", "Arnold", "2065553696", "ween@email.com"),
                    new Contact("Harry", "Larkin", "3608785423", "poop@gmail.com")
            };

            adapter = new ContactAdapter(getActivity(), R.layout.contact_item , contactList);   

            view = (ListView) rootView.findViewById(R.id.listView);
            view.setAdapter(adapter);

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

                    Contact item = (Contact) parentAdapter.getItemAtPosition(position);
                    current = view;
                    //adapter.remove(adapter.getItem(position));

                    DialogFragment dialog = AddContactDialog.newInstance(item);

                    dialog.show(getActivity().getSupportFragmentManager(), "Add Contact Dialog");
                    //Toast.makeText(getActivity().getApplicationContext(), item.first, Toast.LENGTH_SHORT).show();

                }
            });


            return rootView;
        }


        class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
            private static final String DEBUG_TAG = "Gestures"; 


            @Override
            public boolean onFling(MotionEvent event1, MotionEvent event2, 
                    float velocityX, float velocityY) {
                if(event2.getX() - event1.getX() > 10){
                    int id = view.pointToPosition((int) event1.getX(), (int) event1.getY());
                    adapter.remove(adapter.getItem(id));
                }
                return true;
            }
        }

    }







}

1 个答案:

答案 0 :(得分:1)

我认为,“正确”的方式是将对象声明置于需要它们的“最小”范围内。

例如,在这种情况下,ListView及其GestureDetector都应位于Fragment内。 Activity不应该关注它们(例如片段应该是可替换的 - 如果你用另一个没有有一个ListView的片段替换Fragment,为什么你需要GestureDetector?)

同样适用于OnSelectionMade界面的实现者,&amp; c。

此外,片段不需要是Activity的内部类。我不确定为什么IDE会像这样创建它们,这会导致混淆:/