這是我的情況:我有一個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
這是RecyclerView的自然的工作。在滾動時,OnbindViewHolder方法會多次調用,只有在滾動顯示視圖銷燬和新視圖後創建的屏幕中才會顯示可見項。 –
我剛剛通過把notifyDataSetChanged()解決;在片段初始化之前:) – 4face