首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓RecyclerView适配器随机调用(在我看来)

安卓RecyclerView适配器随机调用(在我看来)
EN

Stack Overflow用户
提问于 2016-11-07 15:34:19
回答 1查看 185关注 0票数 0

下面是我的情况:我有一个ListView (我会尽快将它传递给RecyclerView ),它由一个ArrayAdapter填充,works完美无缺。每个item_main都有一个网格,包含0x0 ImageView、0x1 TextView、1x0/1片段,setVisibility没有了,通过“标准”OnClickListener切换到可视。其中一个项目有一个动态片段,我通过侦听器调用他的类,可能是错误的。问题是片段包含一个RecyclerView视图,它有各种item_hours,包含一个TablerRow,有5个TextView。实际上,我正在填充一个市场的小时表;片段类在item_main上的每次触摸都被称为“正确”,但是他的onBindViewHolder方法被随机调用了10次(根据我的观点)。

我做了一个更好的草稿: ListView: 4x item_main // item_main =>网格=>图像,文本,片段//片段=>回收视图=> TableRow =>文本,text / RecyclerView适配器随机调用者

这是密码和日志:

OnClickListener (ItemAdapterMain.class方法)

代码语言:javascript
复制
case hours:
    HoursFragment hoursFragment = new HoursFragment();
    FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_hours, hoursFragment);
    fragmentTransaction.commit();
    setVisible(2);
    s = "hours"; // for Log only
    break;

HoursFragment.class

代码语言:javascript
复制
public class HoursFragment extends android.app.Fragment {

    private static final String TAG = "com.forface.luxurymom";

    private Context context;

    private final String _10_30 = "10:30";
    private final String _13 = "13:00";
    private final String _15_30 = "15:30";
    private final String _16 = "16:00";
    private final String _20 = "20:00";

    List dayList;
    private Day mon, tue, wen, thu, fri, sat, sun;

    public HoursFragment() {
        // Required empty public constructor
    }
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = container.getContext();

        View view = inflater.inflate(R.layout.fragment_hours, container, false);

        mon = new Day("mon",_15_30,_20);
        tue = new Day("tue",_10_30,_13, _15_30,_20);
        wen = new Day("wen",_10_30,_13, _15_30,_20);
        thu = new Day("thu",_10_30,_13, _15_30,_20);
        fri = new Day("fri",_10_30,_13, _15_30,_20);
        sat = new Day("sat",_10_30,_13, _15_30,_20);
        sun = new Day("sun",_16,_20);

        dayList = new LinkedList();
        dayList.add(mon);
        dayList.add(tue);
        dayList.add(wen);
        dayList.add(thu);
        dayList.add(fri);
        dayList.add(sat);
        dayList.add(sun);

        Log.i(TAG, "Call ItemAdapterHours"); /////////////////////////////////////////////////////// LOG CALL ADAPTER

        RecyclerView hoursRecyclerView = (RecyclerView) view.findViewById(R.id.hours_recycler_view);
        ItemAdapterHours adapter = new ItemAdapterHours(getActivity(), dayList);
        hoursRecyclerView.setAdapter(adapter);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        hoursRecyclerView.setLayoutManager(llm);

        return view;
    }
}

ItemAdapterHours.class

代码语言:javascript
复制
public class ItemAdapterHours extends RecyclerView.Adapter<ItemAdapterHours.MyViewHolder>{

    private static final String TAG = "com.forface.luxurymom";

    private final int call = R.id.main_call;
    private final int write = R.id.main_write;
    private final int hours = R.id.main_hours;
    private final int map = R.id.main_map;

    private List<Day> dayList;

    Context context;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView itemDay;
        public TextView itemOpen;
        public TextView itemPauseStart;
        public TextView itemPauseEnd;
        public TextView itemClose;

