6

这个话题有很多讨论。但它们都不适合我。我实现了一个自定义的 ListView,其中包含一个ImageViewListView并且ListView是从ListFragment. 我的 ImageView 是可点击的,因此当用户单击 ImageView 时,会激活 Camera 活动,并且 ImageView 会使用捕获的图像进行更新。XML如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_background_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:clickable="true"
            android:focusable="false"
            android:contentDescription="@string/image_view_description"
            android:layout_height="50dip"
            android:src="@drawable/icons_preview"
            />

    </LinearLayout>  

</RelativeLayout>

现在我需要区分用户的单击是针对列表项还是针对 ImageView。我设置onListItemClick它可以捕获用户单击 ListView 中的列表项,但是当我单击 ImageView 时,它不起作用。如何检测用户何时单击 ImageView。我的 ListFragment 类如下所示。

public class MyListFragment extends ListFragment {
    private customList_Adaptor adapter;
    private static final int CAMERA_REQUEST = 0;
    private static final int RESULT_OK = 0;
    private static MyListFragmentInterface mCallback = null;
    static Context thisListFragmentContext = null;
    private String[] mListItems;
    private TrackerDBAdapter dbHelper;
    private List<String> listIDs;   
    public MyListFragment() {   

    }

    public MyListFragment(MyListFragmentInterface callback) {
        mCallback = callback;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.my_list_fragment, container, false); 
        dbHelper = new TrackerDBAdapter(getActivity());
        dbHelper.open();
        Cursor cursor = dbHelper.fetchAllTrackerInTheList();
        mListItems = new String[dbHelper.getLastInsertedID()];
        thisListFragmentContext = getActivity();
        int i = 0;
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            mListItems[i++] = cursor.getString(1);

        dbHelper.close();
        listIDs = new ArrayList<String>(); 
        listIDs = Arrays.asList(mListItems);
        adapter = new customList_Adaptor(thisListFragmentContext, R.layout.list_row, listIDs);
        ListView lv = (ListView) view.findViewById(android.R.id.list);      
        lv.setAdapter(adapter);     



        return view;
    }

    public void addNewTrackerintoListDisplay(TrackerInfo newTracker){
        mListItems = append(mListItems, newTracker.getIdnumber());      
        setListAdapter(new ArrayAdapter<String>(getActivity(), 
                android.R.layout.simple_list_item_1,
            mListItems));
    }

    @SuppressLint("NewApi")
    private String[] append(String[] mListItems2, String idnumber) {
        // TODO Auto-generated method stub
        final int N = mListItems2.length;
        mListItems2 = Arrays.copyOf(mListItems2, N + 1);
        mListItems2[N] = idnumber;
        return mListItems2;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        mCallback.itemSelected(mListItems[position]);
    }


}

我的适配器类如下。

public class customList_Adaptor extends ArrayAdapter<String>{


    protected static final int CAMERA_REQUEST = 0;

    private Context context;

    private List<String> listIDs = new ArrayList<String>();

    public customList_Adaptor(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        this.setContext(context);
        this.listIDs = objects;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.size();
        else
            return 0;
    }

    @Override
    public String getItem(int arg0) {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.get(arg0);
        else
            return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){
            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
            vi.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) vi.getTag();


//      viewHolder.thumbnailImage.setOnClickListener(new OnClickListener(){
//          
//          @Override
//          public void onClick(View arg0) {
//              // TODO Auto-generated method stub
//              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
//              ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
//          }
//          
//      }); 

        // Setting all values in listview
        viewHolder.id.setText(listIDs.get(position));

        return vi;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


}
4

4 回答 4

1

在您的 Adapter 类中,将有 getView 包含该视图中的 imageview 为 imageview 创建 onclick 侦听器

Imageview imageView=(ImageView)view.findviewByid(your id);

imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
于 2013-06-24T04:53:42.943 回答
1

使用onItemClickListener而不是onListItemClick

 lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        mCallback.itemSelected(mListItems[position]);

    }
 });
