使WearableListView的项目可重复

时间:2016-03-24 16:08:47

标签: android listview android-studio wear-os

借助Android关于为可穿戴设备(http://developer.android.com/intl/pt-br/training/wearables/ui/lists.html)创建列表的课程,我想知道如何创建一个可重复的"像闹钟应用程序一样的项目列表如下:

enter image description here

...如果我要如上所述连续向上或向下滚动,我如何让WearableListItemLayout回到项目列表的开头?

谢谢!

编辑(基于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 个答案:

答案 0 :(得分:1)

我不知道获得真实连续列表的方法,但我之前在我们的应用程序上应用的方法非常复杂。

你只是在一个非常长的列表中间做对了。为此,您使用适配器WearableListView上的getItemCount()返回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

}

我记得我们在我工作的应用程序上使用了这个技巧,我们做了数学计算,用户必须滚动几个小时才能到达适配器的开头(或结束)