Problem in user view group when updating ListViews

firstly, I created a repo on gitHub with a sample project with my error in it: https://github.com/renaudfavier/SampleBug

I am having difficulty with my lists in my customViewGroups: I will simulate an assynk data request, then I tring to populate my list, and there are difficulties with it. Please note that there is a little bust in this application, because I need it in my big one. I cannot go to fragments.

My problem is that my listView does not update, it happens when I open my menu

package renaud.bug; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class SampleBugActivity extends Activity { private boolean menuDisplayed; private CustomViewGroup cvg; private ListView myListView; private ArrayList<String> myStrings; private MyAdapter stringAdapter; private LayoutInflater inflater; private ArrayList<String> array; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myStrings = new ArrayList<String>(); inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); cvg = (CustomViewGroup) findViewById(R.id.viewGroup); stringAdapter = new MyAdapter(this, R.layout.list_view_item, myStrings); myListView = (ListView) findViewById(R.id.listView1); stringAdapter.add("TEST "); stringAdapter.notifyDataSetChanged(); myListView.setAdapter(stringAdapter); new Thread(assyncSim).start(); } private Runnable onFinish = new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "thread finishes push menu button", Toast.LENGTH_LONG); Log.i("renaud", "thread finishes push menu button"); if (array != null && array.size() > 0) { stringAdapter.notifyDataSetChanged(); for (int i = 0; i < array.size(); i++) { stringAdapter.add(array.get(i)); } } stringAdapter.notifyDataSetChanged(); } }; private Runnable assyncSim = new Runnable() { @Override public void run() { array = new ArrayList<String>(); for (int i = 0; i < 10; i++) { array.add("Item " + i); } synchronized (this) { try { wait(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } runOnUiThread(onFinish); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { if (!menuDisplayed) { customScroll(200); menuDisplayed = true; return true; } else { customScroll(0); menuDisplayed = false; return true; } } public void customScroll(final int i) { new Thread(new Runnable() { @Override public void run() { if (i == 0) { for (int inc = 200; inc >= 0; inc -= 5) { final int inc2 = inc; runOnUiThread(new Runnable() { @Override public void run() { cvg.scrollTo(inc2); } }); try { synchronized (this) { wait(5); } } catch (InterruptedException e) { e.printStackTrace(); } } } else { for (int inc = 0; inc <= 200; inc += 5) { final int inc2 = inc; runOnUiThread(new Runnable() { @Override public void run() { cvg.scrollTo(inc2); } }); try { synchronized (this) { wait(5); } } catch (InterruptedException e) { e.printStackTrace(); } } } } }).start(); } private class MyAdapter extends ArrayAdapter<String> { private ArrayList<String> myStrings; public MyAdapter(Context context, int textViewResourceId, ArrayList<String> strings) { super(context, textViewResourceId, strings); this.myStrings = strings; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.list_view_item, null); } TextView tv = (TextView) convertView.findViewById(R.id.itemTextView); tv.setText(myStrings.get(position)); return convertView; } } } 

my customViewGroup:

 package renaud.bug; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.View.MeasureSpec; import android.view.ViewGroup; public class CustomViewGroup extends ViewGroup{ private LayoutInflater inflater; private int mCurScreen; private int mDefaultScreen = 0; private View menuView; private View practicalView; public CustomViewGroup(Context context) { super(context); initView(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { mCurScreen = mDefaultScreen; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); practicalView = inflater.inflate(R.layout.layout, null); this.addView(practicalView); menuView = inflater.inflate(R.layout.menu_layout, null); this.addView(menuView); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub if (changed) { int childLeft = 0; final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { final int childWidth = childView.getMeasuredWidth(); childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight()); childLeft += childWidth; } } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( "ScrollLayout only canmCurScreen run at EXACTLY mode!"); } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( "ScrollLayout only can run at EXACTLY mode!"); } // The children are given the same width and height as the scrollLayout final int count = getChildCount(); for (int i = 0; i < count; i++) { getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); } // Log.e(TAG, "moving to screen "+mCurScreen); scrollTo(mCurScreen * width, 0); } public void scrollTo(int i){ scrollTo(i,0); } } 

my layouts:

main:

 <?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" > <renaud.bug.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" > </renaud.bug.CustomViewGroup> </LinearLayout> 

layout.xml:

 <?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:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#991111"/> </LinearLayout> 

menu_layout.xml:

 <?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:orientation="vertical" android:background="#119911" > </LinearLayout> 

Listviewitem

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/itemTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FF0" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> 
+4
source share

Source: https://habr.com/ru/post/1400976/


All Articles