永远不会调用onCreateViewHolder

时间:2016-09-10 14:54:07

标签: android android-recyclerview android-adapter

我正在尝试使用片段内的recyclelerView。这是片段嵌套在viewPager中。

public class UniversityDetail extends Fragment {

RecyclerView universityDetailView;

//need to set Adapter

public static UniversityDetail newInstance(){
    return new UniversityDetail();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_university_lsit,container,false);
    universityDetailView = (RecyclerView)view;
    setupViews();
    return view;
}


private void setupViews(){
    //set the adapter
    UniversityDetailAdapter detailAdapter = new UniversityDetailAdapter(new ArrayList<UniversityDetails>());
    universityDetailView.addItemDecoration(new RecyclerListDecorater(getActivity()));
    universityDetailView.setAdapter(detailAdapter);
    universityDetailView.setLayoutManager(new LinearLayoutManager(getActivity()));
    universityDetailView.setHasFixedSize(false);
}

public RecyclerView getRecyclerView(){
    return this.universityDetailView;
   }
}

这是我想要在viewPager.It中的片段从onCreateView返回一个recyclerView。

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll"
android:paddingBottom="8dp"
android:paddingTop="?attr/actionBarSize"
android:scrollbars="vertical">

fragment_university_lsit.xml

recyclerView使用以下适配器。

public class UniversityDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<UniversityDetails> universityDetails;
private static Map<String,String> admissionRecommendation;

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ((DetailHolder)holder).bind(universityDetails.get(position));
}

@Override
public int getItemCount() {
    return universityDetails.size();
}

public UniversityDetailAdapter(List<UniversityDetails> details){
    this.universityDetails = details;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
    Log.d("CreateUniveristyDetail", "onCreateViewHolder: detail called");
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_fragment,parent,false);
    return new DetailHolder(itemView);
   }
}

DetailHolder是扩展ReyclerView.ViewHolder的类,它的实现与此无关。

适配器列表是从handler和notifyDataSetChanged()

更新的
private void setup(){
    mHandler = new Handler(Looper.getMainLooper()){
      @Override
        public void handleMessage(Message message){
          if(message.what == ConnectionHandler.DETAILMSG){
              Log.d("detail handle message", "handleMessage: Called");
              List<UniversityDetails> details = (List<UniversityDetails>)message.obj;
              UniversityDetailAdapter adapter = (UniversityDetailAdapter)universityDetail.getRecyclerView().getAdapter();
              adapter.addAll(details);
              adapter.notifyDataSetChanged();
              Log.d("size", "handleMessage: " + adapter.getItemCount());
          }
      }
    };
}

片段是在活动中创建的。

 private void setupNavigation(){
    ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    universityDetail = UniversityDetail.newInstance();
    pagerAdapter.addFragment(universityDetail);
    mViewPager.setAdapter(pagerAdapter);

    //setup the fragment transaction.

    headerTab.setViewPager(mViewPager,0);
    //no need to add to back stack
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.view_pager,universityDetail);
    transaction.commit();
}

DetailHolder的实现

 public  class DetailHolder extends RecyclerView.ViewHolder{

    CardView admissionGeneral;
    public View root;

    public DetailHolder (View itemView){
        super(itemView);
        root = itemView;
        admissionGeneral = (CardView)root.findViewById(R.id.admission_general);
    }

    public void bind(UniversityDetails detail){

        Log.d("detail bind", "bind: binding to recyclerView");

        }
    }

这里是adapter.getItemCount()的日志;返回3;这意味着正在添加某些内容,并且应该调用onCreateViewHolder,但它永远不会被调用。

奇怪的是我已经实现了另一个recyclerAdapter是sameProject并且它的工作非常好。

1 个答案:

答案 0 :(得分:0)

如果您可以展示如何从包含universityDetail用法的任何类中分配Handler,则此问题会更容易回答。

还不清楚如何将message.obj强制转换为List。

我相信你在那里使用片段的不同实例而不是你想要更新的片段。

所以,你知道这条线很好

List<UniversityDetails> details = (List<UniversityDetails>)message.obj;

因为你在执行此操作时在输出中看到3

Log.d("size", "handleMessage: " + adapter.getItemCount());

现在,这些线可能是问题所在。

UniversityDetailAdapter adapter = (UniversityDetailAdapter)universityDetail.getRecyclerView().getAdapter();
adapter.addAll(details);
adapter.notifyDataSetChanged();

首先,addAll没有RecyclerView.Adapter方法。所以我不确定如何编译。反正...

尽量将跨类引用保持在最低限度。在这里,我们只公开添加细节的方法,而不是整个RecyclerView。

然后,对于RecyclerViews,您应该仅使用notifyItemRangeInserted

通知已插入的数据而不是所有数据
public class UniversityDetailFragment extends Fragment {

    private RecyclerView universityDetailView;
    private UniversityDetailAdapter detailAdapter;

    private List<UniversityDetails> details = new ArrayList<UniversityDetails>();

    ...

    public void setupViews() {
        detailAdapter = new UniversityDetailAdapter(details);
        // etc...
    }

    public void addDetails(List<UniversityDetails> details) {
        int curSize = detailAdapter.getItemCount();
        this.details.addAll(details);
        detailAdapter.notifyItemRangeInserted(curSize, details.size()); 
    }
}

然后只使用片段的实例

  if(message.what == ConnectionHandler.DETAILMSG){
      Log.d("detail handle message", "handleMessage: Called");
      List<UniversityDetails> details = (List<UniversityDetails>) message.obj;
      universityDetail.addDetails(details);

另一个建议是为holder类使用正确的泛型类型,以避免不必要的强制转换。

public class UniversityDetailAdapter extends RecyclerView.Adapter<DetailHolder>