我似乎无法让我的RecyclerAdapter响应正确添加数据。 这是我使用RecyclerView
的Activitypublic class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
FloatingActionMenu fam;
//startActivity(intent);
private RecyclerView mRecyclerView;
ArrayList<TeamInfo> teamsInfo;
SelectedTeamsRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if(savedInstanceState == null) {
if(teamsInfo == null) {
Log.d("TEAM SELECTION", "teams Info == null");
teamsInfo = new ArrayList<>();
}
}
else {
if(teamsInfo == null) {
teamsInfo = savedInstanceState.getParcelableArrayList("selectedTeams");
}
}*/
teamsInfo = new ArrayList<>();
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.menu_item);
fam = (FloatingActionMenu) findViewById(R.id.menu);
fam.setClosedOnTouchOutside(true);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
addClicked();
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.selected_teams_recyclerview);
adapter = new SelectedTeamsRecyclerAdapter(this, teamsInfo);
adapter.setHasStableIds(true);
mRecyclerView.setAdapter(adapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//teamsInfo = TeamInfo.restoreTeamInfo(this);
//adapter.setHasStableIds(true);
//mRecyclerView.setItemAnimator(null);
//DragSortRecycler dragSortRecycler = new DragSortRecycler();
mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
//mRecyclerView.addOnItemTouchListener(dragSortRecycler);
//mRecyclerView.addOnScrollListener(dragSortRecycler.getScrollListener());
//dragSortRecycler.setViewHandleId(R.id.selected_teams_draghandle); //View you wish to use as the handle
/*dragSortRecycler.setOnItemMovedListener(new DragSortRecycler.OnItemMovedListener() {
@Override
public void onItemMoved(int from, int to) {
itemMoved(from, to);
}
});
*/
}
public void itemMoved(int from, int to) {
TeamInfo item = teamsInfo.remove(from);
teamsInfo.add(to, item);
adapter.notifyItemInserted(teamsInfo.size() - 1);
}
public void addClicked() {
fam.close(true);
getNewTeamValues();
//Intent intent = new Intent(this, NewTeamActivity.class);
}
public void addNewTeam(String teamName, int primaryColor, int secondaryColor) {
teamsInfo.add(new TeamInfo(teamName, primaryColor, secondaryColor));
//adapter.mTeamsInfo.add(new TeamInfo(teamName, primaryColor, secondaryColor));
//adapter.addItem(new TeamInfo(teamName, primaryColor, secondaryColor));
adapter.notifyDataSetChanged();
}
public void getNewTeamValues() {
TextView nameTV;s
MaterialDialog dialog = new MaterialDialog.Builder(this)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
// TODO
LineColorPicker primaryColorPicker = (LineColorPicker) dialog.getCustomView().findViewById(R.id.primaryColor);
LineColorPicker secondaryColorPicker = (LineColorPicker) dialog.getCustomView().findViewById(R.id.secondaryColor);
EditText newTeamNameEditText = (EditText) dialog.getCustomView().findViewById(R.id.teamNameEditText);
View v = dialog.getContentView();
String teamName = newTeamNameEditText.getText().toString();
int primaryColor = primaryColorPicker.getColor();
int secondaryColor = secondaryColorPicker.getColor();
addNewTeam(teamName, primaryColor, secondaryColor);
}
})
.title("Enter Team Info")
.customView(R.layout.dialog_new_team, true)
.positiveText("Add")
.negativeText("Cancel").build();
dialog.show();
}
}
这是我的自定义适配器
public class SelectedTeamsRecyclerAdapter extends RecyclerView.Adapter<SelectedTeamsRecyclerAdapter.ViewHolder>{
public ArrayList<TeamInfo> mTeamsInfo;
Context mContext;
public SelectedTeamsRecyclerAdapter(Context context, ArrayList<TeamInfo> teamsInfo) {
mTeamsInfo = teamsInfo;
mContext = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView teamNameTV;
public View primaryColorV;
public View secondaryColorV;
public ViewHolder(View itemView) {
super(itemView);
teamNameTV = (TextView) itemView.findViewById(R.id.selected_teams_teamname);
primaryColorV = (View) itemView.findViewById(R.id.selected_teams_primarycolor);
secondaryColorV = (View) itemView.findViewById(R.id.selected_teams_secondarycolor);
}
}
@Override
public int getItemCount() {
return mTeamsInfo.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
TeamInfo teamInfo = mTeamsInfo.get(position);
holder.teamNameTV.setText(teamInfo.getTeamName());
holder.primaryColorV.setBackgroundColor(teamInfo.getPrimaryColor());
holder.secondaryColorV.setBackgroundColor(teamInfo.getSecondaryColor());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.selected_teams_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public long getItemId(int position) {
return position;
}
public void addItem(TeamInfo teamInfo) {
mTeamsInfo.add(teamInfo);
}
/*public ArrayList<TeamInfo> getTeamsInfo() {
return this.teamsInfo;
}*/
}
我过去写的几乎完全相同的代码,一切都很顺利。
答案 0 :(得分:0)
首先,你没有添加mRecyclerView.setLayoutManager(mLayoutManager )
如果你有任何问题,请尝试添加并告诉我。