2

我已经在 android 4.0 中实现了片段。我只想为第一项添加图像到列表片段中。

我已经实现了列表片段如下:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     dummyContent = (new listFragmentAdapter<DummyContent.DummyItem>(getActivity(),DummyContent.ITEMS));
setListAdapter(dummyContent);   
    }

如下listFragmentAdapter

    public listFragmentAdapter(Context context, T[] items) {

            super(context,R.layout.list_fragment_text, items);
            this.ctx = context;

        }

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

            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view.findViewById(R.id.cust_view);
            Typeface tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/segoeuil.ttf");
            textView.setTypeface(tf);

            return view;
        }

}
4

1 回答 1

1

ImageView你需要在你的布局中至少有一个。然后,在方法中添加/删除您的图像(可绘制/位图)getView(),如下所示:

LayoutInflater mInflater = LayoutInflater.from(context);
itemView = mInflater.inflate(R.id.cust_view, null); 

// if first item
if (position == 0) {
    imageView = (ImageView) itemView.findViewById(R.id.imageViewFirstRow);
    imageView.setImageResource(R.drawable.yourImage);
    imageView.setVisibility(Visible.VISIBLE);
}
else {
    // hide/remove image
    imageView.setVisibility(Visible.INVISIBLE); // or GONE, as you wish
}

希望这可以帮助。

于 2013-01-07T09:36:17.720 回答