onItemClick不能在片段中的列表视图中工作

时间:2014-12-29 18:45:33

标签: android listview android-fragments onitemclicklistener onitemclick

所以,我在片段中处理listview ... " onItemClick"不起作用,但onItemLongClick和刷新工作正常.. (使用SherlockLibary ......) 这是我的代码:

     public static class  MyListActivity extends SherlockListFragment
        implements AdapterView.OnItemClickListener,    AdapterView.OnItemLongClickListener,OnRefreshListener {

    ArrayList<article> items2;
    private String[] articles = {"x","y","z"};    //articles titles

    private String[] Dates = {"20:12"
            , "18:20"
            , "15:15"
            , "14:11"
            , "10:00"
    };    //articles dates
    private Site[] Sites = {
            Site.Ynet
            , Site.bla
            , Site.blabla
            , Site.blablabla
            , Site.blablabla
    };    //articles Gender


    private void initData() {
        items2 = new ArrayList<article>();
        for (int i = 0; i < 5; i++) {
            items2.add(new article(articles[i], Dates[i], Sites[i]));
        }
    }

    private PullToRefreshLayout mPullToRefreshLayout;
    ListView list;
    MyArrayAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        mPullToRefreshLayout = (PullToRefreshLayout) view.findViewById(R.id.listlay);
        ActionBarPullToRefresh.from(getActivity())
                .allChildrenArePullable()
                .listener(this)
                .setup(mPullToRefreshLayout);

        initData();
        list = (ListView)view.findViewById(android.R.id.list);
        adapter = new MyArrayAdapter(getActivity(), items2);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
        list.setOnItemLongClickListener(this);
        return view;
    }



    // Handle click on an item (displays it in a Toast)
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        items2.remove(position);
        Toast.makeText(getActivity(), "select: " + items2.get(position).toString(), Toast.LENGTH_LONG).show();
    }

    // Handle a long click on an item (deletes it)
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View v,
                                   int position, long id) {
        Toast.makeText(getActivity(),
                "del: " + items2.get(position).toString(),
                Toast.LENGTH_LONG).show();
        items2.remove(position);
        adapter.notifyDataSetChanged(); // Update the ListView

        return true;  // i.e. all ended well
    }

    @Override
    public void onRefreshStarted(View view) {

        /**
           nathing here yet...
         * Simulate Refresh with 4 seconds sleep
         */

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(Constants.SIMULATED_REFRESH_LENGTH);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                // Notify PullToRefreshLayout that the refresh has finished
                mPullToRefreshLayout.setRefreshComplete();
            }
        }.execute();

    }
}

任何想法我能用它做什么? 我尝试了很多,但是努力工作....请帮助....

我尝试添加list.setItemsCanFocus(false);或者android:focusable =&#34; false&#34; 或者ndroid:clickable =&#34; false&#34; ... 不起作用......

1 个答案:

答案 0 :(得分:1)

如果onItemClick()的项目可以调整,则

ListView方法将无效。检查你的项目xml,看看你是否为它的任何元素设置了这些。

android:focusable="true"

android:clickable="true" 

另一个解决方案:使用此行

list.setItemsCanFocus(false);

之后

list = (ListView)view.findViewById(android.R.id.list);