简体   繁体   English

如何以编程方式刷新线性布局

[英]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).我的应用程序有一个LinearLayoutLinearLayout由 3 个Linearlayouts组成,并且每个线性布局都有一个Relativelayout和一个线性布局(最初这些线性布局的可见性消失了)。 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.检索包含所有内容的 LinearLayout。 When you need to "refresh" it, call invalidate .当您需要“刷新”它时,请调用invalidate Only call it in the UI thread though.不过只能在 UI 线程中调用它。 If you call it in another thread (say a timer), then call postInvalidate .如果您在另一个线程(例如计时器)中调用它,则调用postInvalidate Both cause the View's onDraw method to be called when the OS is ready to call it.当操作系统准备好调用 View 时,两者都会导致调用 View 的onDraw方法。

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而且我发现了一个简单的方法,用 3 行代码刷新 LinearLayout

You must set transperent color in style.xml您必须在 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()要以图形方式刷新视图,请使用 invalidate 调用 onDraw() 并重新计算视图尺寸以及与高度、宽度、边距和填充相关的任何内容,使用 requestLayout 调用 onMeasure()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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