Прежде всего, я должен упомянуть, что использую Roboguice.Roboguice view injection: Внедрение пользовательского вида в адаптере RecyclerView не удается
У меня есть Activity
с RecyclerView
. Я пытаюсь раздуть пользовательский вид внутри моего адаптера RecyclerView's
. Я использую Roboguice
в своем проекте.
MainActivity.java
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.recycler_view) RecyclerView recyclerView;
private UserAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<User> users = fetchUsers();
mAdapter = new UserAdapter(this,users);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
}
UserAdapter.java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.CustomViewHolder> {
@Inject CustomView.Provider myViewProvider;
private List<User> userList;
private Context mContext;
public UserAdapter(Context context, List<User> userList) {
this.userList = userList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = myViewProvider.get(parent);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int position) {
((CustomViewHolder) holder).init(userList.get(position));
}
@Override
public int getItemCount() {
return (null != userList ? userList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
private final CustomView myView;
public CustomViewHolder(View itemView) {
super(itemView);
myView = (CustomView) itemView;
}
public void init(final User user) {
myView.init(user);
}
}
}
CustomView.java
@ProvidedBy(CustomView.Provider.class)
public class CustomView extends RelativeLayout {
private View caret;
private TextView questionText;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
caret = findViewById(R.id.view_caret);
questionText = (TextView) findViewById(R.id.view_question);
}
public CustomView init(final User user) {
this.user = user;
//Set view
return this;
}
public static class Provider extends ViewProvider<CustomView> {
@Inject
public Provider(LayoutInflaterWithInjection<CustomView> inflater) {
super(R.layout.module_user, inflater);
}
}
}
Так что, когда я запускаю это, приложение получает разбился. пиловочника -
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.user.CustomView$Provider.get(android.view.ViewGroup)' on a null object reference
at com.user.UserAdapter.onCreateViewHolder(UserAdapter.java:356)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5228)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:16678)
который я предполагаю, потому что мой CustomView не инициализирован. Может ли кто-нибудь помочь? Благодаря !!
Ничего себе !! Это сработало отлично. Большое спасибо !! – user1001