android - ViewPager 中使用的 Fragment 内的 ListView 的行为与预期不符

标签 android android-layout android-listview fragment android-viewpager

我对该 ListView 中的项目有疑问。正常行为是当用户点击一项时,背景颜色变为橙色。我的应用没有发生这种情况,我不明白为什么。

main.xml 只有一个:

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

fragment 的 xml 只有一个:

<ListView
        android:id="@android:id/list"
        style="@style/MyListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

我在 listView 项目中膨胀的 View 是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/imageView1"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/textViewName"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/bottom_arrow" />

</RelativeLayout>

我为ListView定义的样式是:

<style name="MyListView" parent="@android:style/Widget.ListView.White">
        <item name="android:listSelector">@drawable/list_subject_selector</item>
        <item name="android:cacheColorHint">@android:color/transparent</item>
        <item name="android:background">@drawable/list_subject_selector</item>
    </style>

list_subject_selector 可绘制对象是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- touch down -->
    <item android:drawable="@drawable/list_subject_focused" android:state_pressed="true"/>
    <!-- selected -->
    <item android:drawable="@drawable/list_subject_unfocused" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
</selector>

我不知道为什么会这样...我正在使用 Android ICS 进行测试。

有什么想法吗?

谢谢大家 菲利普

更新 1 这是 ListFragment 代码:

public class MyFragment extends ListFragment {
    private ListAdaptor adapter;
    private InternalDB db;
    Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getActivity();
        db = new InternalDB(getActivity());
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayList<EventVO> listVO = db.getSummaryEvents(true);
        adapter = new ListAdaptor(mContext, R.layout.event_inflate, listVO);
        setListAdapter(adapter);            
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout rl = (LinearLayout) inflater.inflate(R.layout.general_list, container, false);
        return rl;
    }

    private class ListAdaptor extends ArrayAdapter<EventVO> {
        private ArrayList<EventVO> listVO;

        public ListAdaptor(Context context, int textViewResourceId, ArrayList<EventVO> items) {
            super(context, textViewResourceId, items);
            this.listVO = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                v = ViewGroup.inflate(getContext(), R.layout.event_inflate, null);
            }
            final EventVO o = listVO.get(position);
            TextView fullName = (TextView) v.findViewById(R.id.textViewName);
            fullName.setText(o.getUserName());

            //removed part of the code for brevity

            v.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent i = new Intent(mContext, EventDetailsActivity.class);
                    i.putExtra("USER_ID", o.getUserId());                       
                    startActivity(i);
                }
            });
            return v;
        }
    }
}

更新 2 现在可以了!那很烦人。我这样做是为了让它工作:

我用来膨胀的 xml 文件有:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true" >

我改为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

但是,我认为真正带来改变的是: onActivityCreated() 内部

list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                ListAdaptor la = (ListAdaptor)arg0.getAdapter();
                EventVO vo = la.getItem(arg2);
                Intent i = new Intent(mContext, EventDetailsActivity.class);
                i.putExtra("USER_ID", vo.getUserId());
                i.putExtra("IS_BORROWED", true);
                startActivity(i);
            }

重要的是行 list = getListView(); 我没有通过这段代码获得 ListView。我在 onCreateView() 方法中获取了带有 findViewById 的 listView。我想我失去了与真实 ListView 对象的链接。

感谢所有帮助过我的人! :)

费利佩 });

最佳答案

尝试更改您的列表选择器。目前,第二个可绘制对象仅在您的 ListView 中的项目未获得焦点按下并且被选中时使用。我真的不明白你希望你的选择器看起来如何,但是如果 "@drawable/list_selector_unfocused" 是你希望列表选择器正常看起来的样子,只需删除所有其他属性,这意味着"@drawable/list_selector_focused"在按下项目时使用。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- touch down -->
<item android:drawable="@drawable/list_subject_focused" android:state_pressed="true"/>
<!-- selected -->
<item android:drawable="@drawable/list_subject_unfocused" />

关于android - ViewPager 中使用的 Fragment 内的 ListView 的行为与预期不符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10786811/

相关文章:

android - 在android中将布局转换为位图时获取倒置图像

java - 如何在 Activity 类中启用 xml Button?

android - 在方向更改时保留列表 fragment 中的列表

android - WrongViewCast 错误 : Unexpected implicit cast to TabLayout: layout tag was Linear Layout

android - 是否可以在 android 中以编程方式阻止/禁用出厂重置?

android - 代号 One Android sendMessage HTML

android - 使用复选框从 ListView 中获取选中的项目

android - cocos2dx(2.0.4) android 创建项目问题

View 上的Android动态气泡

安卓工程: ListView versus just a dynamic LinearLayout