Search code examples
androidandroid-listfragment

How to remove transparency from ListFragment


I have a Fragment which extends ListFragment, this is added at runtime to a FrameLayout. The FrameLayout already contains an ImageView.

When the ListFragment is added at runtime, I can see through to the background, I just want a solid color instead.

main.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/list_fragment" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/icon"
        android:tileMode="repeat" />

</FrameLayout>

I have tried the following but this just makes it go black when I scroll:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    getListView().setCacheColorHint(R.color.list_background);
    super.onActivityCreated(savedInstanceState);
}

And in the R.color.list_background is:

<color name="list_background">#e2f4c7</color>

see screenshot below:

enter image description here

Here is my custom adapter

public class DailyFlightsAdapter extends ArrayAdapter<DailyFlightVO> {

Context context; 
int layoutResourceId;    
ArrayList<DailyFlightVO> data = null;

public DailyFlightsAdapter(Context context, int layoutResourceId, ArrayList<DailyFlightVO> dailyFlights) {
    super(context, layoutResourceId, dailyFlights);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = dailyFlights;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    FlightHolder holder = null;

    if(row == null){
        LayoutInflater inflator = ((Activity)context).getLayoutInflater();
        row = inflator.inflate(layoutResourceId, parent, false);

        holder = new FlightHolder();
        holder.distance = (TextView)row.findViewById(R.id.distance);
        holder.pilot = (TextView)row.findViewById(R.id.pilot);

        row.setTag(holder);
    }else{
        holder = (FlightHolder)row.getTag();
    }

    DailyFlightVO vo = data.get(position);
    holder.distance.setText(vo.getDistance());
    holder.pilot.setText(vo.getPilot());

    return row;
}

static class FlightHolder{
    TextView distance;
    TextView pilot;
}
}

Solution

  • Instead of getListView().setCacheColorHint(R.color.list_background); try the following:

    getListView().setBackgroundColor(getResources().getColor(R.color.list_background));
    

    Also see the docs for setBackgroundColor and getColor. You have to use getColor here because you need to transform the resource-id into the real color.