1

用例如下:我想在 ViewPager 中加载一个 ListView,同时在列表上设置一个 OnItemClickListener。

有人可以告诉为什么 ListView 没有加载,即屏幕保持黑色。甚至传呼机的条带也不起作用。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0px" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="1"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

</android.support.v4.view.ViewPager>

一些代码:

public static class HeaderFragment extends ListFragment {

    List<Message> messages = new ArrayList<Message>();
    String topic;

    public HeaderFragment(List<Message> messages, String topic) {
        this.messages = messages;
        this.topic = topic;
    }

    public HeaderFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.main, container, false);

        final ListView listView = (ListView) v.findViewById(android.R.id.list);

        final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
                R.layout.headers, messages, 1);


        //the click listener.
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Message message = adapter.getItem(position);
                Intent intent = new Intent();
                intent.setClassName(
                        "main",
                        "NewsActivity");
                intent.putExtra("title", message.getTitle());
                intent.putExtra("description", message.getDescription());
                intent.putExtra("source", message.getLink().toString());
                intent.putExtra("language", location);
                startActivity(intent);
            }
        });

        listView.setAdapter(adapter);

        return v;
    }

提前致谢 !!!

编辑:

调用片段的代码:

public class HeadersActivityPagerAdapter extends FragmentStatePagerAdapter {

    public HeadersActivityPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {

        int k = 0;
        String topic = null;
        List<Message> messages = new ArrayList<Message>();

        for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
            if (k==i)
            {
                topic = elem.getKey();
                messages = elem.getValue();
                break;
            }
            k++;
        }

        return new HeaderFragment(messages, topic);
    }

    @Override
    public int getCount() {
        return fragmentsData.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        int k = 0;
        String topic = "";
        for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
            if (k==position)
            {
                topic = elem.getKey();
                break;
            }
            k++;
        }
        return topic;
    }
}

它像这样工作,但我失去了 ListView 的侦听器:

主要的.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >

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

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />
</android.support.v4.view.ViewPager>

子主.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />
</FrameLayout>

一些代码:

public static class HeaderFragment extends ListFragment {

    List<Message> messages = new ArrayList<Message>();
    String topic;

    public HeaderFragment(List<Message> messages, String topic) {
        this.messages = messages;
        this.topic = topic;
    }

    public HeaderFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.submain, container, false);

        final ListView listView = (ListView) v.findViewById(android.R.id.list);

        final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
                R.layout.headers, messages, 1);


        //the click listener.
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Message message = adapter.getItem(position);
                Intent intent = new Intent();
                intent.setClassName(
                        "main",
                        "NewsActivity");
                intent.putExtra("title", message.getTitle());
                intent.putExtra("description", message.getDescription());
                intent.putExtra("source", message.getLink().toString());
                intent.putExtra("language", location);
                startActivity(intent);
            }
        });

        listView.setAdapter(adapter);

        return v;
    }

问题是我失去了我的听众,没有触发。我找到的解决方案减慢了我的应用程序。

对于每个项目,我将侦听器放置在适配器内。

4

1 回答 1

0

您的 ViewPager 设置为 height 0px。给它一个更大的高度,你应该能够看到它。也许:

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

编辑:

显示您正在创建此片段并将其添加到屏幕的代码。还要向此构造函数添加一些日志记录:

public HeaderFragment(List<Message> messages, String topic) {
    this.messages = messages;
    Log.i("TAG", "messages size: " + this.messages.size(); // <- add this
    this.topic = topic;
}

以确保messages在传递到此处时其中包含一些项目。此片段从不添加任何项目,messages因此如果在您创建片段时它没有任何项目,则不会在 ListView 中显示任何项目。

于 2013-05-18T13:41:58.177 回答