Label each row in the list

I am trying to set the ListViewdefault background image and highlighted background image for each individual line.

However, the highlighted background image affects individual lines, and the default background image affects the entire line ListView; but I need this to affect every line.

Can someone tell me how to do this?

This is my code:

layout /main.xml:

<ListView
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:dividerHeight="1dip"       
 android:listSelector="@drawable/bg_highlighted"
/>

hoods /selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true" android:drawable="@drawable/bg_default"/>
<item android:state_focused="true" android:drawable="@drawable/bg_default"/>

</selector>

SIC /main.java:

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,R.layout.list, mStrings); /*"Item1","Item2","Item3","Item4"*/
    ListView lv = (ListView)findViewById(R.id.list);
    lv.setAdapter(adapter);
    lv.setBackgroundResource(R.drawable.selector);

As the default background and selected background, I use png images.

This is what I have , but this is what I want .

+3
source share
3 answers

arrayadapter, , , getview

0

heepie.
, , , ListView. , getView . . , -. /main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >

        <ListView
           android:id="@+id/my_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
         />

</LinearLayout>

/my _list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="left|center_vertical"
    android:paddingLeft="6dip"
    android:textColor="#FFFFFF"
    android:background="@drawable/list_selector"    
/>

/list _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:drawable="@drawable/bg_highlighted" />
    <item android:drawable="@drawable/bg_default" />
</selector>

png:
/bg _default.png:
 bg_default

/bg _highlighted.png:
 bg_highlighted

/main.java:

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyAdapter adapter;
        ArrayList<String> mStrings = new ArrayList<String>();
        mStrings.add("Item 1");
        mStrings.add("Item 2");
        mStrings.add("Item 3");
        mStrings.add("Item 4");

        ListView lv = (ListView) findViewById(R.id.my_list);
        adapter = new MyAdapter(this, mStrings);
        lv.setAdapter(adapter);
    }
}

/MyAdapter.java:

public class MyAdapter extends BaseAdapter {

public Activity activity;
ArrayList<String> data = new ArrayList<String>();
private static LayoutInflater inflater = null;

public MyAdapter(Activity a, ArrayList<String> d) {
    activity = a;
    data = d;
    inflater = LayoutInflater.from(activity);
}    

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.my_list_item, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Touch on view handle
            Log.d("", "Touched row "+position);
        }

    });

    //customizing view
    holder.textView = (TextView)convertView.findViewById(R.id.my_textview);
    holder.textView.setText(data.get(position));

    return convertView;
}       

public static class ViewHolder {
    public TextView textView;
}
@Override
public int getCount() {
    return data.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
}

:

result.png

+1

, , , ListView :

lv.setBackgroundResource(R.drawable.selector);

All you have to do is set this selector as the background of the root view inside yours layout/list.xml(the layout is overpriced for each item in the list)

0
source

All Articles