在ListView onItemClick行不通的片段 [英] onItemClick doesnt work in a listview in a fragment

查看:133
本文介绍了在ListView onItemClick行不通的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我工作在一个片段一个列表视图... 在onItemClick不工作,但onItemLongClick和刷新效果很好.. (使用SherlockLibary ...) 这里是我的code:

so, i working on a listview in a fragment... the "onItemClick" doesnt work, but the onItemLongClick and the refresh works well.. (using SherlockLibary...) here is my code:

     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();

    }
}

任何想法,我能做些什么呢? 我尝试了很多,但nathing工程....帮助请....

any idea what can i do with it? i tried a lot but nathing works.... help please....

我试过加list.setItemsCanFocus(假);或机器人:可调焦=假 或款A ndroid:点击=假? 不工作...

i tried the to add list.setItemsCanFocus(false); or android:focusable="false" or a ndroid:clickable="false" ... doesnt work...

推荐答案

onItemClick()方法将不能工作,如果的ListView的项目可获得焦点。检查项目的XML,看看你是否已经设置这些为它的任何元素。

onItemClick() method won't work if the items of ListView is focusable. Check your item xml to see if you have set these for any of its element.

android:focusable="true"

android:clickable="true" 

另一个soulution:使用此行

list.setItemsCanFocus(false);

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

这篇关于在ListView onItemClick行不通的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