RecyclerView意外地表现出来

时间:2016-09-15 03:44:52

标签: java android android-recyclerview recycler-adapter

我尝试构建自定义RecyclerView,因为第一行的RecyclerView布局应该与其他行不同,如图所示

custom recyclerview

我已经搜索并找到了一些资源并获得了RecyclerView的自定义适配器解决方案

public class PostDetailAdapter extends RecyclerView.Adapter<ViewHolder>  {

    private static final String TAG = PostDetailAdapter.class.getName();

    private List<Solution> mSolutionList;
    private Issue mIssue;

    private Context mContext;
    public static final int ISSUE = 0;

    public PostDetailAdapter(Context mContext, Issue mIssue, List<Solution> mSolutionList){
        this.mContext = mContext;
        this.mIssue = mIssue;
        this.mSolutionList = mSolutionList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v;
        try {
            if (viewType == ISSUE) {
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.row_detail_issue, viewGroup, false);
                return new PostDetailViewHolder(v);
            } else {
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.row_detail_solution, viewGroup, false);
                return new AnswerDetailViewHolder(v);
            }
        }catch (Exception e){
            Toast.makeText(mContext, "Error caused by " + e.getCause(), Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Error caused by " + e.getCause());
            return null;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        try {
            if (viewHolder.getItemViewType() == ISSUE) {
                PostDetailViewHolder postDetailViewHolder = (PostDetailViewHolder) viewHolder;
                postDetailViewHolder.setTitleText(mIssue.getQ());
                postDetailViewHolder.setDescText(mIssue.getD());
            } else {
                AnswerDetailViewHolder answerDetailViewHolder = (AnswerDetailViewHolder) viewHolder;
                answerDetailViewHolder.setDescText(mSolutionList.get(position).getD());
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {                
            return 0;
        } else{                
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return (null != mSolutionList) ? mSolutionList.size() : 0 / 0;             

    }

}
MainActivity中的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecycler = (RecyclerView)findViewById(R.id.post_detail_recycler_view);
    mRecycler.setHasFixedSize(true);

    mSolutionList = populateSolutionList();

    mAdapter = new PostDetailAdapter(MainActivity.this, new Issue("Issue title", "Issue Desc"), mSolutionList);

    // Set up Layout Manager
    mManager = new LinearLayoutManager(MainActivity.this);
    mManager.setReverseLayout(false);
    mManager.setStackFromEnd(false);

    mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
    mStaggeredGridLayoutManager.setReverseLayout(false);
    mRecycler.setLayoutManager(mStaggeredGridLayoutManager);

    mRecycler.setAdapter(mAdapter);    


}

private List<Solution> populateSolutionList() {

    List<Solution> solutions = new CopyOnWriteArrayList<>();

    solutions.add(new Solution("Solution1"));
    solutions.add(new Solution("Solution2"));
    solutions.add(new Solution("Solution3"));
    solutions.add(new Solution("Solution4"));
    solutions.add(new Solution("Solution5"));
    solutions.add(new Solution("Solution6"));
    solutions.add(new Solution("Solution7"));
    solutions.add(new Solution("Solution8"));
    solutions.add(new Solution("Solution9"));

    return Solutions;
}

此时预先填充的List<Soultion>效果正常并符合预期,但当我想删除populateSolutionList()并通过点击添加Solution

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCount +=1;
            Solution solution = new Solution("Solution " + mCount);
            mSolutionList.add(solution);
            mAdapter.notifyDataSetChanged();
            Log.e("MAIN_ACTIVITY", "Click return..." + mCount);
        }
    });
适配器中的

我已经改变

@Override
    public int getItemCount() {
        try {
            if (!mSolutionList.isEmpty()){
                int i = (null != mSolutionList) ? mSolutionList.size() : 0 / 0;
                return (i);
            }else {
                return 1;
            }
        }catch (Exception e){
            return 0;
        }

    }

当我点击第一个解决方案行已添加但未显示在RecyclerView所有其他行显示时,我发现它是适配器getItemCount()的问题任何人都建议我如何解决此问题< / p>

1 个答案:

答案 0 :(得分:0)

您必须考虑列表中的第一行(问题),并且您添加的任何解决方案都将从第二行添加。您的项目计数应始终为+1。

因此,getItemCount()应该返回mSolutionList.size() + 1