在recyclerview中更新微调器

时间:2016-09-02 12:12:32

标签: android android-recyclerview android-arrayadapter android-spinner

在包含微调器的recylerview中,如何在微调器中动态添加更多项目(仅限于特定位置)...现在当我更新子微调器时,recylerview的所有微调器都会更新。请帮我更新特定位置(recylerview)的微调器。

以下是我的适配器(在recylerview中给这些微调器充气):

public class OfferRideRegularListViewAdapter extends RecyclerView.Adapter<OfferRideRegularListViewAdapter.MyViewHolder> implements SelectTimeFragment.SelectedTimeListener
{
    LayoutInflater inflater;
    CustomSpinnerAdapter leavingTimeAdpater,returningTimneAdpater;
    ArrayList<CommonListViewItems> leavingTimeItemsList,returningTimeItemsList;
    List<String> checkedItemsList;
    Context context;
    View convertView;
    private static final int LEAVING_TIMEPICKER_FRAGMENT = 2;
    private static final int RETURNING_TIMEPICKER_FRAGMENT = 3;
    private Fragment fragment;
    private int selectedDayPosition;
    private ArrayList<CommonListViewItems> leavingTimeOfDayList,returningTimeOfDayList;
    private ViewGroup viewGroup;
    private int TIMEPICKER_FRAGMENT=0;
    private CommonListViewItems items;
    RecylerView recyclerView;

    public OfferRideRegularListViewAdapter(Context context,ArrayList<CommonListViewItems> leavingTimeItemsList,ArrayList<CommonListViewItems> returningTimeItemsList,RecylerView recyclerView) {
        this.context=context;
        this.recyclerView = recyclerView;
        inflater = LayoutInflater.from(context);
        this.leavingTimeItemsList = leavingTimeItemsList;
        this.returningTimeItemsList = returningTimeItemsList;
        checkedItemsList = Arrays.asList(context.getResources().getStringArray(R.array.week_days));
        leavingTimeAdpater=new CustomSpinnerAdapter(context,R.layout.common_listview_item,this.leavingTimeItemsList);
        returningTimneAdpater = new CustomSpinnerAdapter(context,R.layout.common_listview_item,this.returningTimeItemsList);
        leavingTimeOfDayList = new ArrayList<CommonListViewItems>();
        for (int i=0;i<checkedItemsList.size();i++)
        {
            items = new CommonListViewItems();
            items.setlistItem(leavingTimeItemsList.get(0).getlistItem());
            leavingTimeOfDayList.add(items);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_offer_ride_regular_list_items, parent, false);

        convertView = itemView;
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.checkDay.setText(checkedItemsList.get(position));
        holder.spinnerLeavingTime.setAdapter(leavingTimeAdpater);
        holder.spinnerReturningTime.setAdapter(returningTimneAdpater);
    }

    @Override
    public int getItemCount() {
        return checkedItemsList.size();
    }


    public int getCount() {
        return checkedItemsList.size();
    }

    public void showSelectTimeDialog(int TIMEPICKER_FRAGMENT)
    {
        this.TIMEPICKER_FRAGMENT = TIMEPICKER_FRAGMENT;
        FragmentManager fm = ((MainActivity)context).getFragmentManager();
        SelectTimeFragment selectTimeFragment = new SelectTimeFragment();
        selectTimeFragment.setSelectedTimeListener(this);
        selectTimeFragment.show(fm,"TimePicker");
    }


    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        public CheckBox checkDay;
        public Spinner spinnerLeavingTime;
        public Spinner spinnerReturningTime;

        public MyViewHolder(View view) {
            super(view);
            checkDay=(CheckBox)view.findViewById(R.id.checkedDay);
            spinnerLeavingTime=(Spinner)view.findViewById(R.id.spinner_leavingTime);
            spinnerReturningTime=(Spinner)view.findViewById(R.id.spinner_returningTime);

            spinnerLeavingTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position == (leavingTimeAdpater.getCount() - 1)) {
                        hideKeyboard(view);
                        selectedDayPosition = getAdapterPosition();
                        showSelectTimeDialog(LEAVING_TIMEPICKER_FRAGMENT);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }

    @Override
    public String onTimeSelected(String selectedTime) {
        updateChildSpinnerList(selectedTime);
        return null;
    }

    @Override
    public void onTimeNotSelected(boolean noTimeSelected) {

    }

    private void updateChildSpinnerList(String selectedTime)
    {

        if (selectedTime!=null)
        {
            CommonListViewItems items;
            switch (TIMEPICKER_FRAGMENT)
            {
                case LEAVING_TIMEPICKER_FRAGMENT:
                {
                    items = new CommonListViewItems();
                    items.setlistItem(selectedTime);
                    items = new CommonListViewItems();
                    items.setlistItem("Change...");
                    leavingTimeOfDayList.remove(selectedDayPosition);
                    leavingTimeOfDayList.add(selectedDayPosition, items);
                    Spinner spinner= (Spinner) recyclerView.getChildAt(selectedDayPosition).findViewById(R.id.spinner_leavingTime);
                    notifyItemChanged(selectedDayPosition);
                }
            }
        }
        else
        {

        }



    }
}

我调用此适配器的片段:

offerRideRegularListViewAdapter = new OfferRideRegularListViewAdapter(getActivity(),getLeavingTimeList(),getReturningTimeList(),listEachDay);

        listEachDay.setAdapter(offerRideRegularListViewAdapter);

