BottomSheetDialog+Recyclerview一次记录

记一次Recyclerview+BottomSheetDialog的简单使用

列子里用到的依赖


    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.android.support:design:27.1.0'

BottomSheetDialog+Recyclerview一次记录_第1张图片大致形式是这样,没有实现点赞,收藏和喜欢功能,底部可以弹出输入框,并加到列表中显示。

/**
 *srdx
 *2019/8/6
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ArrayList list=new ArrayList();
    private RecyclerView recyclerView;
    private CommitAdapter mAdapter;
    private GridLayoutManager mLayoutManager;
    private BottomSheetDialog dialog;
    private TextView bt_comment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
    private void initData(){
        for (int i=0;i<10;i++){
            list.add("这是第"+i+"条评论");
        }
    }
    private void initView(){
        recyclerView=findViewById(R.id.recy);
        mAdapter=new CommitAdapter(this,list);
        recyclerView.setAdapter(mAdapter);
        mLayoutManager=new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(mLayoutManager);
        bt_comment =  findViewById(R.id.detail_page_do_comment);
        bt_comment.setOnClickListener(this);
    }
    private void showCommentDialog(){
        dialog =new BottomSheetDialog(this);
        View commentView = LayoutInflater.from(this).inflate(R.layout.comment_dialog_layout,null);
        final EditText commentText=commentView.findViewById(R.id.dialog_comment_et);
        final Button bt_comment=commentView.findViewById(R.id.dialog_comment_bt);
        dialog.setContentView(commentView);
        View parent = (View) commentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        commentView.measure(0,0);
        behavior.setPeekHeight(commentView.getMeasuredHeight());

        bt_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String commentContent=commentText.getText().toString().trim();
                if(!TextUtils.isEmpty(commentContent)){
                    dialog.dismiss();
                    mAdapter.addNewComment(commentContent);
                    Toast.makeText(MainActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.show();
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.detail_page_do_comment){
            Log.d("click","this is click---");
            showCommentDialog();
        }
    }

}
public class CommitAdapter extends RecyclerView.Adapter {
    private ArrayList list = new ArrayList();//数据源
    public Context context;
    public LayoutInflater inflater;
    private int ThemPsersonType = 0;
    private int NormalType = 1;
    public CommitAdapter(Context context, ArrayList list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if(viewType==NormalType){
            return new ViewHolder(inflater.from(context).inflate(R.layout.comment_item_layout, null));
        }else{
            return new ThemPersonHolder(inflater.from(context).inflate(R.layout.themperson_comment,null));
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView commit;

        public ViewHolder(View itemView) {
            super(itemView);
            commit = itemView.findViewById(R.id.comment_item_content);
        }
    }

    class ThemPersonHolder extends RecyclerView.ViewHolder {
        public ThemPersonHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder){
            ((ViewHolder) holder).commit.setText(list.get(position-1).toString());
        }

    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ThemPsersonType;
        } else {
            return NormalType;
        }
        //return super.getItemViewType(position);
    }

    public void addNewComment(String comment) {
        list.add(comment);
        notifyDataSetChanged();
    }
}

 

你可能感兴趣的:(Android,Recyclerview)