2016-11-07 100 views
0

这是我的情况:我有一个ListView(我将它传递到RecyclerView asap)由ArrayAdapter填充,这工作完美无瑕。每个item_main都有一个Grid,其中包含0x0 ImageView,0x1 TextView,1x0/1片段,setVisibility Gone,通过“标准”OnClickListener切换为可见。 其中的一个项目有一个动态的片段,我叫他的班级听众,也许有错误。 问题是Fragment包含一个RecyclerView视图,该视图具有包含TableRRow和5个TextView的各种item_hours。实际上我正在填充一个市场的小时表; Fragment类在item_main的每次触摸时被调用“正确”,但他的onBindViewHolder方法被随机调用了10次(在我的观点上)。Android RecyclerView适配器随机调用

我做了更好的草案: 的ListView:4X item_main // item_main =>格=>图像,文本片段// 该片段=> Recyclerview =>的TableRow =>文本,文本,文本,文本,文本// RecyclerView适配器呼叫者随机

这里的代码和经度:

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

LOG

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 
+0

这是RecyclerView的自然的工作。在滚动时,OnbindViewHolder方法会多次调用,只有在滚动显示视图销毁和新视图后创建的屏幕中才会显示可见项。 –

+0

我刚刚通过把notifyDataSetChanged()解决;在片段初始化之前:) – 4face

回答

0

恰恰解决了这个chaning:

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

}

相关问题