0
隨着Android的對可穿戴設備(http://developer.android.com/intl/pt-br/training/wearables/ui/lists.html)創建列表的課的WearableListView的項目,我想知道如何創建如下項目就像一個鬧鐘應用程序的「重複」的文章:製作可重複
...如果我要連續向上或向下滾動,如何使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;
}
}
}
...然後我結束了以下錯誤:
謝謝您的迴應。我真的不明白它背後的數學是誠實的笑,但我試了一下,如上所示在我編輯的帖子!有什麼想法,我做錯了,以擺脫索引錯誤? – DaveNOTDavid
我把這一切都放在心上,我想我的錯誤是在'position = Integer.MAX_VALUE%position'。符號'%'給出了另一個的整數除法的餘數。因此,任何大數除以12(用於時鐘)的其餘部分總是在'0〜11'之間,所以綁定發生在實際數據集的正確位置。 – Budius