Android RecyclerView 适配器随机调用(在我看来)

问题描述 投票:0回答:1

这是我的情况:我有一个由 ArrayAdapter 填充的 ListView(我将尽快将其传递到 RecyclerView 中),它工作完美。每个item_main都有一个Grid,其中包含0x0 ImageView,0x1 TextView,1x0/1 Fragment,setVisibility消失,通过“标准”OnClickListener切换为VISIBLE。

其中一个项目有一个动态的Fragment,我通过Listener调用他的类,也许有错误。 问题是Fragment包含一个RecyclerView视图,其中有各种item_hours,包含一个TablerRow,有5个TextView。实际上,我正在填充市场的营业时间表; Fragment 类在每次触摸 item_main 时都会被“正确”调用,但他的 onBindViewHolder 方法会随机调用 10 次(在我看来)。

我起草了一份更好的草稿:
ListView:4x item_main //
item_main => 网格 => 图像、文本、片段 //
该片段 => Recyclerview => TableRow => 文本,文本,文本,文本,文本 //
RecyclerView Adapter 随机调用者

这是代码和日志:

OnClickListener(ItemAdapterMain.class的方法)

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

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

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();
    }
}

日志

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
android android-recyclerview fragment adapter
1个回答
0
投票

刚刚解决了这个问题:

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();
}

进入这个:

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;
            }

}

© www.soinside.com 2019 - 2024. All rights reserved.