简体   繁体   中英

How to make 1st item focus in ListView by default whenever enter the list View screen?

In my list View ,i have to make 1st item to be focused.In my app i have done D-PAD implementation,If i found the initial focus ,i will navigate the focus by using D-PAD. i tried with setSelection(position) this code is not working.

I found some strange behaviour of listview,by default it always focus -1 position.I dont know why?,please clarify this behaviour.

My list view items are get load from server.So,when i have to make setSelection() and how to check the setAdpter task completed or not?.

@Override
    public final View getView(final int position, final View convertView, final ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View rowView = inflater.inflate(R.layout.list_item_faq, parent,
                false);
        final TextView textView = (TextView) rowView
                .findViewById(R.id.faqlist_text);
        textView.setText("text");
        textView.setMaxLines(2);
        textView.setEllipsize(android.text.TextUtils.TruncateAt.END);
        return rowView;
    }

adapter.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/sel_list_item" //this is selector
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:layout_margin="2dp"
            android:layout_gravity="center"
            android:src="@drawable/cc_question_mark" />

        <TextView
            android:id="@+id/faqlist_text"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:gravity="center_vertical"
            android:textColor="#6f6f6f"
            android:textSize="22sp" />
</LinearLayout>

Please suggest any idea to make 1st item Focus.

Is your yourListView focusable ?

Do in onResume()

    yourListView.postDelayed(new Runnable() {
    @Override
    public void run() {
        yourListView.setSelection(0);
        yourListView.getChildAt(0).requestFocus()
    }
}, 500);

Try doing the following:

public void onStart() {
    super.onStart();
    onListItemClick(listView, findViewById(android.R.id.content), 1, 0);
}

public void setActivateOnItemClick(boolean activateOnItemClick) {
    listView.setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
            : ListView.CHOICE_MODE_NONE);
}

You might need to give a little bit of delay before using set selection.

listView.postDelayed(new Runnable() {
    @Override
    public void run() {
        listView.setSelection(0);
    }
}, 500);

if you already try list.setSelection(position) then try

list.setItemChecked(0, true);

I hope it works for you. but remember, update this in main thread or ui thread

UPDATE

also try this

list.setFocusable(true);
list.requestFocus();

UPDATE 1

When u get all list from data then call

adapter.notifyDataSetChanged();
list.setSelection(position);

Try this

listView.getAdapter().getView(position, null, null).performClick();

Could you show me code getview method in Adapter? I think you set static viewholder in Adapter. You should delete if(convertView == null) condition. Look like:

        // if (convertView == null) {

        holder = new ViewHolder();
        convertView = mInflater.inflate(
                R.layout.baseadapter_dropdown_layout, null);
        holder.img = (ImageView) convertView.findViewById(R.id.imageView1);
        convertView.setTag(R.layout.baseadapter_dropdown_layout, holder);

        // } else {
        // holder = (ViewHolder) convertView
        // .getTag(R.layout.baseadapter_dropdown_layout);
        // }

In the onResume() go for

mListView.setSelection(position);

or

mListView.setSelection(listview.getAdapter().getCount()-1);

also

mListView.requestFocus();

also try to manage null check before entering hard coded positions.

Use performItemClick. It will call the OnItemClickListener, if it is defined. This works in charm for me.

int setPosition = 0; // Your default position. Any value less than your list size.
int ignoreId = 0; // We ignore the row id of the item that was clicked.
yourListView.setItemChecked(setPosition, true);
yourListView.performItemClick(yourListView.getSelectedView(), setPosition,   ignoreId);

没有必要使用list with listview,只需在将数据添加到列表后使用listView.setSelection(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM