Android ListView:如何以编程方式对hover / hightlight做出反应?

时间:2018-02-14 10:06:16

标签: android listview onclicklistener onfocuschangelistener

正在使用Android SDK 17+应用。 ListView使用BaseAdapter实例来管理其项目。每个项目都是一个简单的布局,只包含TextViewTextView使用其CompoundDrawable来显示图标。

如果用户点击并按住列表项,则该图标应更改为更大胆的版本。我怎么能这样做?

如果列表项的突出显示/焦点发生变化,如何更新视图?

虽然可以使用selector drawables轻松更新焦点更改时的文本颜色和背景,但这对于图标是不可能的,因为它在代码(Java)中设置并且未在布局中指定(XML) 。

活动

@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;
}

那么,我如何应对突出显示更改以更新图标?

1 个答案:

答案 0 :(得分:0)

我已经在这个演示中完成了它(100%工作)

首先做这样的演示

JAVA文件

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文件

<强> 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" />

如果有任何问题可以免费提问