简体   繁体   中英

Customize List Item on ListActivity (Android)

I'd like to customize some settings (color, margin) for some items on a listview in a listactivity after or before setting the adapter. How can I do that? Is there any function that can I override?

Thank you.

您可以使用自己的listadapter .. http://www.vogella.com/articles/AndroidListView/article.html

This is how you can do to customize item of your list:

Layout of your list:

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

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

Layout of each item:

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

<TextView
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

</LinearLayout>

And finaly your activity:

public class MyActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activitty);

    final List<String> list = new ArrayList<String>();
    list.add("test");
    list.add("test");
    list.add("test");

    final CustomAdapter adapter = new CustomAdapter(this, list);
    final ListView listView = getListView();
    listView.setAdapter(adapter);
}


public class CustomAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList;

    public CustomAdapter(Context context, List<String> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public String getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Holder holder;

        if (convertView == null) {

            // if it is the first time you create the row
            // you get the layout of each row here
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);

            // you keep your layout in a holder
            holder = new Holder();
            holder.mText = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);

        } else {

            // if the row has already been created, you get it from the holder
            holder = (Holder) convertView.getTag();
        }

        // you do what you want with the content

        holder.mText.setText(getItem(position));
        holder.mText.setTextColor(Color.BLUE);

        return convertView;
    }

    private class Holder {
        public TextView mText;
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM