Layout problems when opening the keyboard on android - android

I'm having problems with the layout of a chat activity on android. I'm doing the tests in a simple application, to make it easier for you to understand.
This is the xml layout:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toTopOf="parent">
<ListView
android:id="#+id/listViewMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="7dp" />
<LinearLayout
android:id="#+id/linearLayoutCaixaTexto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="8dp"
android:layout_weight="0"
android:background="#drawable/fundo_mensagem_chat_branco"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="16dp">
<EditText
android:id="#+id/editMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00000000"
android:cursorVisible="true"
android:ems="10"
android:hint="Type here..."
android:imeOptions="actionDone"
android:inputType="textPersonName"
android:linksClickable="true"
android:longClickable="true"
android:maxLength="500"
android:paddingRight="5dp"
android:singleLine="true"
android:soundEffectsEnabled="false"
android:textColor="#android:color/black"
android:textStyle="normal" />
<ImageView
android:id="#+id/imageViewSendMessage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
ads:srcCompat="#drawable/ic_enviar_preto_24dp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
These are the problems, all with respect to the keyboard:
When you click on EditText, the keyboard opens normally, fast. But when I add items to the listView (so that it fills the entire screen), when clicking on EditText, the keyboard takes a while to open, and also to hide, it gets a little stuck. This is a very annoying problem.
When the keyboard opens, the background in ConstraintLayout is changed, you can see the difference in the images below. I would like the keyboard to be above the background, just like chat apps, when we put a background image of the conversation. The closest I got was putting "adjustNothing" in the windowSoftInputMode, but then the keyboard was over EditText and ListView.
See, if I use "adjustPan" in windowSoftInputMode, this happens:
In short, I would like it to look like this:
The keyboard should open and hide quickly.
EditText must always be above the keyboard (this already happens).
The background image of the conversation should not be changed when opening the keyboard.
I recorded a short video so you can see the problem. Notice in the video that when there are no messages in the listView, the keyboard opens and closes fast, but when there are messages, it is slow, stuck. And also notice the background, when you open the keyboard it is changed. At the end of the video I recorded the same situation in another app, there you can see that the keyboard opens normally and the wallpaper is not changed.
Video: https://www.youtube.com/watch?v=DdMFufZ_Glg&feature=youtu.be
Other codes (if needed):
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.applicationtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="Chat"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity:
public class MainActivity extends AppCompatActivity {
private List<Message> listMessages = new ArrayList<>();
private EditText editMessage;
private ImageView imageViewSendMessage;
private MessageAdapter adapterMessages;
private ListView listViewMessages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editMessage = findViewById(R.id.editMessage);
imageViewSendMessage = findViewById(R.id.imageViewSendMessage);
listViewMessages = findViewById(R.id.listViewMessages);
adapterMessages = new MessageAdapter(listMessages, this);
listViewMessages.setDividerHeight(0);
listViewMessages.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
listViewMessages.setAdapter(adapterMessages);
imageViewSendMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!editMessage.getText().toString().isEmpty()) {
Message message = new Message();
message.setM(editMessage.getText().toString());
message.setDm("20/08/2022");
listMessages.add(message);
adapterMessages.notifyDataSetChanged();
}
}
});
}
}
Adapter Class:
public class MessageAdapter extends ArrayAdapter<Message> {
private Context context;
private List<Message> mensagens;
public MessageAdapter(List<Message> list, Context c) {
super(c, 0, list);
this.context = c;
this.mensagens = list;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
Message message = mensagens.get(position);
listItem = LayoutInflater.from(context).inflate(R.layout.adapter_msg_rement,parent,false);
TextView textMensagem;
TextView dataMensagem;
textMensagem = listItem.findViewById(R.id.textMensagemTexto);
dataMensagem = listItem.findViewById(R.id.dataMensagem);
textMensagem.setText(message.getM());
dataMensagem.setText(message.getDm());
textMensagem.setTextColor(Color.parseColor("#000000"));
return listItem;
}
}

ListView attempts to reuse view objects in order to improve performance and avoid a lag in response to user scrolls. To take advantage of this feature, check if the convertView provided to getView(...) is null before creating or inflating a new view object. See Making ListView Scrolling Smooth for more ways to ensure smooth user experience.
As mentioned above you should take care of the performance by using convertView
I'm guessing that's what causing your "little stuck" issues.
As that said I would recommend using RecyclerView
About the background image - you can use adjustPan to push the whole layout above and avoid resizing the image (hope that this is what you wanted...)
Enjoy and Good Luck

Related

How can I enable split-screen programmatically from inside my android app through on click button

