Viewpager 片段中的 Android 列表

发布于 2024-12-10 03:18:20 字数 1205 浏览 1 评论 0原文

我为每个页面设置了一个视图分页器和片段,但无法弄清楚如何在其中一个片段中显示列表,我尝试过的所有内容都已强制关闭,所以这就是我现在回到的内容有关

public class Extras extends Fragment {

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

如何我的任何帮助可以将其变成一个简单的列表将不胜感激

===编辑===

最终结果

public class Extras extends ListFragment { 

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.getListView().setTextFilterEnabled(true);
        this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.row, items));
        this.setEmptyText(getString(R.string.loading));
    }
    static final String[] items = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
     super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(), l.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }
}

I have a viewpager set up and fragments for each page but can't figure out how to show a list inside one of the fragments everything i've tried has forced closed so here's what i'm back to right now

public class Extras extends Fragment {

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

Any help on how i could turn that into a simple list would be greatly appreciated

===Edit===

Final Result

public class Extras extends ListFragment { 

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.getListView().setTextFilterEnabled(true);
        this.setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.row, items));
        this.setEmptyText(getString(R.string.loading));
    }
    static final String[] items = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
     super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(), l.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-12-17 03:18:20

您可以使用 ListFragment

public class ExtraListFragment extends ListFragment { 

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

        this.getListView().setTextFilterEnabled(true);

        this.setListAdapter(adapter); // your list adapter
        this.setEmptyText(getString(R.string.loading));
    }
}

you can use ListFragment

public class ExtraListFragment extends ListFragment { 

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

        this.getListView().setTextFilterEnabled(true);

        this.setListAdapter(adapter); // your list adapter
        this.setEmptyText(getString(R.string.loading));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文