2

我有一个片段ArticleFragment,它传递了一个Article对象。通常显示在 Activity 中,ArticleFragment但我想在 ListView 中重用片段。ListView 的每一行都应该包含一个ArticleFragment. 我知道我可以使用getView()适配器的方法自定义一行,但是显示的 UI 与ArticleFragment一行非常相似,因此如果 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

谢谢你的帮助!

4

0 回答 0