This bounty has ended. Answers to this question are eligible for a +50 reputation bounty. Bounty grace period ends in 6 hours.
M. Usman Khan wants to draw more attention to this question:
Please somebody tell us how to trigger multi-window. Specially for Android 13 and above
My app contains a button through that button just want to trigger split screen functionality
Try to use setSplitScreenState(boolean) to trigger split screen functionality
Example
inside button
onClick{
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
if(manager != null){
manager.setSplitState(true);
}
}
R.layout.activity_main (xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:onClick="startNewActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="297dp" />
</android.support.constraint.ConstraintLayout>
R.layout.activity_second (xml)
(Just a textView to keep it simple)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:text="Second Activity" />
</LinearLayout>
Then your MainActivity launch second activity from first activity when Start button is clicked and set Flags. Rect takes care of the switching in division and activity option takes care of the transiting animation;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startNewActivity(View view) {
Intent i = new Intent(this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
Rect rt = new Rect(0, 0, 100, 100)
ActivityOptions actoptions = ActivityOptions.makeBasic();
ActivityOptions bounds = actoptions.setLaunchBounds(rt);
startActivity(i);
}
}
For this example, nothing I am going to leave the second activity as is.
SecondActivity
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Finally and most importantly MainActivity.this in your manifest.xml file should look like this;
<activity android:name=".MainActivity"
android:resizeableActivity="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To switch into multi window mode long press on the overview button and drag the app to one side of the screen
That's all.

Why is my AutoCompleteTextView not working?

This feels strange as normally the widgets work as they are supposed to. I have an AutoCompleteTextView that I want to populate with a list of city names. It seems simple but doesn't work as I intend to. Here is the resulting output:
Here is the layout in picture:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity1">
<AutoCompleteTextView
android:id="#+id/autocomptv_city_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/bg_edittext_rect_opd"
android:hint="Select City"
android:text=""
android:inputType="text"
android:maxLines="1"
android:completionThreshold="0"
android:padding="10dp"
android:singleLine="true"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
And below is the java code for the same:
public class MainActivity1 extends AppCompatActivity {
AutoCompleteTextView mAutCompleteTextViewSelfCity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
mAutCompleteTextViewSelfCity = ((AutoCompleteTextView) findViewById(R.id.autocomptv_city_list));
setupCityDropdownwidget();
}
private void setupCityDropdownwidget() {
Type listType = new TypeToken<List<CityName>>() {}.getType();
List<CityName> citiesList = Singletons.getGsonInstance().fromJson(TestData.cityDataJson, listType);
CityArrayAdapter adapter = new CityArrayAdapter(this, R.layout.item_spinner_city, citiesList);
mAutCompleteTextViewSelfCity.setAdapter(adapter);
adapter.notifyDataSetChanged();
mAutCompleteTextViewSelfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CityName selectedCitySelf = ((CityName) parent.getItemAtPosition(position));
mAutCompleteTextViewSelfCity.setText(selectedCitySelf.getCityName());
}
});
}
}
Problem:
Well, I want the view to be such that as soon as the user taps on it, it shows a dropdown of cities and when the user starts typing their city for filtering, the view keeps showing narrowed down suggestions for the same.
Currently, the only time it is able to suggest is when I type in something and empty out the text view. If I change the completion threshold to 1, no suggestions are shown ever.
What's wrong with my code?
Here is the complete source for reference: https://wetransfer.com/downloads/ce4017f5f2488288ef7494dc029e033420191019092536/7afa9a3e64afb257293533bd634d6c3220191019092536/dc2341
So ultimately, it turned out to be about the basics - The data item that ArrayAdapter works with, should provide a meaningful toString() override. That is what had been missing from my implementation.
From the docs:
By default, the array adapter creates a view by calling
Object#toString() on each data object in the collection you provide,
and places the result in a TextView.
I did end up wasting up some time but the experience and knowledge would surely come in handy some day.
YOu don't need to set adapter.notifyDataSetChanged();, just set your adapter and everything goes fine.

Binding event not firing on Adapter

