Changing view attributes in a list item

I have a ListView that contains custom rows. This user line has the following user interface elements

  • ImageView imageView1, imageView2
  • TextView textview1, textView2, textView3

The requirement is always when the list row is selected, there will be the following changes

imageView1 background, color changed

Changed the color of imageView1

textview1 color, size and font are changes

textview2 color, resized

textview3 color, resized

What would be the best way to do this?

AFAIK we cannot apply styles in the selector. Is there a better way to work on this rather than being processed in Java code?

We have a setOnItemSelectedListener that can be set in a Listview that will have the following callback methods:

i) onItemSelected

ii) onNothingSelected

, , . ?

.

+5
4

, Compound Control. SDK samples/ApiDemos ( Android-, ).

, , , . , a LinearLayout. , LinearLayout .

setSelected .

, :

public class MyCustomLayout extends LinearLayout {
    public ImageView imageView1;   // These are public since you likely want
    public ImageView imageView2;   // to set them in your Adapter.
    public TextView textView1;
    public TextView textView2;
    public TextView textView3;

    public MyCustomLayout(Context ctxt) {
        super(ctxt);

        // The call below attaches the items in the mycustomlayout.xml file to this
        // instance, which serves as the root element in the layout.
        LayoutInflater.from(ctxt).inflate(R.layout.mycustomlayout, this, true);

        imageView1 = (ImageView)findViewById(R.id.imageview1);
        imageView2 = (ImageView)findViewById(R.id.imageview2);
        textView1 = (TextView)findViewById(R.id.textview1);
        textView2 = (TextView)findViewById(R.id.textview2);
        textView3 = (TextView)findViewById(R.id.textview3);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setChecked(selected);
        if (selected) {
            imageView1.setBackgroundColor(0xFFFF0000); // whatever color you want
            // Do all the rest of the setting of your subviews here for
            // the case when the row is selected.
        } else {
            imageView1.setBackgroundColor(0xFF000000); // whatever color you want
            // Do all the rest of the setting of your subviews here for
            // the case when the row is not selected
        }
    }
}

, mycustomlayout.xml, <merge>, :

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/imageview1" />
    <ImageView
        android:id="@+id/imageview2" />
    <TextView
        android:id="@+id/textview1" />
    <TextView
        android:id="@+id/textview2" />
    <TextView
        android:id="@+id/textview3" />
</merge>

subviews , , , XML . , XML LayoutInflater. Android dev, .

getView MyCustomLayout ( ), , .

+2

... ... , ... .. , ...

0

selectedPos .

onItemSelected selectedPos = position onNothingSelected -1 notifyDataSetChanged() .

ListView. getView

if(position == selectedPos)
{
   // set color and background to view
}

,

@Override
public int getItemViewType(int position) {
 return position;
}

@Override
public int getViewTypeCount() {
 return _ArrayListObject.size();
}
0

Khotmanish,

, . , , , , . , , , . Google .

, StateListDrawable .

StateListDrawable, :

StateListDrawable _drawStates;

:

_drawStates.add(new int[] {android.R.attr.state_pressed}, _newImage);

, getView() :

v.setBackgroundDrawable(_drawStates);

. OnListItemClick(). , OnListItemClick() , . id invalidate() . . , . . :

@Override protected void onListItemClick(final ListView inList, final View v, final int atPos, final long rowID)
{//Get the Child, set the properties
    ImageView _img = (ImageView)(v.findViewById(R.id.app_package));
    _img.setBackgroundColor(myClickedColor);
    _img.invalidate();  // <---- BEST Option
// ... OR ...
    v.invalidate();     // <---- 2nd Best Option
// ... OR ...
    inList.invalidate();  // <---- Last resort or specialized implementation
}

, , . Clicked View... ListView. -, . , "stateful" , ( ).

Now a more popular but less flexible approach is to create your StateListDrawable in XML. There are several resources to learn how to do this on the Android developer website.

Hope this helps, FuzzicalLogic

0
source

All Articles