如何从Fragment正确获取RecyclerView适配器?

时间:2017-10-26 11:07:05

标签: android android-fragments

我有一个片段,使用扩展ListRecyclerViewAdapter的课程RecyclerView.Adapter显示列表:

public class ListFragment extends Fragment {

    private ListRecyclerViewAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...
        adapter = new ListRecyclerViewAdapter(context, itemsToDisplay);
        ...
    }

    public ListRecyclerViewAdapter getListAdapter() {
        return adapter;
    }
}

当我尝试从具有上述片段的活动中获取适配器时,我得到null

public class DataActivity extends FragmentActivity {

 @Override
 protected void onResume() {
    super.onResume();
    listFragment = new ListFragment(itemsToDisplay);
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();
    ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
 }

}

为什么?在我看来,当我调用onCreateView()时,我没有调用Fragment的方法listFragment.getAdapter()

2 个答案:

答案 0 :(得分:0)

当您尝试调用方法时,不会创建片段。所以检查是否在访问适配器之前附加了片段。

public ListRecyclerViewAdapter getListAdapter() {
    if(this.isAdded() && adapter!=null){
        return adapter;
    }
 return null;   
}

还在活动的oncreate中加载片段并在onresume中调用adpter引用方法

public class DataActivity extends FragmentActivity {

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listFragment = new ListFragment(itemsToDisplay);
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();
        }
        @Override
        protected void onResume() {
            super.onResume();
            ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
        }

    }

答案 1 :(得分:0)

一旦创建,就会调用listfragment adapter。因此,UI创建将花费一些时间,但流程流向该行。所以它返回null。你可以通过以下方式实现这一点。

public class ListFragment extends Fragment {
LoadAdapter loadAdapter;
private ListRecyclerViewAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    adapter = new ListRecyclerViewAdapter(context, itemsToDisplay);
    ...
}

public ListRecyclerViewAdapter getListAdapter() {
    return adapter;
}
interface LoadAdapter {
public void onAdapterLoaded();
}
 public void setOnAdapterLoad(LoadAdapter loadAdapter){
  this.loadAdapter = loadAdapter;
 }
 @Override
 public void onResume(){
    loadAdapter.onAdapterLoaded();
 }

}

现在活动代码如下:

public class DataActivity extends FragmentActivity {

@Override
protected void onResume() {
  super.onResume();
  listFragment = new ListFragment(itemsToDisplay);
  listFragment.setOnAdapterLoad(new LoadAdapter(){
   @Override
    public void onAdapterLoaded(){
        ListRecyclerViewAdapter wantedAdapter = listFragment.getAdapter();
    }
  });
  FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_list_container, listFragment).commit();

}

}

希望这有帮助。