I am following Android Archt. component to build a project. Following the guidelines I have created a custom Adapter named CataloguesAdapter extending DataBoundListAdapter as :
public class CataloguesAdapter extends DataBoundListAdapter<CatalogueEntity, CatalogueItemBinding> {
private final android.databinding.DataBindingComponent dataBindingComponent;
private final ContributorClickCallback callback;
private CatalogueItemBinding mBinding;
public CataloguesAdapter(DataBindingComponent dataBindingComponent,
ContributorClickCallback callback) {
this.dataBindingComponent = dataBindingComponent;
this.callback = callback;
}
#Override
protected CatalogueItemBinding createBinding(ViewGroup parent) {
mBinding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()),
R.layout.catalogue_item, parent, false,
dataBindingComponent);
//while this click event is working fine
mBinding.getRoot().setOnClickListener(v -> {
CatalogueEntity catalogueEntity = mBinding.getCatalogue();
if (catalogueEntity != null && callback != null) {
callback.onClick(catalogueEntity);
}
});
//todo:not working, this event is not firing
mBinding.deleteIcon.setOnClickListener(v-> callback.onItemDelete());
return mBinding;
}
}
I am implementing swipe to delete layout on Recycler view item. Below is the XML layout of list item:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="catalogue"
type="com.mindtree.igxbridge.traderapp.datasource.local.entity.CatalogueEntity" />
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/view_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorRed">
<ImageView
android:id="#+id/delete_icon"
android:layout_width="#dimen/dimen_30_dp"
android:layout_height="#dimen/dimen_30_dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/dimen_10_dp"
app:srcCompat="#drawable/ic_delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/dimen_10_dp"
android:layout_toStartOf="#id/delete_icon"
android:text="#string/text_delete"
android:textColor="#color/Material.87.white"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/view_foreground"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/arrow_icon"
android:layout_width="#dimen/dimen_30_dp"
android:layout_height="#dimen/dimen_30_dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/dimen_10_dp"
app:srcCompat="#drawable/ic_arrow_right" />
</RelativeLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
</layout>
Another operation like swipe left/right is working fine but clicking on Delete button event is not getting called.
I tried checking findViewbyId and register click event by that, but no luck with that too.
While CatalogueItemBinding is registered correctly, I am not able to find any other source of error.
Thanks.
Correct me if I have misunderstood your code. You used a FrameLayout to host two relative layouts one top of each other (foreground and background). The delete button is in the background and foreground has match_parent in its width attribute. Therefore, I think the delete button is getting covered by the foreground, leading to "not firing of the event".
Possible Solution
Try incorporating the delete button in the foreground. It makes sense to put UI components in the front.
I think you forget to tell your adapter class to where your XML is set or not to adapter class. just create a variable in XML which will import your adapter class have look.
<variable
name="myAdapter"
type="import your adapter class">
</variable>
Now set this variable to your adapter.
#Override
protected CatalogueItemBinding createBinding(ViewGroup parent) {
mBinding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()),
R.layout.catalogue_item, parent, false,
dataBindingComponent);
mBinding .setmyAdapter(this);
return mBinding;
}
}
then your click will work. Hope it will help you.

AutocompleteTextView suggestion list goes up

possible duplicates
Android : autocompletetextview, suggestion list displays above the textview?
I am fully trying to display suggestion list overlapping on keyboard when suggestion list scroll by user but it always open up side.
here I am getting this way
here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SuggestionListActivity"
android:windowSoftInputMode="adjustResize|adjustPan|stateHidden">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
here is my main.xml file
<?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="fill_parent"
android:orientation="vertical" android:padding="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_margin="10dp"/>
<TextView android:layout_margin="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is testing for the auto complete textview in this application to display suggestion list overlapping on keyboard." />
<AutoCompleteTextView android:id="#+id/autocomplete"
android:layout_width="fill_parent" android:layout_margin="5dp"
android:layout_height="wrap_content" android:hint="Search"
android:layout_marginLeft="5dp" android:dropDownHeight="300dp"
android:inputType="textAutoComplete" android:singleLine="true"
/>
</LinearLayout>
what to do in this code to display the suggestion over keyboard when list was focus.
I've had this problem before. For me, there was more screen space above the AutocompleteTextView than below (testing on a "normal" device), so the list opened upwards. I adjusted my layout slightly so that there was more space below the AutocompleteTextView and it started opening downwards. That's what fixed it for me.
You can either adjust the layout so that there is more space below the AutoCompleteTextView
or
you can change the dropdown height android:dropDownHeight and set some high value,
this would work when its inside a scrollView and the AutoCompleteTextView is near the top.
To display the list of options on focus do something like this
autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
autoCompleteTextView.showDropDown();
}
}
});
This would display a list of options when the user focuses on the AutoCompleteTextView
The trick is to ensure that the desired drop-down height is never larger than the available space below. My approach is to create a subclass that overrides showDropDown:
public class DownOnlyAutoCompleteTextView extends AppCompatAutoCompleteTextView {
private final static int MINIMAL_HEIGHT = 50;
public DownOnlyAutoCompleteTextView(Context context) {
super(context);
}
public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void showDropDown() {
Rect displayFrame = new Rect();
getWindowVisibleDisplayFrame(displayFrame);
int[] locationOnScreen = new int[2];
getLocationOnScreen(locationOnScreen);
int bottom = locationOnScreen[1] + getHeight();
int availableHeightBelow = displayFrame.bottom - bottom;
if (availableHeightBelow >= MINIMAL_HEIGHT) {
setDropDownHeight(availableHeightBelow);
}
super.showDropDown();
}
}
Then use this in your layout, e.g.:
<your.package.DownOnlyAutoCompleteTextView
android:id="#+id/auto_complete_text_view"
android:layout_margin="12dp"
android:hint="AutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="HardcodedText" />
Adjust MINIMAL_HEIGHT to fit your requirements -- if there's no or very little space below, it's probably better not to force the issue.
EDIT
As mentioned in the comments, passing a negative number to setDropDownHeight will trigger an exception in some Android versions. As long as you define a MINIMAL_HEIGHT greater than zero, that should not be a problem.
Here's my solution
private final static int DELAY_MS = 500;
autoCompletionTextView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
autoCompletionTextView.requestFocus();
new Handler().postDelayed(() -> autoCompletionTextView.showDropDown(), DELAY_MS);
return false;
}
});
After keyboard shows up suggestion list is listed above yout AutoCompletionTextView.
use: android:dropDownHeight="wrap_content" in AutoCompleteTextView
Just adding the android:dropDownHeight="100dp" to the AutoCompleteTextView tag in your layout file will be the best solution I guess! it will simply control the height of drop down hight and allow us to scroll!
<AutoCompleteTextView
android:id="#+id/acetxt_assignclient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dropDownHeight="100dp">
</AutoCompleteTextView>
I have found if you are using a nested scroll view, it is more prone to open the view above or below as it sees fit where as when you are using a regular scroll view it opens below.
Set Full Layout containing Autocompletetextview inside Scrollview
This will solve your problem!

