代码在Android中的setLayoutManager之后跳过线条

时间:2016-11-12 16:16:30

标签: android android-recyclerview fragment

我以前从未见过这样的人。代码只是跳过某些行没有错误或异常。

我有一个正在做某事的课程,当它完成时返回Adapter并且它正常工作,适配器包含正确的数字并正确返回。在我的Fragment之后,我尝试将此AdapterRecyclerView一起使用并创建列表。这是我的片段:

public class ServicesFragment extends Fragment {

ArrayList<Item> list;
public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mLayoutManager;
public ItemAdapter itemAdapter;
DownloadDataThread downloadDataThread;
public static boolean dataisready;
ProgressDialog dialog;

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_service, parent, false);

    mRecyclerView = (RecyclerView)v.findViewById(R.id.services_recycler_view);
    list = new ArrayList<Item>();
    list.add(0,new Item());
    itemAdapter = new ItemAdapter(list);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(itemAdapter);

    downloadDataThread = ((MainActivity)getActivity()).downloadDataThread;


    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

    Runnable helloRunnable = new Runnable() {
        public void run() {
            dataisready = downloadDataThread.ready;
            if(dataisready){
                itemAdapter = downloadDataThread.getItemAdapter();
                Log.d("THREAD","adapter " + itemAdapter);
                readAndLoadList(itemAdapter);
                executor.shutdown();
            }
            Log.d("THREAD","nézem "+dataisready);
        }
    };
    executor.scheduleAtFixedRate(helloRunnable, 0, 200, TimeUnit.MILLISECONDS);

    return v;
}

private void readAndLoadList(ItemAdapter itemAdapter1){
    itemAdapter = itemAdapter1;
    itemAdapter.notifyDataSetChanged();
    Log.d("THREAD"," adaptert22 "+itemAdapter.getItemCount());
    mRecyclerView.setHasFixedSize(true);
    Log.d("THREAD","muszáfá0 "+ mRecyclerView);
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    Log.d("THREAD","muszáfá1 "+ mLayoutManager);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // And after that the other logs never shows up

    Log.d("THREAD","muszáfá2 "+ mLayoutManager);
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    Log.d("THREAD","muszáfá3 "+ mLayoutManager);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
    Log.d("THREAD","muszáfá4 ");
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    Log.d("THREAD","muszáfá5 ");
    mRecyclerView.setAdapter(itemAdapter);
    mRecyclerView.invalidate();
    mRecyclerView.getAdapter().getItemCount();
    Log.d("THREAD","lássuk az adaptert33 "+mRecyclerView.getAdapter().getItemCount());
}

}

因为你可以看到我每200毫秒检查一下适配器是否准备就绪,当它准备就绪时我会带着它的适配器返回并尝试加载RecyclerView但在该行之后:mRecyclerView.setLayoutManager(mLayoutManager);˛其他日志永远不会显示。

在你问我之前,我已经在onCreateView的开头创建了一个完整的RecyclerView并加载,因为我总是在RecyclerView上出错,但在我添加之前它也很糟糕。

你有什么想法吗?

0 个答案:

没有答案