Hide listdivider for selected row android?

980 views Asked by At

I have a android native listview.now i have to hide the divider of only selected row and it's neighbour row like in the image shown follows.how i can accomplish that ?

enter image description here

Here my lisview code

 <ListView
            android:id="@+id/listView_errors"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:divider="@drawable/list_divider_complaint"
            android:dividerHeight="1sp"
            android:listSelector="@drawable/bgselected"
            android:scrollbars="none" />

And the result so far

enter image description here

4

There are 4 answers

0
devops On BEST ANSWER

Create your custom divider view inside of your custom list item layout. Then if an item is selected, hide this custom divider view.

Style:

<style name="HorizontalDivider">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">1px</item>
    <item name="android:background">#444</item>
</style>

Custom list item:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:background="@drawable/click_effect_green"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/line_a"
        android:layout_marginTop="4dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/line_b"
        android:layout_marginBottom="4dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="14dp" />

     <View id="@+id/divider" style="@style/HorizontalDivider"/>
</LinearLayout>
2
Vaibs_Cool On

onSelect code you can try this

yourListView.setDivider(null);

or

yourListView.setDivider(new ColorDrawable(android.R.color.transparent));
yourListView.setDividerHeight(0);
2
Garima Mathur On

You have to follow these steps:

  1. Remove your list view divider

     <ListView
        android:id="@+id/listView_errors"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:divider="@null"
        android:listSelector="@drawable/bgselected"
        android:scrollbars="none" />
    
  2. Added a view into list_item.xml consider as list_divider

       <View
        android:id="@+id/list_divider"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@android:color/black" />
    
  3. You can remove view divider when list item is selected as if you are selecting on onItemClick then it should be

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        view.findViewById(R.id.list_divider).setVisibility(View.GONE);
    
    }
    

This is the solution where you can do anything you want customized rows.

0
Anoop M Maddasseri On

list_item.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="55dp"
    android:orientation="vertical"

    >

    <TextView
        android:id="@+id/item_complaint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:text="Daily Consumption"
        android:textColor="#505050"
        android:textSize="18sp"
        android:textStyle="bold" />

    <View
        android:id="@+id/item_divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#9e9e9e" />


</RelativeLayout>

Listview

  <ListView
            android:id="@+id/listView_errors"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:layout_gravity="center_horizontal"
            android:listSelector="@drawable/bgselected"
            android:scrollbars="none" />

Adapter

 ArrayAdapter<String> ar = new ArrayAdapter<String>(getActivity(), R.layout.complaint_listview, R.id.item_complaint, errors);
                listErrors.setAdapter(ar);

Divider Controller

private int prevSelected = -1;

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        selectedError = adapterView.getItemAtPosition(i).toString();
        try {
            controlDividers(i);
        } catch (Exception e) {
            //TODO
        }
    }
    private void controlDividers(int position) {
        if (prevSelected != -1) {
            if (prevSelected == 0) {
                listErrors.getChildAt(prevSelected).findViewById(R.id.item_divider).setVisibility(View.VISIBLE);
            } else {
                listErrors.getChildAt(prevSelected).findViewById(R.id.item_divider).setVisibility(View.VISIBLE);
                listErrors.getChildAt(prevSelected - 1).findViewById(R.id.item_divider).setVisibility(View.VISIBLE);
            }
        }
        if (position == 0) {
            listErrors.getChildAt(position).findViewById(R.id.item_divider).setVisibility(View.INVISIBLE);
        } else {
            listErrors.getChildAt(position).findViewById(R.id.item_divider).setVisibility(View.INVISIBLE);
            listErrors.getChildAt(position - 1).findViewById(R.id.item_divider).setVisibility(View.INVISIBLE);
        }
        prevSelected = position;
    }