ListView去掉默认点击效果

取消ListView默认的点击效果的方法:

在Listview中添加如下代码

android:listSelector="@android:color/transparent"

- 如果在listview中单独添加了一个view,并且想要只有该view不显示点击效果,方法是给该view设置背景色。

public View getView(int position, View convertView, ViewGroup parent) {
            Logger.e("position = " + position);
            if (position == 0) {
                Logger.d("add program description");
                TextView textView = new TextView(getActivity());
                ListView.LayoutParams layoutParams = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT);
                textView.setLayoutParams(layoutParams);
                textView.setPadding(20, 0, 20, 0);
                textView.setText(R.string.program_description);
                textView.setTextColor(getResources().getColor(R.color.black));
                textView.setTextSize(12);
                textView.setBackgroundColor(getResources().getColor(R.color.white));//只有该view没有点击效果。
                textView.setSelected(false);
                return textView;
            } else if (position == 1) {
                ImageView imageView = new ImageView(getActivity());
                ListView.LayoutParams layoutParams = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT);
                imageView.setLayoutParams(layoutParams);
                imageView.setBackgroundResource(R.drawable.bar_next);
                return imageView;
            }
            View programView = mInflater.inflate(R.layout.program_listview_part, null);
            ChannelItem program = mProgramItems.get(position);

            TextView time = (TextView) programView.findViewById(R.id.next_program_time_textview);
            time.setText(program.getPubDate());

            TextView name = (TextView) programView.findViewById(R.id.next_program_name_textview);
            name.setText(program.getTitle());

            return programView;
        }



在断开网络后点击原来Listview中的内容时,会报出如下错误。对应方法是,在网络请求失败后需要再次调用一次mListAdapter.notifyDataSetChanged();函数。

04-27 15:09:53.163  13398-13398/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230833, class android.widget.ListView) with Adapter(class jp.nhkworldtv.android.ui.fragment.ProgramMainFragment$10)]
            at android.widget.ListView.layoutChildren(ListView.java:1770)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3203)
            at android.widget.ListView.onTouchEvent(ListView.java:4089)
            at android.view.View.dispatchTouchEvent(View.java:5568)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2064)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1732)
            at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6217)
            at android.widget.ListView.dispatchTouchEvent(ListView.java:4809)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2070)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1746)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1961)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1398)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1909)
            at android.view.View.dispatchPointerEvent(View.java:5748)
            at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3165)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2719)
            at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:951)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2733)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:4974)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)


你可能感兴趣的:(ListView)