Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add static header image in custom listview?

I will put a picture on top of a custom list view for the header image, but the image is repeated, how can I make it can't click and non-repeatable? this is my xml code :

    <?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>

this is my adapter :

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

like image 339
Muhammad Nafian Wildana Avatar asked Dec 05 '25 14:12

Muhammad Nafian Wildana


2 Answers

Remove the image form the xml.

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

The docs

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.

Edit:

@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);
    }

Edit, if want to set imageview from other xml layout :

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);
like image 187
Raghunandan Avatar answered Dec 07 '25 02:12

Raghunandan


Try below code:-

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

myList.addHeaderView(imageHeaderView);
like image 26
duggu Avatar answered Dec 07 '25 03:12

duggu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!