如何在自定义列表视图中添加静态标题图像?

时间:2014-05-28 09:17:46

标签: android listview

我会在标题图片的自定义列表视图顶部放置一张图片,但图片会重复出现,如何让它无法点击且不可重复? 这是我的xml代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/abs__background_holo_light" >

    <ImageView
        android:id="@+id/detailImage"
        android:layout_width="wrap_content"
        android:layout_height="140dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        android:scaleType="centerCrop"
        android:src="@drawable/pantaigoacina" />

    <RelativeLayout
        android:id="@+id/detailLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/detailImage"
        android:background="@color/abs__background_holo_light" >

        <LinearLayout
            android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="20dip"
            android:background="@color/abs__background_holo_light"
            android:padding="5dip" >

            <ImageView
                android:id="@+id/list_image"
                android:layout_width="15dip"
                android:layout_height="15dip" 
                android:src="@drawable/arrow"
                android:contentDescription="@string/app_name"/>
        </LinearLayout>

        <TextView
            android:id="@+id/NamaLokasi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Pantai Sipelot"
            android:textColor="#040404"
            android:textSize="18sp"
            android:textStyle="bold"
            android:typeface="sans" />

        <TextView
            android:id="@+id/detailLokasi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/NamaLokasi"
            android:layout_marginRight="20dip"
            android:layout_marginTop="5dip"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Deskripsi Konten, Lokasi Berada di Desa Wilayah Malang"
            android:textColor="#343434"
            android:textSize="12sp" />
    </RelativeLayout>

</RelativeLayout>

这是我的适配器:

String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };

SimpleAdapter adapter = new SimpleAdapter(getActivity()
        .getBaseContext(), aList, R.layout.fragment_detail, from,
        to);

setListAdapter(adapter);

return super.onCreateView(inflater, container, savedInstanceState);

this is the header when repeated

2 个答案:

答案 0 :(得分:2)

从xml中删除图像。

ImageView iv = new ImageView(getActivity());
// set image to imageview
ListView list = getListView();
list.addHeaderView(headerView);

文档

public void addHeaderView (View v)

Added in API level 1
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.

Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

Parameters
v   The view to add.

编辑:

@Override
    public void onActivityCreated(Bundle savedInstanceStat)
    {
super.onActivityCreated(savedInstanceState);
               list = getListView();
       ImageView imageHeaderView = new ImageView(getActivity());
       imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pantaigoacina));

       list.addHeaderView(imageHeaderView);

       List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

       for (int i = 0; i < DaerahWisata.pantai.length; i++) {
               HashMap<String, String> hm = new HashMap<String, String>();
               hm.put("pantai", DaerahWisata.pantai[i]);
               hm.put("detail", "Lokasi : \r\n" + DaerahWisata.detailp[i]);
               aList.add(hm);
       }

       String[] from = { "pantai", "detail" };
       int[] to = { R.id.NamaLokasi, R.id.detailLokasi };

       SimpleAdapter adapter = new SimpleAdapter(getActivity()
                       .getBaseContext(), aList, R.layout.fragment_detail, from,
                       to);           
       setListAdapter(adapter);
    }

编辑,如果想从另一个xml布局设置imageview:

list = getListView();
    Context context = getListView().getContext();
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.image_header, null);
    list.addHeaderView(view);

答案 1 :(得分:2)

尝试以下代码: -

ImageView imageHeaderView = new ImageView(this);//use getActivity() on fragment
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));

myList.addHeaderView(imageHeaderView);
相关问题