Hello dear Android developers.
I have a problem with focus on items of ListView after calling
notifyDataSetChanged() on its adapter.
Could you please help me with it?
Problem description:
I need to show ListView with custom items.
Every of such items has 2 Views in it. One on the left side and other
- on the right side.
Both Views are focusable. I need to change selected View of item in
ListView with navigation hard keys.
Also I need to update data in adapter periodically.
I decided to use method notifyDataSetChanged() of the adapter.
Expected result:
After calling this method View that was focused before calling stay
focused.
All data is updated.
Actual result:
After calling this method the left View of the item becomes focused.
Еven if before calling the right View was focused.
All data is updated.
Could you please suggest me any ways to solve my problem?
My code is below.
Thanks in advance,
Dmitry
-------------------My code:------------------
TestActivity.java:
"
package demon.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ListView;
public class TestActivity extends Activity implements OnClickListener
{
CustomAdapter arrayAdapter;
OnFocusChangeListener focusListener = new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.RED);
} else {
v.setBackgroundColor(Color.BLACK);
}
}
};
@Override
public void onClick(View v) {
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = new ListView(this);
arrayAdapter = new CustomAdapter(this, R.layout.item);
for (int i = 0; i < 3; i++) {
LayoutInflater inflater = getLayoutInflater();
View result = inflater.inflate(R.layout.item, null);
result.findViewById(R.id.item_text1).setOnClickListener
(this);
result.findViewById(R.id.item_text2).setOnClickListener
(this);
result.findViewById(R.id.item_text1).setOnFocusChangeListener
(focusListener);
result.findViewById(R.id.item_text2).setOnFocusChangeListener
(focusListener);
arrayAdapter.add(result);
}
listView.setAdapter(arrayAdapter);
listView.setItemsCanFocus(true);
setContentView(listView);
}
}
"
CustomAdapter.java:
"
package demon.test;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class CustomAdapter extends ArrayAdapter<View> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
View result = getItem(position);
return result;
}
}
"
item.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="25px"
android:gravity="center_vertical|center_horizontal">
<TextView
android:focusable="true"
android:id="@+id/item_text1"
android:textSize="16sp"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Bar text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:nextFocusRight="@+id/item_text2"/>
<TextView
android:focusable="true"
android:id="@+id/item_text2"
android:textSize="16sp"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Bar text 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:nextFocusLeft="@+id/item_text1"/>
</LinearLayout>
"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---