WearableListView如何预先选择一个项目?

时间:2015-08-13 08:39:49

标签: android android-listview android-recyclerview wear-os

我在Wear App上的片段中有一个WearableListView。

从手机发送默认情况下必须选择项目及其位置。

但我不知道如何设置所选项目(我对RecycleView没有多少经验)。

因此,当我获得位置时,我更新适配器并调用notifyDataSetChanged,所以onBindViewHolder如果位置匹配,我将setSelected和setActivated设置为相应项的true。但是不起作用。

初始化片段

public BoxInsetLayout mContainerView;
WearableListView mListView;
String[] elements = { "1 parada", "2 paradas", "3 paradas" };
private Adapter mAdapter;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mContainerView = (BoxInsetLayout) getActivity().findViewById(R.id.list_container);

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

    mListView.setGreedyTouchMode(true);

    // Assign an adapter to the list
    mAdapter = new Adapter(getActivity(), elements, -1);
    mListView.setAdapter(mAdapter);

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

    mListView.addOnCentralPositionChangedListener(new WearableListView.OnCentralPositionChangedListener() {
        @Override
        public void onCentralPositionChanged(int i) {
            // TODO: Llamar a TrasnportMe con el valor escogido
            Utils.sendMessage(Constants.SERVICE_CALLED_WEAR, i + "");
            Log.e(TAG, "My pos: " + i);
        }
    });


    IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
    MessageReceiver messageReceiver = new MessageReceiver();
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(messageReceiver, messageFilter);
}
抓住位置更新的

接收器

public class MessageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getBundleExtra("message");

        String stopsForewarning = b.getString("stopsForewarning");

        Log.e(TAG, "STOPS FOREWARNING WEAR: " + stopsForewarning);
        int stopsForewarningIndex = 0;
        if ("1".equals(stopsForewarning)) {
            stopsForewarningIndex = 0;
        } else if ("2".equals(stopsForewarning)) {
            stopsForewarningIndex = 1;
        } else if ("3".equals(stopsForewarning)) {
            stopsForewarningIndex = 2;
        }

        if (mAdapter != null && getActivity() != null) {
            mAdapter = new Adapter(getActivity(), elements, stopsForewarningIndex);
            mAdapter.notifyDataSetChanged();
            mListView.setAdapter(mAdapter);
        }

    }
}

我的适配器

 private static final class Adapter extends WearableListView.Adapter {
    private final int mSelectedPosition;
    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, int selectedPosition) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDataset = dataset;
        mSelectedPosition = selectedPosition;
    }

    // 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) {
        // retrieve the text view
        ItemViewHolder itemHolder = (ItemViewHolder) holder;
        TextView view = itemHolder.textView;
        // replace text contents
        view.setText(mDataset[position]);
        view.setTag(position);
        // replace list item's metadata
        holder.itemView.setTag(position);
        Log.e(TAG, "Adapter, selected position: " + mSelectedPosition);
        if (mSelectedPosition != -1) {
            if (position == mSelectedPosition) {
                holder.itemView.setSelected(true);
                holder.itemView.setActivated(true);
            }
        }



    }


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

}

修改

我期望列表被移动(请参阅移动或不需要滚动)到特定项目。在这个例子中,我手动移动到第二个位置(索引1):

enter image description here

1 个答案:

答案 0 :(得分:1)

最后mListView.scrollToPosition(stopsForewarningIndex);是我需要的