简体   繁体   中英

How to refresh the linear layout programmatically

My application has one LinearLayout that LinearLayout consists of the 3 Linearlayouts and that each linear layout has one Relativelayout and one linear layout(initially the visibility of these linear layouts is gone). If you click on the relative layout then the regarding linear layout will be displayed. But how to refresh the entire linear layout after clicking on the relative layout.

Code:

private OnClickListener exit2Listener = new OnClickListener()
{
    public void onClick(View v)
    {
       if(!exit2status)
       {
          if(RB_Constant.upcomingexits_obj.response.size() > 1)
          {
             if(RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size() > 0)
             {
                 // Create the views on the fly instead of using the ListView
                 UpcomingResultsListViewAdapter2 rbupcadapter2 = new UpcomingResultsListViewAdapter2(RB_UpcomingExits.this);
                 int numItems2 = 0;

                 if(RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size() > 0)
                 {
                    numItems2 = RB_Constant.upcomingexits_obj.response.get(1).listRestaurants.size();
                 }

                 //linearLayout2
                 for(int position=0; position < numItems2; position++)
                 {
                    View convertview = null;
                    convertview = rbupcadapter2.getView(position, convertview, null);
                    listLayout2.addView(convertview);
                 }
             }              
          }
          else
          {
             //toastMsg("No results!");
          }
          listLayout2.setVisibility(View.VISIBLE);
          exit2status=true;

          if(!exit1status || exit3status || exit4status || exit5status)
          {
             //System.out.println("exit2 GONE");
             listLayout1.setVisibility(View.GONE);
             listLayout3.setVisibility(View.GONE);
             exit1status = false;
             exit3status = false;

          }

          LLExitDetails.invalidate();
       }
       else
       {
          System.out.println("exit2 GONE");
          listLayout2.setVisibility(View.GONE);
          exit2status = false;

          LLExitDetails.invalidate();
       }
    }       
};

Retrieve the LinearLayout that contains everything. When you need to "refresh" it, call invalidate . Only call it in the UI thread though. If you call it in another thread (say a timer), then call postInvalidate . Both cause the View's onDraw method to be called when the OS is ready to call it.

I spent a lot of time to solve this problem too. And I found a simple method of refresh LinearLayout in 3 lines of code

You must set transperent color in style.xml

   <color name="transparent">#00000000</color>

And in the code just call to set background

   LinearLayout ll = (LinearLayout) findViewById(R.id.noteList);
   ll.setBackgroundColor(getResources().getColor(R.color.transparent));
   ll.invalidate();

If you has drawable background call

  ll.setBackgroundResource(R.drawable.your_drawable);

To refresh view as a graphical way use invalidate it's call onDraw() and for recalculate view dimensions and any thing related to height, width, margins and padding use requestLayout it's call onMeasure()

linearLayout.removeAllViews();
//The again load the linearlayout view
runLinearLayout();

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