使用片段填充ArrayAdapter的行

时间:2013-03-29 09:53:53

标签: android android-fragments android-arrayadapter android-listfragment

我有一个片段ArticleFragment,它传递了一个Article对象。 ArticleFragment通常显示在Activity中,但我想在ListView中重用该片段。 ListView的每一行都应包含ArticleFragment的实例。 我知道我可以使用适配器的getView()方法自定义行,但ArticleFragment显示的UI与行的UI非常相似,所以必须在更新它时很烦人如果UI改变,则有两个位置。

ArticleFragment.java

public class ArticleFragment extends SherlockFragment {
    static final String ARTICLE_PARCEL_KEY = "parcelable_article";

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

    protected static ArticleFragment newInstance(Article article) {
        ArticleFragment f = new ArticleFragment();
        Bundle args = new Bundle(Article.class.getClassLoader());
        args.putParcelable(ARTICLE_PARCEL_KEY, article);
        f.setArguments(args);
        return f;
    }

    Article getArticle() {
        return getArguments().getParcelable(ARTICLE_PARCEL_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = (View) inflater.inflate(R.layout.article, container, false);
        ArticleImagesFragment imagesFragment = ArticleImagesFragment.newInstance(getArticle().getImages());
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.add(R.id.images_fragment_container, imagesFragment);
        if (getActivity() instanceof ArticleActivity) {
            DisqusCommentsFragment disqusFragment =  DisqusCommentsFragment.newInstance(getArticle().getCommentsUrl());
            ft.add(R.id.comments_fragment_container, disqusFragment);
        }
        ft.commit();
        return layout;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Article article = getArticle();

        TextView kickerView = (TextView) view.findViewById(R.id.kicker);
        kickerView.setText(article.getKicker().toUpperCase());

        TextView titleView = (TextView) view.findViewById(R.id.title);
        titleView.setText(article.getTitle());

        String commentsText = "Jetzt kommentieren!";
        if (article.getNumComments() == 1) {
            commentsText = article.getNumComments().toString() + " Kommentar";
        } else if (article.getNumComments() > 1) {
            commentsText = article.getNumComments().toString() + " Kommentare";
        }
        TextView numCommentsView = (TextView) view.findViewById(R.id.num_comments);
        numCommentsView.setText(commentsText);

        TextView pubDateView = (TextView) view.findViewById(R.id.pub_date);
        String dateText = new SimpleDateFormat("dd. MMMM, hh:mm").format(article.getPubDate());
        pubDateView.setText(dateText);

        TextView authorNameView = (TextView) view.findViewById(R.id.author_name);
        authorNameView.setText(article.getAuthorName());

        TextView articleTextView = (TextView) view.findViewById(R.id.text);
        articleTextView.setText(Html.fromHtml(article.getHtmlContent()));
    }
}

我注意到可以将自定义行布局传递给ArrayAdapter,但这会再次复制用于显示文章UI的逻辑。相反,我更喜欢将片段传递给适配器的构造函数,并使用它来显示行。

是否可以通过ArrayAdapter来实现这一目标? 如果不是在ListView中实现ArticleFragment的重用的正确途径?

感谢您的帮助!

0 个答案:

没有答案