【发布时间】:2016-11-07 15:34:19
【问题描述】:
这是我的情况:我有一个由 ArrayAdapter 填充的 ListView(我会尽快将其传递到 RecyclerView),它完美无瑕。每个 item_main 都有一个 Grid,其中包含 0x0 ImageView、0x1 TextView、1x0/1 Fragment,setVisibility Gone,通过“标准”OnClickListener 切换到 VISIBLE。 其中一个项目有一个动态片段,我通过 Listener 调用他的类,也许有错误。 问题是 Fragment 包含一个 RecyclerView 视图,它有各种 item_hours,包含一个 TablerRow,有 5 个 TextView。实际上,我正在填充市场的小时表;每次触摸 item_main 时都会“正确”调用 Fragment 类,但他的 onBindViewHolder 方法会随机调用 10 次(在我看来)。
我做了一个草稿让它变得更好: ListView: 4x item_main // item_main => 网格 => 图像、文本、片段 // 那个片段 => Recyclerview => TableRow => text, text, text, text, text // 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
【问题讨论】:
-
这是 RecyclerView 的自然工作。滚动时多次调用 OnbindViewHolder 方法,仅在滚动后创建的屏幕中的可见项目会破坏视图并显示新视图。
-
我刚刚解决了 put notifyDataSetChanged();在片段初始化之前:)
标签: android android-recyclerview fragment adapter