开发者

set focus on any item of listview in android

开发者 https://www.devze.com 2023-01-06 01:16 出处:网络
I have a listview which contains textviews as its elements. Now I want the first item of the list to be automatically focused when I launch the application

I have a listview which contains textviews as its elements.

  1. Now I want the first item of the list to be automatically focused when I launch the application
  2. How can I set 开发者_StackOverflowthe focus on any item of the list when I click on the some other view for example a button?


Setting selection and setting focus are two different things. If you want to just setSelection to some item then you can use below code.

    mListView.setSelection(position);

But this definitely does not mean the Listview is focused.For focussing you have to use

    mListView.requestFocus();

For changing focus on click of a button you can place the code on onClick() of the button.


ListView has a setSelected method that takes the index of the item in the list.


for me the problem was solved by

listView.setItemsCanFocus(true);


I guess I was in the same situation. I wanted to be able to control the focus of the listview programmatically with buttons .

One solution is to deal with the setFocusableInTouchMode, but I've never achieved to make it work.

The other solution is to forget about the focus and use a checkable listview. First set your listview to "single choice mode" in XML or in java : Mylistview.setChoiceMode(1)

Then you'll be able to check any item you want with Mylistview.setItemChecked(position, true)

So when you lunch the application (OnCreate), use Mylistview.setItemChecked(0, true) to check your first item.

Then if you want your button to select the next item for exemple, use :

Mylistview.setItemChecked(Mylistview.getCheckedItemPosition() + 1, true)

You can specify the look when the item is checked or not and there's different pre-built chekable listviews.

If you want more explanations, see my post


  1. Your ListView should be like this:

    <ListView
        android:id="@+id/my_list"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_margin="10dp"
        android:choiceMode="none"
        android:focusable="true"
        android:fadeScrollbars="false">
        <requestFocus />
    </ListView>
    
  2. mListView.setSelection(3); //or any other number


I know this is an old question, but in my case I was trying to programmatically focus a menu option in a listView to highlight the item and none of these worked for me. This is what worked:

MenuOption model:

public class MenuOption {

    public static final int ALL_ITEMS = 0;
    public static final int CATEGORIES = 1;
    public static final int DISCOUNTS = 2;

    private int id;
    private String name;
    private String description;
    private boolean isFocused;

    public MenuOption(int id, String name, String description, boolean isFocused) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.isFocused = isFocused;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isFocused() {
        return isFocused;
    }

    public void setFocused(boolean focused) {
        isFocused = focused;
    }
}

MenuAdapter:

public class MenuAdapter extends BaseAdapter {

    private ArrayList<MenuOption> menuOptions; // Original Values
    private LayoutInflater inflater;
    private MenuOptionListener listener;
    private Context context;

    public MenuAdapter(Context context, ArrayList<MenuOption> menuOptions, MenuOptionListener listener) {
        this.context = context;
        this.menuOptions = menuOptions;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    private class ViewHolder {
        TextView menuName;
    }

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

        ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_menu_option, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.menuName = convertView.findViewById(R.id.menu_name);

        MenuOption option = menuOptions.get(position);
        holder.menuName.setText(option.getName());
        if (option.isFocused()) {
            convertView.setBackgroundResource(R.color.menu_focus);
            holder.menuName.setTextColor(ContextCompat.getColor(context, R.color.white));
            holder.menuName.setTypeface(null, Typeface.BOLD);
        } else {
            convertView.setBackgroundResource(R.color.menu_un_focus);
            holder.menuName.setTextColor(ContextCompat.getColor(context, R.color.black));
            holder.menuName.setTypeface(null, Typeface.NORMAL);
        }
        convertView.setOnClickListener(view -> {
            if (!option.isFocused()) {
                resetFocus(position);
                listener.onClicked(option);
            }
        });

        return convertView;

    }

    public void resetFocus(int position) {
        for (int i = 0; i < menuOptions.size(); i++) {
            menuOptions.get(i).setFocused(i == position);
        }
        notifyDataSetChanged();
    }

    public interface MenuOptionListener {
        void onClicked(MenuOption option);
    }
}

And then in your Activity, you can call menuAdapter.resetFocus(0).

0

精彩评论

暂无评论...
验证码 换一张
取 消