如何在ListFragment的自定义布局中使用自定义布局的对象填充ListView?

时间:2014-11-07 19:55:11

标签: android android-layout listview

我正在使用ListFragment,默认情况下会扩展全屏ListView。我可以使用自定义布局的对象成功填充此ListView。我的问题是,如何在ListFragment的非默认布局中填充这些自定义布局的自定义对象?

这是我想要扩充和放置对象的布局(id:“article_List”)。      

<RelativeLayout
    android:id="@+id/footer_button_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center" >

    <Button
        android:id="@+id/left_footer_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Back" />

    <Button
        android:id="@+id/right_footer_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/left_footer_button"
        android:text="Next" />
</RelativeLayout>

<ListView
    android:id="@+id/article_List"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"

    android:layout_above="@id/footer_button_layout" >
</ListView>

填充ListView的对象的布局:      

<TextView
    android:id="@+id/journalTitle_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:maxLines="1"
    android:text="journal title"
    android:textSize="12sp" />

<TextView
    android:id="@+id/pubYear_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/journalTitle_textView"
    android:maxLines="1"
    android:text="pubYear"
    android:textSize="12sp" />

<TextView
    android:id="@+id/volume_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/pubYear_textView"
    android:maxLines="1"
    android:text="volume"
    android:textSize="12sp" />

<TextView
    android:id="@+id/issue_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/volume_textView"
   android:maxLines="1"
    android:text="issue"
    android:textSize="12sp" />

<TextView
    android:id="@+id/pageInfo_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/issue_textView"
    android:maxLines="1"
    android:text="pageInfo"
    android:textSize="12sp" />

<TextView
    android:id="@+id/title_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/journalTitle_textView"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:maxLines="2"
    android:text="artile title"
    android:textSize="15sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/authorString_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title_textView"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:maxLines="1"
    android:text="authors"
    android:textSize="12sp" />

我的自定义ArrayAdapter类:

    private class ArticleAdapter extends ArrayAdapter<Article> {
    public ArticleAdapter(ArrayList<Article> entries) {
        super(getActivity(), 0, entries);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // View of single article in ListView
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater().inflate(
                    R.layout.articlelistfragment, null);
        }

        // This is the magic method that gets the article at position
        // "position"
        Article en = getItem(position);

        // if attribute value is null, then set textView visibility GONE.
        // Does not take up any layout space
        TextView journalTitleView = (TextView) convertView
                .findViewById(R.id.journalTitle_textView);
        if (en.getJournalTitle() != null) {
            journalTitleView.setText(en.getJournalTitle());
        } else {
            journalTitleView.setVisibility(View.GONE);
        }

        TextView pubYearView = (TextView) convertView
                .findViewById(R.id.pubYear_textView);
        if (en.getPubYear() != null) {
            pubYearView.setText(en.getPubYear());
        } else {
            pubYearView.setVisibility(View.GONE);
        }

        TextView volumeView = (TextView) convertView
                .findViewById(R.id.volume_textView);
        if (en.getVolume() != null) {
            volumeView.setText("vol." + en.getVolume());
        } else {
            volumeView.setVisibility(View.GONE);
        }

        TextView issueView = (TextView) convertView
                .findViewById(R.id.issue_textView);
        if (en.getIssue() != null) {
            issueView.setText("(" + en.getIssue() + ")");
        } else {
            issueView.setVisibility(View.GONE);
        }

        TextView pageInfoView = (TextView) convertView
                .findViewById(R.id.pageInfo_textView);
        if (en.getPageInfo() != null) {
            pageInfoView.setText("pp." + en.getPageInfo());
        } else {
            pageInfoView.setVisibility(View.GONE);
        }

        TextView titleView = (TextView) convertView
                .findViewById(R.id.title_textView);
        titleView.setText(en.getTitle());

        TextView authorStringView = (TextView) convertView
                .findViewById(R.id.authorString_textView);
        if (en.getAuthorString() != null) {
            authorStringView.setText(en.getAuthorString());
        } else {
            authorStringView.setVisibility(View.GONE);
        }

        return convertView;
    }
}

我想坚持使用ArrayAdapter。使用ListFragment的默认实现来使用我的自定义适配器填充ListView,我执行了以下操作(有效):

    @Override
    protected void onPostExecute(ArrayList<Article> result) {
        getActivity().setTitle(R.string.title_bar);
        ArticleAdapter adapter = new ArticleAdapter(articles);
        setListAdapter(adapter);
    }

0 个答案:

没有答案