Listview


ListView on ScrollView

Buat custom ListView tanpa fitur scroll.

package al.akh.example.testing.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;

public class ListViewNonScroll extends ListView {

    public ListViewNonScroll(Context context) {
        super(context);
    }
    public ListViewNonScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ListViewNonScroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

 

Pada layout resource file, gunakan custom ListView yang telah dibuat di atas.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdgeLength="0dp"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:scrollbars="none" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <al.akh.example.testing.view.ListViewNonScroll
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/ >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/list_view" >
            ...
        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

Custom Color Listview Selected

Pada res, buat file color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#04b3d3" />
    <item android:state_selected="true" android:color="#048563" />
    <item android:color="#04b3d3" />
</selector>

Pada textview property, tambahkan android:textColor=”@color/color_selector”.

<TextView
    android:id="@+id/tv_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/color_selector"/>

Pada program java, tambahkan view.setSelected(true);.

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
    }
});