2016-03-24 79 views
0

隨着Android的對可穿戴設備(http://developer.android.com/intl/pt-br/training/wearables/ui/lists.html)創建列表的課的WearableListView的項目,我想知道如何創建如下項目就像一個鬧鐘應用程序的「重複」的文章:製作可重複

enter image description here

...如果我要連續向上或向下滾動,如何使WearableListItemLayout返回到項目列表的開頭?

謝謝!

EDIT(基於Budius的帖子):

這是我的新成立的主要活動:

public class DimensionsActivity extends Activity implements WearableListView.ClickListener { 

    // Sample dataset for the list 
    private String[] elements = {"1", "2", "3", "4"}; 

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

     // Get the list component from the layout of the activity 
     WearableListView listView = (WearableListView) findViewById(R.id.wearable_list); 

     // Assign an adapter to the list 
     listView.setAdapter(new Adapter(this, elements)); 

     // Set a click listener 
     listView.setClickListener(this); 

     if(savedInstanceState == null) { 
      listView.scrollToPosition(Integer.MAX_VALUE/2); // adjust this value as needed 
     } 
    } 

    // WearableListView click listener 
    @Override 
    public void onClick(WearableListView.ViewHolder v) { 
     Integer tag = (Integer) v.itemView.getTag(); 
    } 

    @Override 
    public void onTopEmptyRegionClick() {} 

    // The adapter populates the WearableListView.OnCenterProximityListener element with content. 
    // The following simple adapter populates the list with elements based on an array of strings: 
    private static final class Adapter extends WearableListView.Adapter { 
     private String[] mDataset; 
     private final Context mContext; 
     private final LayoutInflater mInflater; 

     // Provide a suitable constructor (depends on the kind of dataset) 
     public Adapter(Context context, String[] dataset) { 
      mContext = context; 
      mInflater = LayoutInflater.from(context); 
      mDataset = dataset; 
     } 

     // Provide a reference to the type of views you're using 
     public static class ItemViewHolder extends WearableListView.ViewHolder { 
      private TextView textView; 
      public ItemViewHolder(View itemView) { 
       super(itemView); 
       // find the text view within the custom item's layout 
       textView = (TextView) itemView.findViewById(R.id.name); 
      } 
     } 

     // Create new views for list items 
     // (invoked by the WearableListView's layout manager) 
     @Override 
     public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      // Inflate our custom layout for list items 
      return new ItemViewHolder(mInflater.inflate(R.layout.list_item, null)); 
     } 

     // Replace the contents of a list item 
     // Instead of creating new views, the list tries to recycle existing ones 
     // (invoked by the WearableListView's layout manager) 
     @Override 
     public void onBindViewHolder(WearableListView.ViewHolder holder, int position) { 
      position = Integer.MAX_VALUE % position; // offset the position to be within your range 

      // retrieve the text view 
      ItemViewHolder itemHolder = (ItemViewHolder) holder; 
      TextView view = itemHolder.textView; 
      // replace text contents 
      view.setText(mDataset[position]); 
      // replace list item's metadata 
      holder.itemView.setTag(position); 
     } 

     // Return the size of your dataset 
     // (invoked by the WearableListView's layout manager) 
     @Override 
     public int getItemCount() { 
      return Integer.MAX_VALUE; 
     } 
    } 
} 

...然後我結束了以下錯誤:

enter image description here

回答

1

我不知道獲得真正的連續列表的方式,但有一個棘手的我已經應用b之前在我們的應用程序,很好地工作。

你只是在一個非常長的名單中間。爲此,您在適配器getItemCount()上使用WearableListView,您將返回Integer.MAX_VALUE,並在onCreate()期間將列表移動到中間位置。

像這樣:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ... all the good to build views and setup the WearableListView 

    if(savedInstanceState == null) { 
     wearableListView.scrollToPosition(Integer.MAX_VALUE/2); // adjust this value as needed 
    } 
} 

適配器上:

public int getItemCount() { 
    return Integer.MAX_VALUE; 
} 

public void bindViewHolder(Holder holder, int position) { 
    position = position % mDataset.length; // offset the position to be within your range 

    ... proceed with the normal position view binding 

} 

我記得我們用這個棘手的我工作的應用來看,我們做了數學和用戶將有滾動幾個小時直到達到適配器的開始(或結束)

+0

謝謝您的迴應。我真的不明白它背後的數學是誠實的笑,但我試了一下,如上所示在我編輯的帖子!有什麼想法,我做錯了,以擺脫索引錯誤? – DaveNOTDavid

+1

我把這一切都放在心上,我想我的錯誤是在'position = Integer.MAX_VALUE%position'。符號'%'給出了另一個的整數除法的餘數。因此,任何大數除以12(用於時鐘)的其餘部分總是在'0〜11'之間,所以綁定發生在實際數據集的正確位置。 – Budius