于 2013-06-24T04:44:26.153 回答
1
    public class MyListFragment extends ListFragment implements OnItemClickListener {
        private customList_Adaptor adapter;
        private static final int CAMERA_REQUEST = 0;
        private static final int RESULT_OK = 0;
        private static MyListFragmentInterface mCallback = null;
        static Context thisListFragmentContext = null;
        private String[] mListItems;
        private TrackerDBAdapter dbHelper;
        private List<String> listIDs;   
        public MyListFragment() {   

        }

        public MyListFragment(MyListFragmentInterface callback) {
            mCallback = callback;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view =  inflater.inflate(R.layout.my_list_fragment, container, false); 
            dbHelper = new TrackerDBAdapter(getActivity());
            dbHelper.open();
            Cursor cursor = dbHelper.fetchAllTrackerInTheList();
            mListItems = new String[dbHelper.getLastInsertedID()];
            thisListFragmentContext = getActivity();
            int i = 0;
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
                mListItems[i++] = cursor.getString(1);

            dbHelper.close();
            listIDs = new ArrayList<String>(); 
            listIDs = Arrays.asList(mListItems);
            adapter = new customList_Adaptor(thisListFragmentContext, R.layout.list_row, listIDs,this);
            ListView lv = (ListView) view.findViewById(android.R.id.list);      
            lv.setAdapter(adapter);     

            lv.setOnItemClickListener(this);

            return view;
        }

        public void addNewTrackerintoListDisplay(TrackerInfo newTracker){
            mListItems = append(mListItems, newTracker.getIdnumber());      
            setListAdapter(new ArrayAdapter<String>(getActivity(), 
                    android.R.layout.simple_list_item_1,
                mListItems));
        }

        @SuppressLint("NewApi")
        private String[] append(String[] mListItems2, String idnumber) {
            // TODO Auto-generated method stub
            final int N = mListItems2.length;
            mListItems2 = Arrays.copyOf(mListItems2, N + 1);
            mListItems2[N] = idnumber;
            return mListItems2;
        }

            @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
             mCallback.itemSelected(mListItems[position]);

        }
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {


        }

      public void imagepos(int i){
       //onImage click comes here
       }

    }
-----------------------------------------------------
 public class customList_Adaptor extends ArrayAdapter<String>{


        protected static final int CAMERA_REQUEST = 0;

        private Context context;
        MyListFragment mMyListFragment;
        private List<String> listIDs = new ArrayList<String>();

        public customList_Adaptor(Context context, int textViewResourceId,
                List<String> objects, MyListFragment mMyListFragment) {
            super(context, textViewResourceId, objects);
            this.setContext(context);
            this.listIDs = objects;
            this.mMyListFragment= mMyListFragment
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(listIDs != null)
                return listIDs.size();
            else
                return 0;
        }

        @Override
        public String getItem(int arg0) {
            // TODO Auto-generated method stub
            if(listIDs != null)
                return listIDs.get(arg0);
            else
                return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder viewHolder=new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(vi==null){
                vi = inflater.inflate(R.layout.list_row, parent, false);
                viewHolder.id=(TextView)vi.findViewById(R.id.title);
                viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
                viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
                vi.setTag(viewHolder);
            }
            else
                viewHolder=(ViewHolder) vi.getTag();


          viewHolder.thumbnailImage.setOnClickListener(new onMyClick(position));

            // Setting all values in listview
            viewHolder.id.setText(listIDs.get(position));

            return vi;
        }

public class onMyClick implements OnClickListener {

        private final int pos;
        public onMyClick(int pos) {
            this.pos = pos;
        }

        @Override
        public void onClick(View v) {
            mMyListFragment.imagepos(pos);
        }

    }

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }


    }
于 2013-06-24T04:45:14.587 回答
0

用于onItemClickListener()列表视图,您可以android:clickable="true"从图像视图中删除。另一种方法是在自定义适配器时设置onClickListener()图像视图。getView()但尝试第一种方法。

于 2013-06-24T04:50:04.593 回答