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 のリスナーを失います:

main.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>

submain.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 に答える