Home Android ListView: How to react to hover/hightlight programmatically?
Reply: 1

Android ListView: How to react to hover/hightlight programmatically?

Andrei Herford
1#
Andrei Herford Published in 2018-02-14 10:06:15Z

Am working on a Android SDK 17+ app. A ListView uses a BaseAdapter instance to manage its items. Each item is a simple layout which only includes a TextView. The TextView uses its CompoundDrawable to show an icon.

If the user taps and holds a list item, the icon should change to a bolder version. How can I do this?

How can I update the view when the highlight/focus of the list item changes?

While the text color and the background can easily be updated on focus changes by using selector drawables, this is not possible for the icon since it set in code (Java) and not specified in the layout (XML).

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    listView= (ListView)findViewById(R.id.listView);

    MyListAdapter listAdapter = new MyListAdapter(this);
    listView.setAdapter(listAdapter );

    // These only fire when a an item is selected (clicked and released)
    // and not when highlight changes (clicked but not released)
    listView.setOnItemClickListener(...);
    listView.setOnFocusChangeListener(...);
    listView.setOnItemSelectedListener(...);
    ...
}

private static class MyListAdapter extends BaseAdapter {
    ...

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.listitem__textItem, null);

            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView)convertView.findViewById(R.id.textView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        DataItem dataItem = getDataItemForIndex(position); 
        viewHolder.textView.setText(dataItem.getText()); 

        int iconResID = dataItem.getNormalIconId();
        viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(0, iconResID , 0, 0);
        ...
}

static class ViewHolder {
    TextView textView;
}

So, how can I react to highlight changes to update the icon?

SahdevRajput74
2#
SahdevRajput74 Reply to 2018-02-14 12:20:43Z

I have done it in this demo SEE IT (100% work)

FIRST MAKE DEMO LIKE THIS

JAVA FILE

public class MainActivity extends AppCompatActivity {

ListView listview;

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

    listview = (ListView) findViewById(R.id.listView1);

    // 2 arraylist
    ArrayList<String> days = new ArrayList<String>();
    ArrayList<String> compoundDrawable = new ArrayList<String>();

    // populate days
    days.add("Sunday");
    days.add("Monday");
    days.add("Tuesday");
    days.add("Wednesday");
    days.add("Thursday");
    days.add("Friday");
    days.add("Saturday");

    // compundDrawble value for chnage it
    for (int i = 0; i < days.size(); i++) {
        compoundDrawable.add("0");
    }

    // set adapter
    final myAdpater adpt = new myAdpater(days, compoundDrawable);
    listview.setAdapter(adpt);

    // listview touchEvent
    listview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {

            if (arg1.getAction() == MotionEvent.ACTION_DOWN) {

                adpt.Bold();

            }
            if (arg1.getAction() == MotionEvent.ACTION_UP) {

                adpt.Simple();
            }

            return false;
        }
    });
}

private class myAdpater extends BaseAdapter {
    ArrayList<String> days;
    ArrayList<String> compoundDrawable;
    int pos = 0;
    Holder holder;

    class Holder {
        TextView textview;
    }

    public myAdpater(ArrayList<String> days,
            ArrayList<String> compoundDrawable) {
        this.days = days;
        this.compoundDrawable = compoundDrawable;

    }

    @Override
    public int getCount() {

        return this.days.size();
    }

    @Override
    public Object getItem(int position) {
        this.days.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    public void Bold() {

        compoundDrawable.set(pos, "1");
        notifyDataSetChanged();
    }

    public void Simple() {

        compoundDrawable.set(pos, "0");
        notifyDataSetChanged();
    }

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

        holder = new Holder();
        LayoutInflater inf = (LayoutInflater) getApplicationContext()
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inf.inflate(R.layout.raw_file, null);

            holder.textview = (TextView) convertView
                    .findViewById(R.id.textView1);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.textview.setText(this.days.get(position));

        if (compoundDrawable.get(position).equals("0")) {
            holder.textview.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.round, 0, 0, 0);

        } else {
            holder.textview.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.round2, 0, 0, 0);
        }

        holder.textview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                pos = position;

                return false;
            }
        });

        return convertView;
    }

}

}

XML FILES

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.de3mo.MainActivity" >

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

raw_file.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/ll"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="#000"
    android:padding="10dp"
    android:text="Good Afternoon" />

If any quetion free to ask

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311887 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO