为什么Polymer&#39的计算属性需要显式属性参数?

时间:2015-06-04 17:16:36

标签: polymer polymer-1.0

我正在挖掘Polymer 1.0元素,我对计算属性有点好奇。

例如,在 paper-drawer-panel.html 中,

public class ListActivity extends FragmentActivity {

private UserCustomFilters mUserCustomFilters = new UserCustomFilters();

// Our database hostname and the credentials
String url = ; //
String user = ;
String pass =;

SQLUtils sqlu ; //The SQLUtil object type that will be initialized later depending on the credentials given above.
ArrayList<Event> mEvents;       //The Array that will hold the Events that we will pass around(to Adapter,the List...)
List<Event> Even;

//Change Even to static if intent is used to refresh
ArrayList<Event> newEvent =new ArrayList<>();
ListviewAdapter adapter;

//Default Constructor for the class ListActivity
public ListActivity()
{
    sqlu = new SQLUtils(url, user, pass); //Creating Object type SQLUtils using credentials needed
    Even = sqlu.Events();  //Imports the List of Events from the Database.

    mEvents = new ArrayList<>();  //Assigning the new array where the events go.

    //Setting it into the new Array.
    for(int i=0;i<Even.size();i++)
    {
        mEvents.add(Even.get(i));
    }
}

//Injecting Buttons using ButterKnife Library
@InjectView(android.R.id.list) ListView mListView;

private void setUpFilters(){

    // Calling the FilterView class to set the layout for the filters

    FilterView filterView = new FilterView(this);
    mUserCustomFilters = filterView.init();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list2);

    setUpFilters();

    adapter=new ListviewAdapter(this, mEvents); 

    mListView.setAdapter(adapter);

    //Swipe stuff
    adapter.setMode(Attributes.Mode.Single);
}

..........

@OnClick(R.id.filterSaveButton)
public void ImplementingButton(View view)  {

    for(int i =0; i< mEvents.size();i++){

        if(mEvents.get(i).getEventName().equals("Event B")){

        }
        else{
            newEvent.add(mEvents.get(i));
        }
    }

    adapter.getData().clear();

    adapter.getData().addAll(newEvent);

    adapter.notifyDataSetChanged();

}

public class ListviewAdapter extends BaseSwipeAdapter { private Activity mActivity; private ArrayList<Event> mEvents; public static int Clicks=0; //the Constructor for the class. public ListviewAdapter(Activity activity, ArrayList<Event> events) { mActivity = activity; mEvents = events; } @Override public int getCount() { return mEvents.size(); //Returns length of the array of Events } @Override public Object getItem(int position) { return mEvents.get(position); //Returns the Item being accessed in the the array} } @Override public long getItemId(int position) { return 0; //Id of the Item being accessed in the view } public ArrayList<Event> getData() { return mEvents; } @Override public int getSwipeLayoutResourceId(int i) { return R.id.swipe; } @Override public View generateView(int position, ViewGroup parent) { //Inflates the view to be used View convertView = LayoutInflater.from(mActivity).inflate(R.layout.list_item, parent, false); ViewHolder holder = new ViewHolder(); //Making variable of class type ViewHolder def convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Assigning the Relative Layout that contains the detailed description. RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.DescriptionLayout); //Assigning the summary description stuff that will hide and reappear depending on the clicks. ImageView Bubble = (ImageView) v.findViewById(R.id.EventImageBubble); TextView EventName = (TextView) v.findViewById(R.id.TextEventName); TextView EventDate = (TextView) v.findViewById(R.id.TextEventDate); TextView EventPrice = (TextView) v.findViewById(R.id.TextEventPrice); TextView EventDistance = (TextView) v.findViewById(R.id.TextEventDistance); if (Clicks % 2 == 0) { //Popping the detailed description into view. layout.setVisibility(View.VISIBLE); //Hiding the summary Description from view to display the detailed description. Bubble.setVisibility(View.INVISIBLE); EventName.setVisibility(View.INVISIBLE); EventDate.setVisibility(View.INVISIBLE); EventPrice.setVisibility(View.INVISIBLE); EventDistance.setVisibility(View.INVISIBLE); } else { //Hiding the detailed description upon the 2nd click. layout.setVisibility(View.INVISIBLE); //Displaying the summary description back upon the 2nd click. Bubble.setVisibility(View.VISIBLE); EventName.setVisibility(View.VISIBLE); EventDate.setVisibility(View.VISIBLE); EventPrice.setVisibility(View.VISIBLE); EventDistance.setVisibility(View.VISIBLE); } Clicks++; //Adds to the number of times the user has tapped on an item. } }); convertView.setTag(holder); //sets the tag //Summary Description of the events. holder.EventPicture= (ImageView) convertView.findViewById(R.id.ImageEventPicture); holder.EventIcon = (ImageView) convertView.findViewById(R.id.ImageEventIcon); holder.EventName = (TextView) convertView.findViewById(R.id.TextEventName); holder.EventDate = (TextView) convertView.findViewById(R.id.TextEventDate); holder.EventPrice= (TextView) convertView.findViewById(R.id.TextEventPrice); holder.EventDistance= (TextView) convertView.findViewById(R.id.TextEventDistance); //Initializing each item to the required type Event event = mEvents.get(position); //Detailed Description of the events. holder.EventDName=(TextView) convertView.findViewById(R.id.DesEventName); holder.EventDPrice= (TextView) convertView.findViewById(R.id.DesEventPrice); holder.EventLocName=(TextView) convertView.findViewById(R.id.DesEventLocName); holder.EventLocSt=(TextView) convertView.findViewById(R.id.DesEventLocStreet); holder.EventLocAdd=(TextView) convertView.findViewById(R.id.DesEventLocAddress); holder.EventStartDate=(TextView) convertView.findViewById(R.id.DesEventStartDate); holder.EventStartTime=(TextView) convertView.findViewById(R.id.DesEventStartTime); holder.EventEndDate=(TextView) convertView.findViewById(R.id.DesEventEndDate); holder.EventEndTime= (TextView) convertView.findViewById(R.id.DesEventEndTime); //Setting the text boxes to the information retrieved from the arrays of events //Setting the summary description holder.EventDistance.setText(event.getEventDistance()+"km"); holder.EventName.setText(event.getEventName()); holder.EventDate.setText(event.getEventDate()); holder.EventPrice.setText("$"+event.getEventPrice()); //holder.EventIcon.setImageResource(event.getEventIcon()); //holder.EventPicture.setImageResource(event.getEventPicture()); //Setting the detailed description. holder.EventDName.setText(event.getEventName()); holder.EventDPrice.setText("$"+event.getEventPrice()); holder.EventLocName.setText(event.getEventLocName()); holder.EventLocSt.setText(event.getEventLocSt()); holder.EventLocAdd.setText(event.getEventLocAdd()); holder.EventStartDate.setText(event.getEventDate()); holder.EventStartTime.setText(event.getEventStartTime()); holder.EventEndDate.setText(event.getEventEndDate()); holder.EventEndTime.setText(event.getEventEndTime()); //Swipe methods being Implemented SwipeLayout swipeLayout = (SwipeLayout)convertView.findViewById(getSwipeLayoutResourceId(position)); swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut); swipeLayout.addDrag(SwipeLayout.DragEdge.Left, convertView.findViewById(R.id.bottom_wrapper)); swipeLayout.addDrag(SwipeLayout.DragEdge.Right, convertView.findViewById(R.id.mLinear)); swipeLayout.addSwipeListener(new SimpleSwipeListener() { @Override public void onOpen(SwipeLayout layout) { } }); return convertView; } @Override public void fillValues(int position, View convertView) { } private static class ViewHolder{ //The values holding summary description of the event. ImageView EventPicture; ImageView EventIcon; TextView EventName; TextView EventDate; TextView EventPrice; TextView EventDistance; //The Values holding detailed description of the event. TextView EventDName; TextView EventDPrice; TextView EventLocName; TextView EventLocSt; TextView EventLocAdd; TextView EventStartDate; TextView EventStartTime; TextView EventEndDate; TextView EventEndTime; } <dom-module id="paper-drawer-panel" …> … <div id="main" style$="[[_computeDrawerStyle(drawerWidth)]]"> … </div> … </dom-module> <script> Polymer({ is: 'paper-drawer-panel', … _computeDrawerStyle: function(drawerWidth) { return 'width:' + drawerWidth + ';'; }, … </script> 的属性,为什么将它显式包含在计算属性的参数中如此重要?

drawerWidth

这是不好的做法吗?

3 个答案:

答案 0 :(得分:10)

计算绑定中的显式参数有一个重要目的:告诉Polymer计算绑定所依赖的属性。这允许Polymer知道何时重新计算和更新计算的绑定。

[[_computeDrawerStyle()]]为例。在这种情况下,Polymer不知道计算绑定所依赖的其他属性,并且只会在加载时计算一次。

只要您明确添加drawerWidth[[_computeDrawerStyle(drawerWidth)]]),Polymer现在就知道每次drawerWidth更改时,它应该再次为新值运行计算绑定。

答案 1 :(得分:0)

我觉得你很困惑。你在这里的代码示例中指的是style$="[[_computeDrawerStyle(drawerWidth)]]"是对一个名为_computeDrawerStyle的私有函数的调用,当然你需要明确地给它提供正确的参数。查看文档here以了解计算属性。

答案 2 :(得分:0)

Polymer有两个独立的概念,你会混淆它们。

  1. 计算属性。这些属性依赖于其他属性,并在组件发生更改时重新计算。然后,您可以将该计算属性的值数据绑定为属性值。 <paper-draw-panel>没有计算属性(我检查了代码)。

  2. 数据绑定中引用的函数调用(_computeDrawStyle)是什么。这会导致Polymer在其任何参数发生变化时调用该函数(元素)。参数是所有属性(或者您可以使用对象的子属性和数组的索引)这就是这里发生的事情。