当l向下滚动listview自动另一个项目列表bgcolor设置时,单击特定列表项目设置bgcolor

时间:2016-08-27 09:39:56

标签: android listview listviewitem

我创建列表视图程序和特定列表项单击以设置backgroundcolor列表,当向下滚动列表视图时自动设置颜色其他项。请建议我的代码。

以下示例代码。

public class MainActivity extends Activity {

ListView lvCountry;

ArrayList<Integer> list = new ArrayList<Integer>();
String[] country = { "India", "USA", "Russsia", "China", "Pakistan",
        "Canada", "UK", "arcot", "vellore", "gudiyattam", "arani",
        "palani", "chennai", "padi", "velacherry", "ambattur",
        "ambatttur ot", "maduravoyal", "guindy" };

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

    lvCountry = (ListView) findViewById(R.id.listView1);
    // Array adapter
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            MainActivity.this, android.R.layout.simple_list_item_1, country);
    lvCountry.setAdapter(arrayAdapter);

    // list selection part
    lvCountry.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // color selection select item
            arg1.setBackgroundColor(Color.GRAY);
        }
    });
}}

1 个答案:

答案 0 :(得分:0)

试试这个。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:background="@color/black_30">


    <android.support.v7.widget.Toolbar
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/toolbarAnnouncement"
        android:theme="@style/ToolBarStyle"
        android:elevation="5dp"
        android:textAlignment="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title"
            android:textSize="20sp"
            android:textStyle="bold"
            android:background="#00000000"
            android:textColor="#ffffff" />


    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="15"
        android:id="@+id/channelContainer"
        android:orientation="horizontal"
        android:layout_below="@+id/toolbarAnnouncement"
        android:background="@color/material_grey_50">


        <TextView
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="To :"
            android:id="@+id/textView16"
            android:textSize="20sp"
            android:paddingLeft="5dp"
            android:layout_gravity="center_vertical" />


    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="11"
        android:id="@+id/chipViewLayout"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingTop="10dp">

        <com.plumillonforge.android.chipview.ChipView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/buttonChannelSelectorAnnouncement"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="left"
            android:padding="5dp"
            android:minHeight="90dp" />


    </RelativeLayout>


        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:id="@+id/buttonRemoveChannel"
            android:background="@android:drawable/ic_menu_close_clear_cancel"
            android:visibility="gone"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dp" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/attacheImage1"
        android:layout_below="@+id/channelContainer"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="#ffffff"
        android:visibility="gone">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:id="@+id/imageViewAttach1"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="5dp"
            android:background="#ffffff"
            android:src="@drawable/location" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Attaching..."
            android:id="@+id/textViewAttach1"
            android:layout_alignParentBottom="false"
            android:layout_toRightOf="@+id/imageViewAttach1"
            android:layout_toEndOf="@+id/imageViewAttach1"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="5dp"
            android:layout_centerInParent="true" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Remove"
            android:id="@+id/buttonRemoveAttach1"
            android:background="@drawable/rounded_corner_filled"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="5dp" />

        <Button
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Retry"
            android:id="@+id/buttonRetry1"
            android:textColor="#ffffff"
            android:layout_centerVertical="true"
            android:background="@drawable/rounded_corner_filled"
            android:layout_toLeftOf="@+id/buttonRemoveAttach1"
            android:layout_toStartOf="@+id/buttonRemoveAttach1"
            android:layout_marginRight="5dp"
            android:visibility="gone" />

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/attacheImage2"
        android:layout_below="@+id/attacheImage1"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="#ffffff"
        android:visibility="gone">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:id="@+id/imageViewAttach2"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="5dp"
            android:background="#ffffff"
            android:src="@android:drawable/sym_def_app_icon" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Attaching..."
            android:id="@+id/textViewAttach2"
            android:layout_alignParentBottom="false"
            android:layout_toRightOf="@+id/imageViewAttach2"
            android:layout_toEndOf="@+id/imageViewAttach2"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="5dp"
            android:layout_centerVertical="true" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Remove"
            android:id="@+id/buttonRemoveAttach2"
            android:background="@drawable/rounded_corner_filled"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="5dp" />

        <Button
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Retry"
            android:id="@+id/buttonRetry2"
            android:textColor="#ffffff"
            android:background="@drawable/rounded_corner_filled"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/buttonRemoveAttach2"
            android:layout_toStartOf="@+id/buttonRemoveAttach2"
            android:layout_marginRight="5dp"
            android:visibility="gone" />


    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/attacheImage3"
        android:layout_below="@+id/attacheImage2"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="#ffffff"
        android:visibility="gone">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:id="@+id/imageViewAttach3"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="5dp"
            android:background="#ffffff"
            android:src="@drawable/icon_admin" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Attaching..."
            android:id="@+id/textViewAttach3"
            android:layout_alignParentBottom="false"
            android:layout_toRightOf="@+id/imageViewAttach3"
            android:layout_toEndOf="@+id/imageViewAttach3"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="5dp"
            android:layout_centerVertical="true" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Remove"
            android:id="@+id/buttonRemoveAttach3"
            android:background="@drawable/rounded_corner_filled"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="5dp" />

        <Button
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:text="Retry"
            android:id="@+id/buttonRetry3"
            android:layout_centerVertical="true"
            android:textColor="#ffffff"
            android:background="@drawable/rounded_corner_filled"
            android:layout_toLeftOf="@+id/buttonRemoveAttach3"
            android:layout_toStartOf="@+id/buttonRemoveAttach3"
            android:layout_marginRight="5dp"
            android:visibility="gone" />

    </RelativeLayout>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:inputType="textMultiLine"
        android:ems="10"
        android:fitsSystemWindows="true"
        android:id="@+id/editTextDescriptionAnnouncement"
        android:hint="Type your announcement..."
        android:gravity="top"
        android:padding="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/attacheImage3"
        android:background="#ffffff"
        android:layout_above="@+id/relativeLayout4" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="@color/button_material_light"
        android:id="@+id/relativeLayout4">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:text="Image"
            android:id="@+id/buttonAddImage"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:text="Send"
            android:id="@+id/buttonSendAnnouncement"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />


    </RelativeLayout>

</RelativeLayout>