        public MyViewHolder(View view) {
            super(view);
            itemDay = (TextView)view.findViewById(R.id.hours_item_day);
            itemOpen = (TextView)view.findViewById(R.id.hours_item_open);
            itemPauseStart = (TextView)view.findViewById(R.id.hours_item_pause_start);
            itemPauseEnd = (TextView)view.findViewById(R.id.hours_item_Pause_end);
            itemClose = (TextView)view.findViewById(R.id.hours_item_cose);
        }
    }

    public ItemAdapterHours (Context context, List<Day> mDayList){
        Log.i(TAG, "ItemAdapterHours call received"); ////////////////////////////////////////////// LOG CALL RECEIVED
        this.context = context;
        Activity activity = (Activity) context;
        dayList = mDayList;
    }

    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.i(TAG, "ItemAdapterHours started"); //////////////////////////////////////////////////// LOG STARTED
        Day item = dayList.get(position);

        holder.itemDay.setText(item.getName());
        if (item.openTime() != null)
            holder.itemOpen.setText(item.openTime().toString());
        if (item.pauseStartTime() != null)
            holder.itemPauseStart.setText(item.pauseStartTime().toString());
        holder.itemPauseEnd.setText(item.pauseEndTime().toString());
        holder.itemClose.setText(item.closeTime().toString());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hours,parent, false);
        return new MyViewHolder(v);
    }

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

日志

代码语言:javascript
复制
11-07 15:43:27.466 : ItemAdapterHours call received
11-07 15:43:27.947 : Call ItemAdapterHours
11-07 15:43:27.947 : ItemAdapterHours call received
11-07 15:43:28.355 : Call ItemAdapterHours
11-07 15:43:28.355 : ItemAdapterHours call received
11-07 15:43:28.791 : Call ItemAdapterHours
11-07 15:43:28.791 : ItemAdapterHours call received
11-07 15:43:29.197 : Call ItemAdapterHours
11-07 15:43:29.197 : ItemAdapterHours call received
11-07 15:43:29.647 : Call ItemAdapterHours
11-07 15:43:29.647 : ItemAdapterHours call received
11-07 15:43:30.044 : Call ItemAdapterHours
11-07 15:43:30.044 : ItemAdapterHours call received
11-07 15:43:30.045 : Call ItemAdapterHours
11-07 15:43:30.045 : ItemAdapterHours call received
11-07 15:43:30.446 : Call ItemAdapterHours
11-07 15:43:30.446 : ItemAdapterHours call received
{......}
11-07 15:43:36.240 : Call ItemAdapterHours
11-07 15:43:36.240 : ItemAdapterHours call received
11-07 15:43:37.088 : Call ItemAdapterHours
11-07 15:43:37.088 : ItemAdapterHours call received
11-07 15:43:37.088 : Call ItemAdapterHours
11-07 15:43:37.088 : ItemAdapterHours call received
11-07 15:43:37.238 : ItemAdapterHours started
11-07 15:43:37.239 : ItemAdapterHours started
11-07 15:43:37.648 : Call ItemAdapterHours
11-07 15:43:37.648 : ItemAdapterHours call received
11-07 15:43:37.648 : Call ItemAdapterHours
11-07 15:43:37.648 : ItemAdapterHours call received
11-07 15:43:37.792 : ItemAdapterHours started
11-07 15:43:37.793 : ItemAdapterHours started
11-07 15:43:38.215 : Call ItemAdapterHours
11-07 15:43:38.215 : ItemAdapterHours call received
11-07 15:43:38.216 : Call ItemAdapterHours
11-07 15:43:38.216 : ItemAdapterHours call received
11-07 15:43:38.344 : ItemAdapterHours started
11-07 15:43:38.346 : ItemAdapterHours started
EN

回答 1

Stack Overflow用户

发布于 2016-11-07 23:13:04

刚解决了这个问题:

代码语言:javascript
复制
switch (v.getId()){
                case call:
                    setVisible(0);
                    s = "call";
                    break;
                case write:
                    setVisible(1);
                    s = "write";
                    break;
                case hours:
                    setVisible(2);

                    HoursFragment hoursFragment = new HoursFragment();
                    FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_hours, hoursFragment);
                    fragmentTransaction.commit();
                    s = "hours";
                    break;
                case map:
                    setVisible(3);
                    s = "map";
                    break;
            }
            notifyDataSetChanged();
}

这方面:

代码语言:javascript
复制
switch (v.getId()){
                case call:
                    notifyDataSetChanged();
                    setVisible(0);
                    s = "call";
                    break;
                case write:
                    notifyDataSetChanged();
                    setVisible(1);
                    s = "write";
                    break;
                case hours:
                    setVisible(2);
                    notifyDataSetChanged();

                    HoursFragment hoursFragment = new HoursFragment();
                    FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.fragment_hours, hoursFragment);
                    fragmentTransaction.commit();
                    s = "hours";
                    break;
                case map:
                    notifyDataSetChanged();
                    setVisible(3);
                    s = "map";
                    break;
            }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40468904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档