以下是CustomSpinnerAdpater:

public class CustomSpinnerAdapter extends ArrayAdapter<CommonListViewItems> {
    CommonListViewItems servicesItem;
    Context context;
    int layoutId;
    private ArrayList<CommonListViewItems> itemsArrayList;

    public CustomSpinnerAdapter(Context context, int layoutId, ArrayList<CommonListViewItems> itemsArrayList)
    {
        super(context,layoutId,itemsArrayList);
        this.context = context;
        this.layoutId=layoutId;
        this.itemsArrayList=itemsArrayList;
    }

    @Override
    public int getCount() {
        return itemsArrayList.size();
    }

    @Override
    public CommonListViewItems getItem(int position) {
        return itemsArrayList.get(position);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position,convertView,parent);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        return getCustomView(position,convertView,parent);
    }

    private View getCustomView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;

        if (convertView==null)
        {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(layoutId,parent,false);
            holder=new ViewHolder();
            servicesItem = new CommonListViewItems();
            holder.textView=(TextView)convertView.findViewById(R.id.tv_listItems);
            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }
        final CommonListViewItems servicesItem =(CommonListViewItems)itemsArrayList.get(position);
        holder.textView.setText(servicesItem.getlistItem());
        return convertView;
    }

    private class ViewHolder
    {
        public TextView textView;
    }

}

CommonListViewItems类:

public class CommonListViewItems {

    String listItem;

    public String getlistItem() {
        return listItem;
    }

    public void setlistItem(String listItem) {
        this.listItem = listItem;
    }
}

recyclerview看起来像这样:

enter image description here

并且在点击微调器时我想从TimePickerDialog添加更多项目(时间),但添加的项目会添加到每个微调器,我只想添加到被触摸的特定微调器:

提前致谢...

3 个答案:

答案 0 :(得分:1)

您需要具有不同数据集的同一spinnerAdapter的多个实例。

您通过使用arraylist为checkday textview做了同样的事情。在这种情况下,在onBindViewHolder中,当天的值取决于列表中的位置。尝试做同样的事。

答案 1 :(得分:1)

从您发布的代码中看不清楚每个rowItem是什么,所以我将布置如何处理这个问题:

1)为任意行项目制作单独的布局,这应包含CheckBox,用于保存日期的TextView以及要填充的Spinner。

2)创建一个代表所有这些的对象,如'DayAndTimeSelection'或类似的。这些对象中的每一个都有自己的Spinner实例和自己的SpinnerAdapter实例,因为您需要在该行的Spinner与该微调器中可能的时间集之间建立关联。

确认在进行之前,这对于一行项目的预期工作....

3)创建一个外部'DayAndTimeItemListAdapter'来处理整个单独行的集合。在您的示例中,此列表中只有7个项目,但如果您更改为月视图,则可能会获得30.如果对每个行正确执行抽象,则更改每个行项中的值,添加旋转器的时间和获取/设置复选框将清楚。外部适配器是您将回收视图的那个,因此请确保列表的getView()和getItem()按预期工作,并且每行都有一个单独且唯一的Spinner和SpinnerAdapter实例(请查看在该行的Spinner&amp; SpinnerAdapter中的'@'符号后面的id ......如:'Spi​​nnerAdapter @ 1234')

4)利润......

答案 2 :(得分:0)

是的,具有不同数据集的每个适配器的不同实例可以更新recyclerview中选定位置的特定微调器适配器。

我解决了这个问题:

  1. 创建ArrayLists的ArrayList,如:

    ArrayList<String> [] list = new ArrayList[predefined_size];
    for (int i=0;i<7;i++)
    {
        list[i]=new ArrayList<String>();
        list[i].addAll(parentList); //parentList is an ArrayList of custom items;
    }
    
  2. 现在位于onBindViewHolder recyclerview setAdapter Spinner SpinnerAdpater public void onBindViewHolder(MyViewHolder holder, int position) { holder.spinner.setAdapter(new CustomSpinnerAdapter(context,R.layout.common_listview_item,list[position])); list[position].add(item); 个实例与单独的数据集(列表或arraylist等) 。),像这样:

    spinner
  3. 然后我们可以将项添加到特定微调器的列表或适配器中,如下所示:

    public async ActionResult DoWork()
    {
    //Carry out some processing
    
        string jsonMessage = new JavaScriptSerializer().Serialize(MyDataObject);
        await storageAccess.AddItemToQueue(new CloudQueueMessage(jsonMessage));
    }
    
    
    
    
    public async Task AddItemToQueue(CloudQueueMessage message)
    {
    
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            ConfigurationManager.AppSettings["StorageConnectionString"]);
    
                // Create the blob client.
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            var queue = queueClient.GetQueueReference("outbox");
    
            await queue.CreateIfNotExistsAsync();
            await queue.AddMessageAsync(message);
    }
    
  4. 通过这种方式,只有1. ksasdsad-asdsad-asdsads-s222 12:05 MyObject1 2. sadadsd-weqwe-sdddsd-e21323 12:10 MyObject1 3. 32323-asdsads-2123213-sddwe 12:30 MyObject1 将被更新,我们想要更新//即给定位置的微调器。

    因此,实现了在RecyclerView中给定位置更新微调器。