Click Event on ImageButton not dispatched immediately

I'm having a weird problem, in my rather complex view layout. (I will try to simplify it a bit in my explanation)
Basically I have a ListView, where each item consists of a TextView and an ImageButton. I am able to either click the list item (on the textview), or the button (I set the ImageButton to non-focusable, otherwise it wouldn't work)
Now it seems to work fine, until I open another window and return to the listview.
From that point on, I can click the ImageButton without anything happening (not even the background changes during the click). But when I click on the TextView again, all the click events from the ImageButton are dispatched at once.
Why is that?
EDIT:
The List Item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0px"
android:minHeight="40dp"
android:orientation="horizontal"
android:paddingLeft="2px"
android:paddingRight="2px"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Text"
android:textSize="19dp"
android:paddingTop="4px"
android:paddingBottom="4px"
android:layout_gravity="center_vertical"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/open_subtree_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0px"
android:orientation="horizontal"
android:padding="0px">
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#drawable/separator_line" />
<com.treeviewer.leveldisplay.DontPressWithParentImageButton
android:id="#+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="#drawable/list_selector_background"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"
android:src="#drawable/arrow_right" />
</LinearLayout>
</LinearLayout>
That's how it is inflated:
[...]
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.tree_row, null, false);
TextView textView = (TextView)mView.findViewById(R.id.text1);
LinearLayout nextNodeButtonContainer = (LinearLayout)mView.findViewById(R.id.open_subtree_layout);
if(childCount >= 0) {
titleBuilder.append(" (" + childCount + ")");
nextNodeButtonContainer.setVisibility(View.VISIBLE);
View button = nextNodeButtonContainer.findViewById(R.id.btn_right);
button.setFocusable(false);
button.setFocusableInTouchMode(false);
//button.setClickable(true);
button.setOnClickListener(clickListener);
button.setTag(tagValue);
} else {
nextNodeButtonContainer.setVisibility(View.GONE);
}
textView.setText(titleBuilder);
Let me know, if you need more code.
Ok, I finally solved this problem.
Unfortunately, in my question I didn't provide the necessary information to solve it, as the problem was somewhere I didn't expect it:
I have a ListAdapter where the getView method looks like this:
public View getView(int position, View convertView, ViewGroup parent) {
return mNodes.get(position).getView(mNodeArrowClickListener, position);
}
And the getView method of the nodes (TreeLevelElements) looked like:
public class TreeLevelElement {
private final Context mContext;
private View mView = null;
//[...] other methods
View getView(OnClickListener clickListener, final int tagValue) {
if(mView == null) {
//[...] produce a new View from XML
}
return mView;
}
}
The problem was, that I stored the Views in my elements, so I guess that conflicted somehow with android strategy to reuse old views for new items.
I don't know what exactly happened, but now that I removed mView and create a new one every time, it works.
I will also change it to reuse the convertView instead.

Categories

Resources